Skip to main content

How to Load a Specific Version of a Registered Model in MLflow

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

An alias follows whichever version it points to. Sometimes you want the opposite: exactly version 1, to reproduce an old result or to compare it with version 2. A registered model's address can name the version number. This guide loads two versions of a model by number and compares their predictions. Real output from MLflow 3.18.0.

Syntax​

Syntax
model = mlflow.pyfunc.load_model("models:/registered-model-name/version")
PartWhat it means
registered-model-nameThe registered model
versionThe version number, such as 1
modelThat version, ready to predict

MLflow's Model Registry page: "You can refer to the registered model by using a URI of this format: models:/<model-name>/<model-version>". A version's model never changes once it's registered, so the address always loads the same model.

Prerequisites​

  • An MLflow tracking server with a database backend at http://127.0.0.1:8080, such as mlflow server --port 8080 with its default SQLite database. The registry needs one.
  • mlflow and scikit-learn in your project, for example with uv add mlflow scikit-learn pandas.

Step 1: Load Two Versions by Number​

The example registers two ridge regressions on scikit-learn's diabetes data as diabetes-ridge: version 1 with light regularization, alpha=0.1, version 2 with heavy, alpha=10.0. Then it loads each version by number and predicts for the same two patients:

The load_version.py script in a macOS-style code window: two Ridge models with alpha 0.1 and 10.0 registered as diabetes-ridge, then a loop over versions 1 and 2 that loads models:/diabetes-ridge/ plus the version number and prints its predictions.

load_version.py
import mlflow
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("load-version-example")
name = "diabetes-ridge"

X, y = load_diabetes(return_X_y=True, as_frame=True)
for alpha in (0.1, 10.0): # version 1 fits the data closely, version 2 is smoothed
with mlflow.start_run():
mlflow.sklearn.log_model(
Ridge(alpha=alpha).fit(X, y), name="model", registered_model_name=name, input_example=X.head(3)
)

patients = X.head(2)
for version in (1, 2):
model = mlflow.pyfunc.load_model(f"models:/{name}/{version}")
print(f"Version {version}:", model.predict(patients).round(1))
Terminal
uv run python load_version.py
Output (last lines)
Version 1: [199.8 73.4]
Version 2: [159.9 132.1]

The end of the output in a terminal: version 1 predicts 199.8 and 73.4 for the two patients, version 2 predicts 159.9 and 132.1.

Each address loaded its own version. Version 2's heavy regularization pulls both predictions toward the average: 159.9 and 132.1 instead of 199.8 and 73.4.

Common Mistakes​

MistakeWhat happensDo this instead
Hard-coding a version number in code that should follow releasesIt keeps loading the old version after a better one is releasedLoad an alias, such as models:/<name>@champion
Expecting version 1 to change after retrainingRegistering again creates version 2; version 1 stays as it wasLoad the new version's number, or move an alias to it

Troubleshooting​

SymptomLikely causeFix
The model loads but predicts differently than expectedThe wrong version numberCheck the number against the registered model's page
The registry calls fail on your own serverThe server stores runs in files, not a databaseStart it with a database backend, such as its default SQLite database

FAQ​

When should I use a version number instead of an alias?​

When you need the same model every time: a test that compares results, a report that reproduces old numbers, or a side-by-side comparison like this one.

How do I find the newest version's number?​

Search the versions newest first: How to Get the Latest Version of a Registered Model in MLflow.

Conclusion​

mlflow.pyfunc.load_model("models:/diabetes-ridge/1") and .../2 loaded the two registered versions, and their predictions for the same patients differed exactly as their regularization did.

Additional Resources​