Sub topic 9
Loading a Model by Alias or Version
Assume you know basic Python, MLflow Tracking, and how to train a scikit-learn model. This chapter also assumes that the registered model iris-classifier already exists and has at least two versions.
A registered model can have many model versions. Each version points to a model artifact produced by a run. Your application needs a stable way to select one of those versions. MLflow supports two common choices:
- An alias is a changeable name that points to one version, such as
champion. - A version number is a fixed identifier, such as
1or2.
The choice affects how deployments behave when the registry changes.
| Model URI | Selects | Behavior |
|---|---|---|
| `models:/iris-classifier@champion` | The version assigned to `champion` | Changes when the alias moves |
| `models:/iris-classifier/2` | Version 2 | Always selects version 2 |
Load a model through an alias
Set the tracking URI before loading. The tracking URI tells the MLflow client which server contains the registry and its metadata.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
model = mlflow.pyfunc.load_model(
"models:/iris-classifier@champion"
)
prediction = model.predict([[5.1, 3.5, 1.4, 0.2]])
print(prediction)mlflow.pyfunc.load_model returns a generic MLflow model interface. The interface provides predict, even when the original model was trained with scikit-learn, XGBoost, or another supported library.
The alias is resolved when the application loads the model. If an administrator later moves champion from version 2 to version 3, the same application code loads version 3 the next time it starts. You can promote a model without editing or rebuilding the application.
Load a fixed version
Use a numeric version when you need reproducibility or an intentional pin.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
model = mlflow.pyfunc.load_model(
"models:/iris-classifier/2"
)
prediction = model.predict([[5.1, 3.5, 1.4, 0.2]])
print(prediction)This URI continues to select version 2 even if champion moves to another version. A test, audit, or reproducibility workflow often benefits from this behavior.
Decide which URI to use
Use an alias in serving code when operations teams promote models independently of application releases. Common aliases include champion, candidate, and shadow.
Use a version number when you need to reproduce a historical result, compare two known versions, or prevent a job from changing its model unexpectedly.
An alias does not copy model files. It only stores a named pointer to a registered model version. The version still points to the artifact location recorded when the model was registered.
If an alias does not exist, MLflow raises an error when it resolves the URI. If a numeric version does not exist, loading also fails. Check the registry before deploying, and make sure the server and artifact storage are reachable from the process that loads the model.
Exercise
You want a production service to receive a newly validated model without changing its source code. Which URI should the service use?
Show answer
Use an alias URI such as models:/iris-classifier@champion. Move the champion alias to the validated version after it passes the promotion checks.