Sub topic 7
Aliases: named pointers to versions
This chapter covers what an alias is, how to set and read one, and where aliases differ from tags and version numbers. It assumes you can run a tracking server with a database backend and that you can register a model from a run. Both are covered in earlier chapters. The examples use the iris-classifier registered model, which should already have at least two versions.
The problem aliases solve
Every time you register a model, the registry creates a new version with the next integer: 1, 2, 3. Version numbers are stable. Version 2 always refers to the same artifacts. That stability is useful for audit, but serving code needs a different kind of name. It needs one that means "the model we currently trust in production", and that name must keep working after the model changes.
If serving code hard-codes models:/iris-classifier/2, every promotion means editing and redeploying that code. An alias moves that decision into the registry. The code asks for a name, and the registry decides which version the name points to.
What an alias is
An alias is a named, mutable reference from a registered model to one of its versions. Four rules describe how it behaves:
- An alias belongs to a registered model. The alias
championoniris-classifieris unrelated tochampionon any other registered model. - An alias points to exactly one version at a time. Setting it again moves it.
- A version can carry several aliases, for example
championandshadow. - An alias is a pointer, not a copy. Moving it does not duplicate artifacts or change the version it used to point to.
The diagram shows one registered model with three versions and two aliases.
Version 1 has no alias. It still exists and can still be loaded by number. An alias is a convenience for naming a version, and a version doesn't need one to exist.
Setting and reading an alias
Use MlflowClient to manage aliases. The call to set an alias creates it if it is new and moves it if it already exists.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.set_registered_model_alias("iris-classifier", "champion", "2")
mv = client.get_model_version_by_alias("iris-classifier", "champion")
print(mv.version)The version argument is passed as a string, as in the example, although an integer also works in recent releases. get_model_version_by_alias returns a ModelVersion object. That object carries the version number, the source run, the tags, and the list of aliases currently on it. You can read the aliases from the version side as well:
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
mv = client.get_model_version("iris-classifier", "2")
print(mv.aliases) # for example ['champion']The registered model object also carries an aliases mapping from alias name to version number. The UI shows the same information next to each version.
Naming rules
Alias names have a few restrictions. They are limited in length and in allowed characters: letters, digits, underscores, and hyphens. Two kinds of name are reserved. latest is reserved because models:/name/latest has its own meaning. Names of the form v followed by digits, such as v1, are reserved because they look like version references. A call with a reserved or invalid name fails with an error from the server.
Pick names for roles, not for properties of a particular model. champion, challenger, and shadow describe a job. Names such as best or good describe a judgment that may stop being true, and v2-final ties the name to a single version, which defeats the purpose of an alias.
Moving and removing aliases
Setting an alias that already exists moves it. There is no separate "move" call, and the move is a single update, so there is no moment when the alias points nowhere.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.set_registered_model_alias("iris-classifier", "champion", "3")
print(client.get_model_version_by_alias("iris-classifier", "champion").version) # 3
client.delete_registered_model_alias("iris-classifier", "champion")After the delete, get_model_version_by_alias for champion raises an MlflowException stating that the alias does not exist. Deleting an alias does not delete the version it pointed to. Deleting a model version, on the other hand, removes any aliases that pointed to it, so an alias can never point to a version that does not exist.
Aliases versus tags
Both aliases and tags are small labels attached to model versions, and it is easy to mix them up. They answer different questions.
| Property | Alias | Tag |
|---|---|---|
| Attached to | A registered model, pointing at one version | A model version or a registered model |
| Uniqueness | One version per alias name | Any number of versions can share a key and value |
| Usable in a `models:/` URI | Yes, as `models:/name@alias` | No |
| Typical content | A role: `champion`, `challenger` | A fact: `validation_status=passed` |
| Changes | Moves when you promote | Records what was observed |
A good pattern is to record facts as tags and let the alias say which version is live. A version tagged validation_status=passed is a candidate. It becomes live only when something sets the alias on it. This separation matters for governance, because a promotion script can check the tag before moving the alias.
How an alias is resolved
The alias lives in the registry's backend store, in the same database as the registered models. It is not stored inside the model artifacts. The registry resolves it when a client asks, and this has consequences:
- A client that calls
mlflow.pyfunc.load_model("models:/iris-classifier@champion")gets whichever version the alias points to at that moment. - A model object that is already loaded in memory does not change when the alias moves. A long-running service must reload to pick up a promotion. How often to reload, and whether to restart or poll, is a deployment choice covered in the loading chapter.
- If two processes load the alias at slightly different times around a promotion, they can hold different versions for a while. Log the resolved version number at load time so you can tell which one a process is serving.
The loading call looks like this, next to a pinned version for comparison:
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
champion = mlflow.pyfunc.load_model("models:/iris-classifier@champion")
pinned = mlflow.pyfunc.load_model("models:/iris-classifier/1")The first URI follows promotions. The second never changes. Use a pinned URI when you need reproducibility, such as an evaluation that must be rerun against the exact same model.
Common errors
- Setting an alias on a version that does not exist. The call fails. Check the version number with
client.search_model_versions("name='iris-classifier'")first. - Using a stage name as an alias. Old stage names like
Productionstill work as aliases only because they are ordinary strings, but they do not trigger any stage behavior. The stages chapter covers migration. - Expecting the alias to survive a model version deletion. It does not. Re-point the alias before deleting the version it uses.
- Assuming case-insensitive matching.
Championandchampionare different aliases. Standardize on lower case to avoid accidental duplicates. - Forgetting the tracking URI.
MlflowClient()with no URI uses the local default, which may be a./mlrunsfolder, not your server. Set the URI explicitly, as the examples do.
Summary of the working model
An alias is a movable pointer from a name to a version, scoped to one registered model. Version numbers record history, tags record facts, and aliases record which version currently plays a role. Setting an alias both creates and moves it, deleting it leaves the version alone, and the registry resolves it only when a client asks.
Exercise
The registered model iris-classifier has versions 1, 2 and 3. The alias champion points to version 2 and a service loaded models:/iris-classifier@champion an hour ago. You now call set_registered_model_alias("iris-classifier", "champion", "3"). Which version does the already-running service hold, which version does a new call to load_model("models:/iris-classifier@champion") return, and what happens to version 2?
Show answer
The running service still holds version 2, because the loaded model object does not change when the alias moves. A new load call returns version 3, because the alias is resolved at load time. Version 2 is not changed or deleted. It simply has no champion alias any more, and it can still be loaded as models:/iris-classifier/2.
Exercise
A teammate wants to mark every version that passed validation so that a dashboard can list them, and also wants to mark the single version that serving should load. Which label should they use for each purpose, and why?
Show answer
Use a tag such as validation_status=passed for the validation marker, because many versions can share the same tag and a tag records a fact. Use an alias such as champion for the serving version, because an alias points to exactly one version and can be used in a models:/name@alias URI.