Skip to main content

How to Find Which Version an Alias Points To in MLflow

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Loading models:/<name>@champion gives you a model but not its version number, and that number is what you want in a log line, a report or a bug ticket. MlflowClient.get_model_version_by_alias answers which version an alias points to right now, with the run that trained it and the model it holds. Real output from MLflow 3.18.0.

Syntax​

Syntax
version = MlflowClient().get_model_version_by_alias(name, alias)
PartWhat it means
nameThe registered model
aliasThe alias, such as champion
versionA ModelVersion: its version number, aliases, run_id, source, tags and more

The reference for get_model_version_by_alias: "Get the model version instance by name and alias."

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: Look Up the Champion​

The example registers two versions of an iris classifier, iris-species, each from a run named after its setting, and points champion at version 2. Then it asks which version champion points to and prints what the version records:

The version_by_alias.py script in a macOS-style code window: two logistic regressions registered as iris-species from runs logreg-c-1.0 and logreg-c-0.1, champion set to version 2, then get_model_version_by_alias and prints of its version, aliases, run name and source.

version_by_alias.py
import mlflow
from mlflow import MlflowClient
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("version-by-alias-example")
client = MlflowClient()
name = "iris-species"

iris = load_iris(as_frame=True)
X, y = iris.data, iris.target_names[iris.target]
for c in (1.0, 0.1): # versions 1 and 2
with mlflow.start_run(run_name=f"logreg-c-{c}"):
mlflow.sklearn.log_model(
LogisticRegression(C=c, max_iter=1000).fit(X, y),
name="model",
registered_model_name=name,
input_example=X.head(3),
)
client.set_registered_model_alias(name, "champion", "2")

champion = client.get_model_version_by_alias(name, "champion")
print("version:", champion.version)
print("aliases:", champion.aliases)
print("run: ", client.get_run(champion.run_id).info.run_name)
print("source: ", champion.source)
Terminal
uv run python version_by_alias.py
Output (last lines)
version: 2
aliases: ['champion']
run: logreg-c-0.1
source: models:/m-9321b272e2f745fcacc5a5f671d13e31

The end of the output in a terminal: version 2, aliases champion, run logreg-c-0.1, and the source models:/m-9321b272e2f745fcacc5a5f671d13e31.

  • version: 2: the number the alias points to.
  • aliases: ['champion']: every alias on that version.
  • run: logreg-c-0.1: the run that trained it, found from the version's run_id.
  • source: the logged model the version holds.

Common Mistakes​

MistakeWhat happensDo this instead
Looking up the alias, then loading @champion separatelyIf the alias moves in between, you log one version and load anotherLoad models:/<name>/<version> with the number you looked up
Using it to check whether an alias existsA missing alias raises an errorRead client.get_registered_model(name).aliases instead

Troubleshooting​

SymptomLikely causeFix
INVALID_PARAMETER_VALUE: Registered model alias champion not found.The registered model has no such aliasSet it, or check its spelling
RESOURCE_DOES_NOT_EXIST: Registered Model with name=... not foundThe registered model's name is wrongCheck the name in the Model Registry

FAQ​

How do I list every alias at once?​

client.get_registered_model(name).aliases returns them all as a dictionary of alias to version number.

Does the version know its trained model?​

Yes, through source: How to Find the Run and Logged Model Behind a Model Version in MLflow.

Conclusion​

client.get_model_version_by_alias("iris-species", "champion") returned version 2, its aliases, the run that trained it, logreg-c-0.1, and the logged model it holds.

Additional Resources​