Sub topic 8
Promoting a version to champion
This chapter covers how to move a validated model version into production use by pointing an alias at it. It assumes you can register a model version and set tags on it, and that you know an alias is a named pointer to one version. It also assumes you have a tracking server running with a database backend, as in the earlier chapters. The examples use the iris-classifier registered model, with versions 1 and 2 already registered.
What promotion means in the registry
Promotion changes which model version your serving code receives. It does not copy or retrain anything. The model files stay where they were logged, and the version number stays the same. Promotion is one metadata write: the alias champion now points at version 2 instead of version 1.
That has three consequences:
- Promotion is cheap and fast. It is a single call to the tracking server.
- Promotion is reversible. The previous version still exists, so you can point the alias back.
- Consumers that load
models:/iris-classifier@championpick up the change the next time they load the model. They do not need a code change or a redeploy of the code itself.
The alias name champion is a convention, not a built-in. MLflow does not treat it specially. Teams often add challenger for a candidate under evaluation, but any valid name works.
The promotion flow
A safe promotion has four steps: register, validate, record the result, then move the alias. The tag from the validation step acts as the evidence that the version may be promoted.
The decision happens before the alias moves. If the gate fails, nothing changes for consumers.
Moving the alias
The core call is set_registered_model_alias. It takes the registered model name, the alias, and the version as a string.
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)Reading the alias back is a cheap check that the write landed where you expected. If the alias already pointed at another version, the call moves it. You do not delete the old alias first.
The rules that govern aliases matter when you promote:
- An alias points to exactly one version of a registered model at a time.
- One version can carry several aliases, for example
championandshadow-eval. - Each registered model has its own aliases.
championoniris-classifierandchampiononfraud-detectorare unrelated. - Alias names cannot be
latest, and they cannot look like a version reference such asv1. MLflow rejects those names so that alias URIs stay unambiguous.
Gating the promotion with tags
A bare alias move trusts the caller. A small wrapper can check the version's tags first and refuse to promote anything that has not passed validation. It also records what the alias pointed at before the move, which you will need for rollback.
from mlflow import MlflowClient
from mlflow.exceptions import MlflowException
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
def promote(name, version, alias="champion"):
version = str(version)
mv = client.get_model_version(name, version)
status = mv.tags.get("validation_status")
if status != "passed":
raise ValueError(
f"{name} v{version} has validation_status={status!r}; refusing to promote"
)
try:
previous = client.get_model_version_by_alias(name, alias).version
except MlflowException:
previous = None # alias has never been set
client.set_registered_model_alias(name, alias, version)
if previous is not None:
client.set_model_version_tag(name, version, "previous_champion", previous)
return previous
previous = promote("iris-classifier", 2)
print("previous champion:", previous)Some details in this function are worth noting:
get_model_versionreturns aModelVersionwhosetagsfield is a plain dictionary of strings. A missing tag givesNone, and the gate treats that the same as a failure.get_model_version_by_aliasraisesMlflowExceptionwhen the alias does not exist. The first promotion of a new registered model hits this path, so thetryblock is needed.- The
previous_championtag is written to the new version after the alias moves. If you also keep your own deployment log, you can drop the tag. Without some record of the old version, rollback depends on someone remembering it.
Aliases and tags do different jobs
People often try to use one mechanism for both purposes. They solve different problems.
| Property | Alias | Version tag |
|---|---|---|
| Meaning | Which version consumers should load | A fact about one version |
| Cardinality | One version per alias name | Any number of tags per version |
| Changes by | Being moved | Being set, overwritten, or deleted |
| Used by loaders | Yes, through `models:/name@alias` | No |
| Example | `champion` | `validation_status=passed` |
Use tags for facts that stay true of a version, such as test accuracy, the dataset hash, or who approved it. Use aliases for decisions that change over time, such as which version is live.
Common errors
Most promotion problems come from a handful of mistakes.
- Passing an integer as the version. The client expects a string, so use
"2". Some client versions accept an integer, but a string is always safe. - Promoting the wrong registered model. Aliases are per model. Promoting
iris-classifier-stagingdoes nothing foriris-classifier. - Assuming the newest version is the champion. A new registration does not move any alias. After you register version 3,
championstill points at whatever it pointed at before. - Setting the alias to a version that does not exist. The call fails with an
MlflowException. Register the version first. - Using a reserved or malformed alias name.
latestand names such asv2are rejected. - Expecting running processes to update. A process that already called
load_modelholds the old model in memory. The alias move takes effect on the next load, so long-lived servers need a reload or restart policy. Chapter material on loading covers the options. - Skipping the readback. A typo in the model name creates no new alias on the intended model. Confirm with
get_model_version_by_alias.
Choosing alias names
Keep the set of aliases small and documented. A typical minimal scheme looks like this:
champion: the version serving production traffic.challenger: a candidate being compared against the champion.
When a challenger wins, you do not rename anything. You move champion to the challenger's version. You can then remove challenger or point it at the next candidate. Moving aliases rather than renaming them keeps the consumer URIs stable.
Checking your work
After a promotion, verify from the consumer's point of view, not only from the client that made the change. Load the model through the alias and confirm the version.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
model = mlflow.pyfunc.load_model("models:/iris-classifier@champion")
print(model.metadata.run_id)The run_id here is the run that produced the version the alias resolved to. Compare it with the run you intended to promote. If they differ, the alias is pointing somewhere else, and you should fix it before declaring the promotion done.
Exercise
You register version 3 of iris-classifier. The champion alias is on version 2. A teammate says "version 3 is live now, since it is the newest." Is that correct, and what single call would make it true?
Show answer
It is not correct. Registering a version does not move any alias, so champion still points at version 2 and consumers loading models:/iris-classifier@champion still get version 2. Calling client.set_registered_model_alias("iris-classifier", "champion", "3") would make version 3 live, ideally after checking that its validation_status tag is passed.
Exercise
In the promote function, why is the previous alias target read before set_registered_model_alias is called, and what happens on the very first promotion of a new registered model?
Show answer
After the alias moves, the old target can no longer be read from the alias, so it must be captured first if you want to record or restore it. On the first promotion the alias does not exist, so get_model_version_by_alias raises MlflowException. The function catches it and sets previous to None, and no previous_champion tag is written.