Sub topic 12
Assigning Aliases and Loading a Registered Model
This chapter assumes you can log a model to a run and register it in Unity Catalog, as covered in the previous chapter. You should also be comfortable with the MlflowClient class and with models:/ and runs:/ URIs. Here you will learn how aliases give versions stable names, how to load a model by version or alias, and which errors to expect.
Why versions alone are not enough
Every call to mlflow.register_model with the same model name creates a new, immutable version: 1, 2, 3, and so on. Version numbers are precise, but they are a poor thing to hard-code in a consumer. If a batch scoring job loads models:/main.default.iris_classifier/3, someone must edit that job each time a better version appears.
An alias solves this. An alias is a named, mutable pointer from a label to one version of a registered model. Consumers load the label. The team that owns the model moves the label when a new version is approved.
Unity Catalog aliases have these properties:
- An alias points to exactly one version of a given model.
- A version can have several aliases.
- Reassigning an alias moves it. The old version loses the alias and keeps its other aliases.
- Alias names are case-insensitive for lookup in most clients, but pick one spelling and keep it. A common convention is lowercase names such as
championandchallenger. - Aliases are scoped to one registered model.
championoniris_classifieris unrelated tochampionon another model.
The older workspace model registry used fixed stages (Staging, Production, Archived). Unity Catalog models do not have stages. Aliases replace them and you define the vocabulary yourself.
Assigning an alias
Point MLflow at Unity Catalog before you register or load anything. Then use MlflowClient.
import mlflow
from mlflow import MlflowClient
mlflow.set_registry_uri("databricks-uc")
model_name = "main.default.iris_classifier"
version = mlflow.register_model(f"runs:/{run.info.run_id}/model", model_name)
client = MlflowClient()
client.set_registered_model_alias(model_name, "champion", version.version)
loaded = mlflow.pyfunc.load_model(f"models:/{model_name}@champion")
print(loaded.predict(X_test[:3]))This snippet reuses run from the explicit model-logging example in the model logging chapter, and X_test from the training code. Run those cells first, or the names will be undefined.
Three details are worth knowing:
register_modelreturns aModelVersionobject. Itsversionattribute is a string such as"1".set_registered_model_aliasaccepts it as is.register_modelcan take a few seconds because it copies the model files into Unity Catalog storage. The version may briefly be in a pending state. Aliasing a version that is not ready can fail or lead to a load error. If you script this, wait until the version status is ready.- Calling
set_registered_model_aliasagain with the same alias and a different version moves the alias. There is no separate "update" call.
A typical promotion flow
A common pattern uses two aliases:
challengermarks a freshly registered version under evaluation.championmarks the version that production consumers use.
from mlflow import MlflowClient
client = MlflowClient()
model_name = "main.default.iris_classifier"
# New version just registered and validated offline.
new_version = "2"
client.set_registered_model_alias(model_name, "challenger", new_version)
# After checks pass, promote it.
client.set_registered_model_alias(model_name, "champion", new_version)
# Optionally remove the label that is no longer needed.
client.delete_registered_model_alias(model_name, "challenger")Promotion is one call. Rollback is also one call: set champion back to the earlier version. Nothing is copied, and the versions themselves never change.
Looking up aliases and versions
You can resolve an alias without loading the model. This is useful for logging which version a job used.
mv = client.get_model_version_by_alias(model_name, "champion")
print(mv.version, mv.run_id, mv.status)
model = client.get_registered_model(model_name)
print(model.aliases)get_model_version_by_alias raises an MlflowException if the alias does not exist. get_registered_model(...).aliases returns a mapping from alias name to version number.
Recording mv.version next to your predictions is good practice. An alias can move between the time a job starts and the time you audit its output, so the alias alone does not tell you which model produced a result.
Loading a registered model
The models:/ URI accepts a version or an alias.
| URI | Resolves to | Use when |
|---|---|---|
| `models:/main.default.iris_classifier/2` | Version 2, always | You need an exact, repeatable result, such as an audit or a test |
| `models:/main.default.iris_classifier@champion` | Whatever `champion` points to when you load | A consumer should follow promotions automatically |
| `runs:/<run_id>/model` | The logged model in a run, not a registered one | You are still experimenting and have not registered anything |
Note the separator. Versions use a slash (/2). Aliases use an at sign (@champion). Writing models:/name/champion makes MLflow look for a version called "champion", which does not exist.
Choosing a flavor to load with
mlflow.pyfunc.load_model returns a generic wrapper with a predict method. It works for every logged model and is the best default for serving code, because the caller does not need to know which library trained the model.
If you need native behavior, such as predict_proba on a scikit-learn model, load with the flavor loader:
import mlflow
mlflow.set_registry_uri("databricks-uc")
native = mlflow.sklearn.load_model("models:/main.default.iris_classifier@champion")
print(native.predict_proba(X_test[:3]))The pyfunc wrapper exposes only predict. Use the flavor loader when you need other methods, and pyfunc when you want one uniform interface.
What the signature does at load time
If the model was logged with a signature, as in the model logging chapter, pyfunc validates input against it when you call predict. A DataFrame with a missing column or an incompatible type raises an error instead of returning silent garbage. You can inspect the expected schema before calling:
loaded = mlflow.pyfunc.load_model("models:/main.default.iris_classifier@champion")
print(loaded.metadata.signature)
print(loaded.metadata.run_id)metadata.run_id links the loaded model back to the training run, so you can find its parameters, metrics and artifacts in the experiment UI.
Resolving the alias once
An alias is resolved at load time, not at predict time. A process that loads @champion at startup keeps using that version until it reloads, even if the alias moves afterward. This has two consequences:
- A long-running service needs a reload policy, such as restarting on a schedule or polling
get_model_version_by_aliasand reloading when the version changes. - A batch job that loads once at the top and scores many partitions is internally consistent. Do not reload the alias partway through, or different partitions could be scored by different versions.
For jobs that must be reproducible, resolve the alias to a version first and load by version:
mv = client.get_model_version_by_alias(model_name, "champion")
model = mlflow.pyfunc.load_model(f"models:/{model_name}/{mv.version}")
print(f"scored with version {mv.version}")Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Model not found when loading `@champion` | The alias was never set, or was set on a different model name | Call `get_registered_model` and check `aliases` |
| Error that a version named `champion` does not exist | Wrote `/champion` instead of `@champion` | Use `@` for aliases |
| Registry URI error, or the model is looked up in the workspace registry | `set_registry_uri("databricks-uc")` was not called in this session | Set the registry URI before registering or loading |
| Permission denied | Missing `EXECUTE` on the model, or missing `USE` on the catalog or schema | Ask the model owner for grants |
| Input schema error at `predict` | The input does not match the logged signature | Compare `loaded.metadata.signature` with your DataFrame columns and types |
| Unexpected version used after a promotion | The consumer loaded the alias before the move and never reloaded | Reload, or restart the process |
Exercises
Exercise
A scoring job loads models:/main.default.iris_classifier@champion at 09:00 and runs until 11:00. At 10:00 the owner moves champion from version 3 to version 4. Which version scores the data between 10:00 and 11:00, and how would you change the job so its output records which version it used?
Show answer
Version 3 keeps scoring, because the alias is resolved once at load time. To record the version, call client.get_model_version_by_alias(model_name, "champion") at startup, store mv.version, load the model by models:/{model_name}/{mv.version}, and write the version number alongside the predictions.
Exercise
Write the single call that rolls champion back to version 2, and say what happens to version 4, which champion pointed to before.
Show answer
client.set_registered_model_alias("main.default.iris_classifier", "champion", 2). Version 4 is not changed or deleted. It only loses the champion alias, and keeps any other aliases it has.