Sub topic 11
Registering Models and Using Aliases
This chapter assumes that you know basic Python, can train a scikit-learn model, and understand MLflow runs, artifacts, and metrics. It also assumes that a tracking server is running and that your script can connect to it.
Logging a model as a run artifact preserves a model file with one experiment. The Model Registry adds a shared name, version history, and deployment label. This makes it easier for another process or service to load the model selected for production.
From a run to a registered model
When you register a model, MLflow creates a registered model with a stable name such as iris-classifier. Each registration creates a new version. A version points to the model artifact produced by a particular run.
A registered model and a model version have different roles:
| Object | Purpose | Example |
|---|---|---|
| Registered model | Stable identity shared by a team | `iris-classifier` |
| Model version | One immutable registered artifact | Version `3` |
| Alias | Human-readable pointer to a version | `champion` |
The model name stays the same while versions change. An alias lets application code request a role instead of a numeric version. For example, an application can load champion without changing its code whenever the team promotes a new version.
Log a model with a signature
A model signature describes the expected inputs and outputs. It can record column names, data types, and the shape of the prediction result. A signature helps catch requests that do not match the data used during training.
The following script trains a model, infers a signature, and registers the resulting model. The tracking server must be reachable at the configured URI.
import mlflow
from mlflow import MlflowClient
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-registry")
X, y = load_iris(return_X_y=True, as_frame=True)
model = LogisticRegression(max_iter=200).fit(X, y)
signature = infer_signature(X, model.predict(X))
with mlflow.start_run(run_name="iris-logreg") as run:
model_info = mlflow.sklearn.log_model(
model,
name="model",
signature=signature,
registered_model_name="iris-classifier",
)
print(f"Run: {run.info.run_id}")
print(f"Logged model: {model_info.model_uri}")
client = MlflowClient()
versions = client.search_model_versions("name = 'iris-classifier'")
version = max(versions, key=lambda item: int(item.version))
client.set_registered_model_alias(
name="iris-classifier",
alias="champion",
version=version.version,
)
loaded = mlflow.pyfunc.load_model("models:/iris-classifier@champion")
print(loaded.predict(X.head(3)))registered_model_name asks MLflow to create the registered model if it does not exist. If it already exists, MLflow adds another version. The MlflowClient provides registry operations such as assigning aliases.
The code chooses the highest version returned by the search. In a real promotion workflow, choose a version after evaluating it, rather than automatically treating the newest version as the best one.
Load by alias
An alias is a mutable pointer. In this example, champion points to one version at a time:
client.set_registered_model_alias(
name="iris-classifier",
alias="champion",
version="2",
)A service can keep using this URI:
model = mlflow.pyfunc.load_model("models:/iris-classifier@champion")An operator can move the alias to version 3 without changing the service code. This separates deployment configuration from application code.
Aliases replace older stage-based workflows such as Production and Staging in newer MLflow practice. Names such as candidate, champion, or rollback can express a team's process more clearly. An alias does not itself prove that a model is safe. Your promotion process must still check metrics, data compatibility, and operational behavior.
Common registry failures
- The client uses a different tracking URI from the one where the model was registered.
- The registry backend is unavailable or does not support the configured setup.
- A model is registered without a signature, so input mistakes are harder to detect.
- A deployment depends on a numeric version and requires a code change for every promotion.
- The alias is changed without recording who changed it, why, and which evaluation supported the change.
Store evaluation metrics, data version identifiers, source revision, and promotion notes with the run. Keep aliases few and meaningful. Use a separate alias for rollback only when the team has a tested rollback procedure.
Exercise
Why should a serving application load models:/iris-classifier@champion instead of a fixed URI for version 2?
Show answer
The alias lets operators promote or roll back a model by changing the registry pointer, while the application keeps the same loading code.