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 under a three-level name (catalog.schema.model), as covered in the previous chapter. It also assumes you have a notebook attached to compute.
Why aliases
Each time you register a model under the same name, Unity Catalog creates a new numbered version. Versions are immutable. Downstream code still needs a stable way to say "the model we currently trust". An alias is a mutable, named pointer to one version of a registered model. Typical names are champion and challenger.
Unity Catalog models use aliases instead of the older fixed stages (Staging, Production). Each alias points to exactly one version. A version can have several aliases. Moving an alias to a new version changes which model consumers load, and consumer code does not change.
Registering and assigning an alias
The code below registers the run's model, then points champion at the new version. It assumes run is the run from the model-logging chapter.
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)set_registered_model_alias takes the model name, the alias and the version number. If champion already points to another version, the call moves it. No other version is touched.
Loading by alias
Load the model with a models:/ URI that uses @ followed by the alias:
loaded = mlflow.pyfunc.load_model(f"models:/{model_name}@champion")
print(loaded.predict(X_test[:3]))mlflow.pyfunc.load_model returns a generic Python model wrapper. It exposes predict no matter which library trained the model. The alias is resolved when you call load_model. A process that has already loaded the model keeps the version it got, so it must reload to see a promotion.
You can also load a fixed version with models:/main.default.iris_classifier/1. Use that when you need an exact, repeatable result, such as in a test. Use the alias when you want the current approved model.
Inspecting and managing aliases
mv = client.get_model_version_by_alias(model_name, "champion")
print(mv.version, mv.run_id)
client.delete_registered_model_alias(model_name, "challenger")get_model_version_by_alias returns the version the alias points to, including the run_id that produced it. That link lets you trace a deployed model back to its parameters, metrics and artifacts.
Common errors
- Wrong registry URI. Without
set_registry_uri("databricks-uc"), MLflow may target the legacy workspace registry. Three-level names and aliases then fail or behave differently. - Two-level names. Unity Catalog needs
catalog.schema.model. A name likeiris_classifieris rejected. - Missing privileges. Registering needs
CREATE MODELon the schema. Loading needsEXECUTEon the model. A permission error usually means one of these is absent. - Alias not found. Aliases are case-sensitive, and they must exist before you load. Check the spelling and confirm with
get_model_version_by_alias. - Stale loaded model. A long-running job that loaded
@championearlier does not follow later moves of the alias.
Exercise
Exercise
Your serving job loads models:/main.default.iris_classifier@champion once at startup. You then run set_registered_model_alias to move champion from version 3 to version 4. Which version does the running job use, and what must you do for it to use version 4?
Show answer
The running job keeps version 3, because the alias was resolved when load_model was called. Restart the job or have it call load_model again so the alias resolves to version 4.