MLflow mlflow.exceptions.MlflowException: Model version already exists

A model version with the specified name and version already exists in the model registry.

Understanding MLflow and Its Purpose

MLflow is an open-source platform designed to manage the machine learning lifecycle, including experimentation, reproducibility, and deployment. It provides tools to track experiments, package code into reproducible runs, and share and deploy models. One of its core components is the Model Registry, which is a centralized store to manage the full lifecycle of an MLflow Model, including versioning, stage transitions, and annotations.

Identifying the Symptom: Model Version Already Exists

When working with MLflow, you might encounter the error: mlflow.exceptions.MlflowException: Model version already exists. This error typically occurs when you attempt to register a model version that already exists in the MLflow Model Registry.

What You Observe

Upon trying to register a new model version, the process fails, and the above exception is thrown. This prevents the new version from being added to the registry.

Explaining the Issue: Why Does This Error Occur?

The error arises because MLflow enforces unique version numbers for each model within the registry. If you attempt to register a model with a version number that already exists, MLflow will raise an exception to prevent duplicate entries.

Root Cause

The root cause of this issue is that a model version with the specified name and version already exists in the model registry. This could happen if you are trying to re-register a model without incrementing the version number.

Steps to Fix the Issue

To resolve this issue, you have a couple of options:

Option 1: Use a Different Version Number

  1. Before registering a new model version, check the existing versions using the MLflow UI or the MLflow API.
  2. Increment the version number to a new, unused number.
  3. Register the model with the new version number. For example:

import mlflow

client = mlflow.tracking.MlflowClient()
client.create_registered_model("MyModel")
client.create_model_version(name="MyModel", source="path/to/model", run_id="run-id")

Option 2: Delete the Existing Version

If the existing version is no longer needed, you can delete it to free up the version number:

  1. Use the MLflow API to delete the existing model version:

client.delete_model_version(name="MyModel", version=1)

Note: Deleting a model version is irreversible, so ensure that the version is no longer needed before proceeding.

Additional Resources

Master

MLflow

in Minutes — Grab the Ultimate Cheatsheet

(Perfect for DevOps & SREs)

Most-used commands
Real-world configs/examples
Handy troubleshooting shortcuts
Your email is safe with us. No spam, ever.

Thankyou for your submission

We have sent the cheatsheet on your email!
Oops! Something went wrong while submitting the form.

MLflow

Cheatsheet

(Perfect for DevOps & SREs)

Most-used commands
Your email is safe with us. No spam, ever.

Thankyou for your submission

We have sent the cheatsheet on your email!
Oops! Something went wrong while submitting the form.

MORE ISSUES

Made with ❤️ in Bangalore & San Francisco 🏢

Doctor Droid