Sub topic 5
Working with versions and aliases
This chapter assumes you can log a model with MLflow and register it under a three-level name such as main.default.iris_classifier. It also assumes you have a Databricks notebook where mlflow.set_registry_uri("databricks-uc") works. You will see how registered models change over time, how to refer to one version without hard-coding its number, and where this stops being automatic once a serving endpoint is involved.
Versions are immutable and numbered
A registered model in Unity Catalog is a container. Each time you call mlflow.sklearn.log_model(..., registered_model_name="main.default.iris_classifier"), Unity Catalog adds a new model version to that container. The first call creates version 1, the next creates version 2, and so on. The numbers only go up.
A version is a fixed snapshot. Its artifacts, signature, and link to the run that produced it do not change. You can change the metadata around it: the description, tags, and aliases. If you retrain, you get a new version. You do not overwrite an old one.
This gives you two guarantees:
- Version 2 today is the same model as version 2 next month.
- Any version can be traced back to the MLflow run, code, and data that produced it.
You can list versions with the client:
import mlflow
from mlflow import MlflowClient
mlflow.set_registry_uri("databricks-uc")
client = MlflowClient()
name = "main.default.iris_classifier"
for mv in client.search_model_versions(f"name='{name}'"):
print(mv.version, mv.status, mv.aliases, mv.run_id)Unity Catalog requires the name='...' filter on this call. You cannot search versions across all models at once.
Why hard-coded version numbers cause trouble
You can load a specific version with a URI like this:
model = mlflow.pyfunc.load_model("models:/main.default.iris_classifier/2")This is exact and reproducible, which is useful for audits and for debugging. The cost shows up in code that runs regularly, such as a batch scoring job or a notebook shared with a team. When you promote version 3, every consumer that says /2 needs an edit. Some will be missed.
An alias solves this by adding one level of indirection.
Aliases are movable names for versions
An alias is a named pointer from a registered model to one of its versions. The alias is mutable, so you can repoint it at any time. The version it points to stays immutable.
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 @ in the URI marks an alias. A / followed by a number marks a version. Consumers that use the alias URI pick up the new target the next time they load the model.
The rules for aliases are:
- Each alias points to exactly one version of a given model.
- A version can carry several aliases, for example
championandapproved. - Calling
set_registered_model_aliaswith an alias that already exists moves it. It does not raise an error. - Alias names are case-sensitive and limited in length and characters. Names that look like a version reference, such as
v1, are rejected. - A model can have a limited number of aliases (10 at the time of writing). Check the current Databricks limits if you plan many.
The diagram shows a typical state with three versions and two aliases.
Aliases replace stages
If you used the older workspace model registry, you may remember stages such as Staging and Production. Unity Catalog does not have stages. Aliases cover the same need and are more flexible:
| Feature | Workspace registry stages | Unity Catalog aliases |
|---|---|---|
| Names | Fixed: None, Staging, Production, Archived | Any name you choose, within the naming rules |
| Versions per name | Several versions could share one stage | One version per alias |
| Versions per label | One stage per version | Several aliases per version |
| Reference in a URI | `models:/name/Production` | `models:/name@alias` |
Pick alias names that describe a role in your process. champion and challenger are common. staging, production, and approved also work, as long as your team uses them consistently.
A promotion workflow
A typical flow trains a new version, gives it a challenger alias, validates it, and then moves champion. Here is the whole sequence as code:
import mlflow
from mlflow import MlflowClient
mlflow.set_registry_uri("databricks-uc")
client = MlflowClient()
name = "main.default.iris_classifier"
# 1. Mark the newest version as the candidate
latest = max(
client.search_model_versions(f"name='{name}'"),
key=lambda mv: int(mv.version),
)
client.set_registered_model_alias(name, "challenger", int(latest.version))
# 2. Validate by loading through the alias
candidate = mlflow.pyfunc.load_model(f"models:/{name}@challenger")
# ... run your evaluation against candidate here ...
# 3. Remember what champion pointed to, so you can roll back
previous = client.get_model_version_by_alias(name, "champion")
# 4. Promote: move the champion alias
client.set_registered_model_alias(name, "champion", int(latest.version))
# 5. Clean up the temporary alias
client.delete_registered_model_alias(name, "challenger")
print(f"champion moved from {previous.version} to {latest.version}")Step 3 matters. If the new model misbehaves in production, you roll back by calling set_registered_model_alias(name, "champion", int(previous.version)). Nothing is rebuilt or retrained. Only the pointer moves.
get_model_version_by_alias returns a ModelVersion object. Use its version field to find the number behind an alias. It raises an error if the alias does not exist, so wrap the call if a first-time run is possible.
Tags and descriptions for context
Aliases say which version plays a role. They do not say why. Use version tags and descriptions for that:
client.set_model_version_tag(name, "3", "validation_status", "passed")
client.set_model_version_tag(name, "3", "eval_accuracy", "0.97")
client.update_model_version(
name, "3", description="Retrained with 50 trees on the cleaned dataset."
)Tags are key-value strings attached to a version. A common pattern is to have a validation job write a tag, and a promotion job check that tag before it moves champion. Treat tags as a record of what happened, and use privileges (next chapters) to control who is allowed to move an alias.
Gotchas
Aliases are not a security boundary by themselves
Anyone who can manage the model can repoint an alias. Moving champion changes what every @champion consumer loads. Restrict who holds the privileges needed to do that.
Cached loads do not update
mlflow.pyfunc.load_model resolves the alias at load time. A long-running process that loaded @champion yesterday keeps using the old model until it loads again. Design batch jobs to load at the start of each run. Design long-lived services to reload on a schedule or on restart.
Serving endpoints pin a version number
This is the most important gotcha for the rest of the tutorial. When you create a serving endpoint, you give it an entity_version, as in entity_version="1". The endpoint serves that exact version. Moving the champion alias does not change what the endpoint serves. You still have to update the endpoint configuration to the new version.
A common pattern is to resolve the alias to a number in your deployment script and pass that number to the endpoint. The deployment script then reads the alias and the endpoint serves the version it found:
version = client.get_model_version_by_alias(name, "champion").version
print(f"Deploy version {version}")You will use this in the chapter on updating an endpoint.
Deleting versions is hard to undo
Deleting a version removes it from the registered model. Do not delete a version that an endpoint or a rollback plan still needs. Keep old versions until you are sure nothing depends on them.
Summary of the model
- A registered model holds numbered, immutable versions.
- Aliases are named, movable pointers, with one target version each.
- Load by alias for "current approved", and by number for "exactly this".
- Record the previous target before you move an alias so rollback is one call.
- Serving endpoints take a version number, so a promotion needs an explicit endpoint update.
Exercise
A scoring job loads models:/main.default.iris_classifier@champion once at startup and runs for several days. You move champion from version 2 to version 3 on day one. Which version does the job use on day three, and what should you change so it picks up the new version without a manual restart?
Show answer
It still uses version 2, because the alias is resolved once, when load_model runs. To pick up version 3, make the job call load_model again, for example at the start of each run or on a timer. Alternatively, restart the job after each promotion.
Exercise
You run client.set_registered_model_alias("main.default.iris_classifier", "champion", 3) while champion already points to version 2. Does the call fail, and what happens to version 2?
Show answer
The call does not fail. The alias is moved, so champion now points to version 3 only. Version 2 is unchanged and still exists, but it no longer carries the champion alias. You can roll back by pointing the alias at 2 again.
Exercise
A serving endpoint was created with entity_version="2". You then move the champion alias to version 3. Does the endpoint start serving version 3? Explain why, and say how you would find the version number to deploy.
Show answer
No. The endpoint is configured with an explicit version number, so it keeps serving version 2 until you update its configuration. To find the number, call client.get_model_version_by_alias(name, "champion").version and pass that value as the new entity_version when you update the endpoint.