Sub topic 12
Why stages are deprecated and how to migrate
This chapter assumes you have read the earlier chapters on registered models, model versions, aliases and promotion with MlflowClient. It also assumes you can read a short Python script and run it against a tracking server. You will see what registry stages were, why MLflow replaced them with aliases, and how to migrate existing stage-based code and data.
What stages were
Older MLflow code organized a model's lifecycle around stages. Every model version had exactly one current_stage value from a fixed list:
None: the default for a new version.Staging: a candidate under test.Production: the version serving live traffic.Archived: a retired version.
You promoted a version by calling a transition function, and you loaded it with a stage name in the URI:
import mlflow
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
# Deprecated: move version 2 into the Production stage.
client.transition_model_version_stage(
name="iris-classifier",
version="2",
stage="Production",
archive_existing_versions=True,
)
# Deprecated: load whatever version is in Production.
model = mlflow.pyfunc.load_model("models:/iris-classifier/Production")This code still appears in older tutorials and codebases. MLflow has marked the stage APIs as deprecated since release 2.9 and plans to remove them in a future major release. Check the release notes of the version you run to see how far removal has progressed.
Why stages were a poor fit
Stages work for a simple pipeline with one environment and one model in production. They break down in several common situations.
- The list of names is fixed. You cannot create
shadow,canary, orchallenger. Teams ended up overloadingStagingto mean several different things. - Stages mix two ideas. A stage describes where a version is in its lifecycle, and it also acts as the pointer that serving code reads. Moving the pointer and changing the lifecycle label were one operation, so you could not do one without the other.
- Stages assume one environment. Real teams often have dev, staging, and prod environments, regions, or customer-specific deployments. One stage field per version cannot represent that.
- Rules about multiple versions are implicit. Without
archive_existing_versions=True, several versions could sit inProductionat once, andmodels:/name/Productionresolved to the latest of them. That hidden rule caused surprises. The flag, when set, silently moved other versions toArchived. - Loading code is tied to lifecycle semantics. If you wanted to test a candidate with the same serving code, you had to put it in
Productionor change the code.
Aliases separate the pointer from the lifecycle label. An alias is a named, mutable reference from a registered model to exactly one version. A version can have several aliases, and you choose the names. Lifecycle facts such as "validated" or "retired" belong in tags, which the earlier chapter on descriptions and tags covered.
Stages and aliases compared
| Aspect | Stages (deprecated) | Aliases |
|---|---|---|
| Names | Fixed: None, Staging, Production, Archived | Any name you pick, such as `champion` or `challenger` |
| Versions per name | Several versions could share a stage | One version per alias per registered model |
| Names per version | One stage | Zero, one, or many aliases |
| Load URI | `models:/name/Production` | `models:/name@champion` |
| Promote | `transition_model_version_stage` | `set_registered_model_alias` |
| Roll back | Transition the older version back and archive the newer one | Repoint the alias at the older version |
| Retire a version | Move it to `Archived` | Remove its aliases and set a tag |
Two details about aliases are worth knowing before you migrate. Alias names are case-sensitive. The names latest and any name of the form v followed by digits, such as v2, are reserved, because models:/name/latest and version-number shortcuts already use those forms. Choose names like champion, challenger, or production-eu.
Choosing alias names for your old stages
You do not have to keep the old names. Pick names that describe the role of the version and keep them stable, because loading code depends on them.
| Old stage | Suggested replacement | Notes |
|---|---|---|
| `Production` | alias `champion` (or `production`) | Serving code loads this alias. |
| `Staging` | alias `challenger` (or `staging`) | A candidate being validated. |
| `Archived` | no alias, plus tag `lifecycle=archived` | Aliases point at live versions, so archived versions get none. |
| `None` | no alias | A version with no alias is simply unpromoted. |
If you run several environments in one registry, you can use an alias per environment, such as prod-eu and prod-us. If you keep separate registered models for each environment, use the copy approach from the previous chapter and keep the same alias name in each.
A migration plan
Migration has three parts that you should do in this order, so serving never breaks:
- Create aliases that mirror the current stages. Stage-based code keeps working at this point.
- Change loading and promotion code to use aliases, and deploy it.
- Remove the remaining stage calls and stop reading
current_stage.
The block above is only a placeholder for ordering. The diagram below shows the same plan.
Step 0: find stage usage
Search your repositories and pipeline definitions before you change anything.
grep -rnE "transition_model_version_stage|get_latest_versions|models:/[^\"' ]+/(Production|Staging|Archived|None)" .Note that get_latest_versions is also tied to stages and is deprecated. Replace it with search_model_versions or an alias lookup.
Step 1: create aliases from existing stages
The script below reads every registered model and sets an alias for the version in each mapped stage. It also tags archived versions so you keep the information that stages used to carry.
from mlflow import MlflowClient
STAGE_TO_ALIAS = {"Production": "champion", "Staging": "challenger"}
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
def all_registered_models(client):
token = None
while True:
page = client.search_registered_models(page_token=token)
yield from page
token = page.token
if not token:
break
for rm in all_registered_models(client):
versions = client.search_model_versions(f"name = '{rm.name}'")
for stage, alias in STAGE_TO_ALIAS.items():
in_stage = [int(v.version) for v in versions if v.current_stage == stage]
if not in_stage:
continue
target = max(in_stage)
if len(in_stage) > 1:
print(f"{rm.name}: {len(in_stage)} versions in {stage}, using {target}")
client.set_registered_model_alias(rm.name, alias, str(target))
print(f"{rm.name}: alias {alias} -> version {target}")
for v in versions:
if v.current_stage == "Archived":
client.set_model_version_tag(rm.name, v.version, "lifecycle", "archived")The script handles one case that stages allowed and aliases do not. When several versions share a stage, an alias can point to only one of them, so the script picks the highest version number and prints a warning. Review those warnings. The highest number matches what models:/name/Production resolved to in older releases, but confirm that it is the version you actually serve.
Run the script twice. The second run should change nothing, because setting an alias to the version it already points at has no effect. This makes it safe to re-run during a gradual rollout.
Step 2: switch loading and promotion code
Replace stage URIs with alias URIs and stage transitions with alias updates:
import mlflow
from mlflow import MlflowClient
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = MlflowClient()
# Loading: was models:/iris-classifier/Production
model = mlflow.pyfunc.load_model("models:/iris-classifier@champion")
# Promotion: was transition_model_version_stage(..., stage="Production")
client.set_registered_model_alias("iris-classifier", "champion", "3")Because both styles resolve to the same version after step 1, you can deploy this change service by service. Rolling back is also easier than before: repoint champion to the earlier version, as shown in the rollback chapter.
Step 3: clean up
When no code calls the stage functions, remove them. Also stop using current_stage in dashboards and reports. Use get_model_version_by_alias to ask which version is live, and use tags for lifecycle facts.
Gotchas
- Aliases do not archive anything. With stages,
archive_existing_versions=Truemoved the old version toArchivedfor you. With aliases, movingchampionleaves the old version untouched with no alias. If you want a record, set a tag when you promote. - Alias moves are immediate and global. Any process that resolves
@championat load time gets the new version on its next load. A running process keeps the model it already loaded until it reloads. - Pin versions in reproducible jobs. Use
models:/name/<version>for batch jobs that must produce repeatable results, and use aliases for services that should follow promotions. - Mixed code can disagree. If part of your system still sets stages while another part sets aliases, the two can point at different versions. Finish the migration for each registered model in one step.
- Permissions apply to the new calls. If your server uses access control, give promoters permission to edit the registered model, because alias changes are edits to the registered model.
Exercise
A registered model has versions 1, 2, and 4 in the Production stage, version 5 in Staging, and version 3 in Archived. You run the migration script from this chapter with Production mapped to champion and Staging mapped to challenger. Which alias and tag results does it produce, and what should you check afterwards?
Show answer
It sets champion to version 4, the highest version in Production, and prints a warning that three versions shared that stage. It sets challenger to version 5. It sets the tag lifecycle=archived on version 3. Versions 1 and 2 get no alias. Afterwards, confirm that version 4 is the version your serving code actually used under models:/iris-classifier/Production. If a different version was serving, repoint champion before you switch the loading code.
Exercise
Why is models:/name@champion safer to promote with than models:/name/Production, and when should you still load by a version number?
Show answer
An alias refers to exactly one version of a registered model, so there is no ambiguity when several versions exist, and you can name the role freely. Moving the alias changes what loads without changing code, and moving it back is a rollback. Load by version number (models:/name/1) in jobs that must be reproducible, because a version number never changes.