Sub topic 5
Working with versions and aliases
This chapter assumes you can log and register a model with MLflow, as shown in the previous chapters. It also assumes you know the three-level name catalog.schema.model. Here you will see how a registered model changes over time and how to point a stable name at the version you want.
Versions are immutable
Each time you register a model under an existing name, Unity Catalog creates a new version. Versions are numbered 1, 2, 3 and so on. A version is a frozen snapshot of the logged model artifacts, its signature, and a link to the run that produced it. You cannot edit a version's artifacts. To change the model, you register a new version.
Unity Catalog does not support the old workspace registry stages such as Staging and Production. Aliases replace them.
What an alias is
An alias is a mutable, named pointer to one version of one registered model. These rules apply:
- A version can have several aliases, for example
championandapproved. - An alias points to only one version of a given model. Setting it again moves it.
- Aliases belong to a single model.
championon one model is unrelated tochampionon another. - An alias cannot use the form
v<number>, such asv1, because that would look like a version.
Setting and using an alias
Set the registry to Unity Catalog, then create the client. Point the alias at a version, and load by alias.
import mlflow
from mlflow import MlflowClient
mlflow.set_registry_uri("databricks-uc")
client = MlflowClient()
# Point the 'champion' alias at version 1
client.set_registered_model_alias("main.default.iris_classifier", "champion", 1)
# Load whichever version the alias currently points to
loaded = mlflow.pyfunc.load_model("models:/main.default.iris_classifier@champion")The loading URI has the form models:/<catalog>.<schema>.<model>@<alias>. Code that loads by alias does not change when you promote a new version. You move the alias, and the next load picks up the new version.
Inspecting versions and aliases
Often you need to know what an alias currently points to, or what versions exist.
import mlflow
from mlflow import MlflowClient
mlflow.set_registry_uri("databricks-uc")
client = MlflowClient()
name = "main.default.iris_classifier"
mv = client.get_model_version_by_alias(name, "champion")
print("champion is version", mv.version)
for v in client.search_model_versions(f"name='{name}'"):
print(v.version, v.aliases)
# Remove an alias without touching the version
client.delete_registered_model_alias(name, "challenger")Deleting an alias never deletes the version it pointed to.
Promoting a new version
A typical promotion flow looks like this:
- Register the new model, which creates the next version number.
- Point a
challengeralias at it and run your validation against that alias. - If it passes, move
championto the new version withset_registered_model_alias. - Keep the old version. If something goes wrong, move
championback.
Moving an alias is a metadata change. It is fast and needs no retraining.
Common errors
| Symptom | Likely cause |
|---|---|
| `RESOURCE_DOES_NOT_EXIST` when loading by alias | The alias was never set on this model, was deleted, or the name is misspelled. |
| Load works in one notebook but gives an old model in another | The code loaded by a fixed version number, or an earlier loaded object is still in memory. |
| Permission denied when setting an alias | Only the model owner, or a principal with sufficient privileges on it, can modify it. Privileges are covered in the next chapter. |
| Load fails with a registry error | `mlflow.set_registry_uri("databricks-uc")` was not called, so MLflow looked in the workspace registry. |
Exercise
Your inference job loads models:/main.default.iris_classifier@champion. You register version 4 and want the job to use it on its next run without editing the job's code. What do you do, and what happens to version 3?
Show answer
Call client.set_registered_model_alias("main.default.iris_classifier", "champion", 4). The alias moves to version 4, so the job's next load returns version 4. Version 3 still exists, unchanged, and loses only the champion alias. You can move the alias back to 3 to roll back.