Skip to main content

How to Set an Alias on a Model Version in MLflow with Python

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

An alias, such as champion, is a movable name for one version of a registered model. Code that loads models:/<name>@champion gets whichever version the alias points to, so releasing a new version is one call that moves the alias. This guide sets an alias with MlflowClient, moves it, and reads the result, with real output from MLflow 3.18.0.

Syntax​

Syntax
client = MlflowClient()
client.set_registered_model_alias(name, alias, version)
client.get_registered_model(name).aliases
PartWhat it means
nameThe registered model
aliasThe alias, such as champion; it's created, or moved if it already points to another version
versionThe version number it should point to, such as "2"
.aliasesA dictionary of every alias of the registered model and the version it points to

The reference for set_registered_model_alias: "Set a registered model alias pointing to a model version." On the name: "aliases of the format v<number>, such as v9 and v42, are reserved and cannot be set."

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: Set the Alias, Then Move It​

The example registers two versions of a breast cancer classifier, cancer-screening: the second uses stronger regularization. It points champion at version 1, then at version 2, printing the aliases after each call:

The set_alias.py script in a macOS-style code window: two logistic regression versions of cancer-screening registered in a loop, then set_registered_model_alias champion to version 1, print the aliases, set it to version 2, print them again.

set_alias.py
import mlflow
from mlflow import MlflowClient
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("set-alias-example")
client = MlflowClient()
name = "cancer-screening"

X, y = load_breast_cancer(return_X_y=True, as_frame=True)
y = y == 0 # True when the tumour is malignant
for c in (1.0, 0.1): # two versions, with stronger regularization in the second
model = make_pipeline(StandardScaler(), LogisticRegression(C=c)).fit(X, y)
with mlflow.start_run():
mlflow.sklearn.log_model(model, name="model", registered_model_name=name, input_example=X.head(3))

client.set_registered_model_alias(name, "champion", "1")
print("After the first call: ", client.get_registered_model(name).aliases)

client.set_registered_model_alias(name, "champion", "2")
print("After the second call:", client.get_registered_model(name).aliases)
Terminal
uv run python set_alias.py
Output (last lines)
After the first call: {'champion': '1'}
After the second call: {'champion': '2'}

The end of the output in a terminal: After the first call champion points to 1, after the second call champion points to 2.

The first call created champion on version 1. The second didn't add a second champion: it moved the one alias to version 2. That move is a release: anything that loads models:/cancer-screening@champion now gets version 2.

Common Mistakes​

MistakeWhat happensDo this instead
Naming an alias v2MLflow reserves v plus a number, so it can't be setUse a word, such as champion or staging
Hard-coding version numbers in the code that loads the modelEvery release needs a code changeLoad models:/<name>@<alias> and move the alias

Troubleshooting​

SymptomLikely causeFix
The alias still points to the old versionThe call went to another registered model or versionPrint get_registered_model(name).aliases to check
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​

How do I roll back?​

Point the alias at the earlier version with the same call: client.set_registered_model_alias(name, "champion", "1").

Can I do this in the UI?​

Yes, from the registered model's page: How to Add an Alias to a Model Version in the MLflow UI.

Conclusion​

client.set_registered_model_alias("cancer-screening", "champion", "1") gave version 1 the alias, and the same call with "2" moved it, as get_registered_model(name).aliases showed: {'champion': '1'}, then {'champion': '2'}.

Additional Resources​