Skip to main content

How to Load a Registered Model by Alias in MLflow

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Code that serves a model shouldn't need a change every time a better version is registered. With an alias, it doesn't: it loads models:/<name>@<alias>, and MLflow hands it whichever version the alias points to at that moment. This guide loads a model by alias, moves the alias, and loads again with the same line. Real output from MLflow 3.18.0.

Syntax​

Syntax
model = mlflow.pyfunc.load_model("models:/registered-model-name@alias")
PartWhat it means
registered-model-nameThe registered model
aliasThe alias, such as production or champion
modelThe version the alias points to, ready to predict

MLflow's Model Registry workflows page: "To fetch a model version by alias, specify the model alias in the model URI, and it will fetch the model version currently under it." The same address works with each flavor's load_model, such as mlflow.sklearn.load_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 by Alias, Move the Alias, Load Again​

The example registers two versions of a model that predicts how far diabetes progresses in a year: a linear regression, then a ridge regression. It points production at version 1 and loads models:/diabetes-progression@production, then points production at version 2 and loads the same address:

The load_by_alias.py script in a macOS-style code window: two versions of diabetes-progression registered in a loop, then production set to version 1, the model loaded from models:/diabetes-progression@production and its predictions printed, then production moved to version 2 and the same address loaded again.

load_by_alias.py
import mlflow
from mlflow import MlflowClient
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression, Ridge

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("load-by-alias-example")
client = MlflowClient()
name = "diabetes-progression"

X, y = load_diabetes(return_X_y=True, as_frame=True)
for model in (LinearRegression(), Ridge(alpha=1.0)): # versions 1 and 2
model.fit(X, y)
with mlflow.start_run():
mlflow.sklearn.log_model(model, name="model", registered_model_name=name, input_example=X.head(3))

patients = X.head(2)
client.set_registered_model_alias(name, "production", "1")
model = mlflow.pyfunc.load_model(f"models:/{name}@production")
print("@production on version 1:", model.predict(patients).round(1))

client.set_registered_model_alias(name, "production", "2")
model = mlflow.pyfunc.load_model(f"models:/{name}@production")
print("@production on version 2:", model.predict(patients).round(1))
Terminal
uv run python load_by_alias.py
Output (last lines)
@production on version 1: [206.1 68.1]
@production on version 2: [182.7 91. ]

The end of the output in a terminal: production on version 1 predicts 206.1 and 68.1 for the two patients, production on version 2 predicts 182.7 and 91.

The same address gave different predictions: 206.1 and 68.1 from the linear regression, then 182.7 and 91 from the ridge regression, once the alias pointed to it. The loading code never changed; only the alias moved.

Common Mistakes​

MistakeWhat happensDo this instead
Loading the model once and keeping it for monthsA moved alias isn't picked up until the next loadLoad again when you want the current version, such as at each start
Logging only the aliasYou can't tell later which version made a predictionLook the version up first and log its number

Troubleshooting​

SymptomLikely causeFix
The address loads an old versionThe alias wasn't moved, or the model was loaded before it movedCheck the alias, then load again
INVALID_PARAMETER_VALUE: Registered model alias production not found.The alias doesn't exist on that registered modelSet it first, or check its spelling

FAQ​

How do I know which version the alias gave me?​

Ask the registry before loading: client.get_model_version_by_alias(name, "production").version. How to Find Which Version an Alias Points To in MLflow shows it.

Can I load a fixed version instead?​

Yes, with models:/<name>/<version>: How to Load a Specific Version of a Registered Model in MLflow.

Conclusion​

mlflow.pyfunc.load_model("models:/diabetes-progression@production") loaded version 1 while production pointed to it and version 2 after the alias moved, with no change to the loading code.

Additional Resources​