Sub topic 11
Promoting across environments by copying versions
This chapter assumes you can run an MLflow tracking server with a SQL backend and register a model from a run. It also assumes you know how aliases point at model versions. If aliases are new to you, review the earlier chapters on aliases and promotion first.
Two ways to model environments
Most teams run a model in more than one environment, such as staging and production. The registry gives you two ways to represent that.
- One registered model, many aliases. A single registered model such as
iris-classifierholds all versions. Aliases likechampionandchallengermark which version plays which role. Promotion is a call toset_registered_model_alias. - One registered model per environment. You keep
iris-classifier-stagingandiris-classifier-prodas separate registered models. Promotion means copying a version from the first into the second.
The alias approach is simpler and is the default recommendation. The copy approach exists for cases where environments need a hard boundary. This chapter covers the copy approach and how to choose between the two.
What copying does
MlflowClient.copy_model_version creates a new model version inside a destination registered model. It reads the source version through a models:/ URI and registers a copy of the model artifacts under the destination name.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.copy_model_version(
src_model_uri="models:/iris-classifier-staging@champion",
dst_name="iris-classifier-prod",
)Several details matter here.
- The source is a URI, not a name and version pair. The URI can use an alias (
@champion) or a version number (/3). An alias is resolved once, at the moment of the copy. - The destination is a registered model name. If it does not exist yet, MLflow creates it.
- The copy gets its own version number in the destination. Version 3 in staging might become version 1 in production. The numbers are independent.
- The model files are physically copied. The new version does not point back at the staging artifacts, so deleting the staging version later does not break production.
- Aliases and tags do not carry over automatically. Set them again on the new version.
The function returns the new ModelVersion. Use its version attribute to set an alias or tag on the copy.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
new_mv = client.copy_model_version(
src_model_uri="models:/iris-classifier-staging@champion",
dst_name="iris-classifier-prod",
)
client.set_model_version_tag(
name="iris-classifier-prod",
version=new_mv.version,
key="promoted_from",
value="iris-classifier-staging@champion",
)
client.set_registered_model_alias("iris-classifier-prod", "champion", new_mv.version)
print(new_mv.name, new_mv.version)The promoted_from tag is a convention, not an MLflow feature. It records where the version came from, which helps when you later need to trace a production model back to its staging history.
A complete promotion script
A promotion script should check the validation result before it copies anything. The staging version carries a validation_status tag, which an earlier step wrote. The script below refuses to promote anything that has not passed.
import mlflow
from mlflow import MlflowClient
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = MlflowClient()
SRC_NAME = "iris-classifier-staging"
DST_NAME = "iris-classifier-prod"
src = client.get_model_version_by_alias(SRC_NAME, "champion")
if src.tags.get("validation_status") != "passed":
raise SystemExit(
f"Version {src.version} of {SRC_NAME} has not passed validation."
)
copied = client.copy_model_version(
src_model_uri=f"models:/{SRC_NAME}/{src.version}",
dst_name=DST_NAME,
)
client.set_model_version_tag(DST_NAME, copied.version, "promoted_from",
f"{SRC_NAME}/{src.version}")
client.set_registered_model_alias(DST_NAME, "champion", copied.version)
print(f"Promoted {SRC_NAME} v{src.version} to {DST_NAME} v{copied.version}")The script copies from models:/{SRC_NAME}/{src.version} rather than from the alias. It has already resolved the alias once to check the tag. Using the fixed version number for the copy means a concurrent change to champion cannot swap in a different version between the check and the copy.
Loading in each environment
Serving code in each environment loads from its own registered model, using the same alias name.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
model = mlflow.pyfunc.load_model("models:/iris-classifier-prod@champion")Only the model name differs between environments. You can pass the name through configuration, such as an environment variable, so the code stays identical.
Rolling back after a copy
Rollback works the same way as in a single registered model. Production keeps every version it has received, so you repoint the alias at an earlier one.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.set_registered_model_alias("iris-classifier-prod", "champion", "1")Check the version numbers in the production model before you do this. They are not the staging numbers.
Choosing between aliases and copies
| Concern | Aliases on one registered model | Copies across registered models |
|---|---|---|
| Setup | One registered model | One registered model per environment |
| Promotion cost | Metadata update, instant | Copies the model files, scales with model size |
| Version numbers | One shared sequence | Independent sequence per environment |
| Access control | Same permissions for all environments | Permissions can differ per registered model |
| Lineage | Version links directly to its source run | Copy links to the source URI, so you add a tag to trace it |
| Isolation | Anyone who can write aliases can promote | Writing to production requires access to the production model |
Choose copies when:
- Your organization requires that only a specific team or service account can write to production models.
- Staging and production use separate tracking servers or registries. In that case the copy needs both registries reachable from the script, and you should confirm that your MLflow version supports the cross-registry source URI form you plan to use.
- You want production to hold only models that were explicitly approved, with no experimental versions mixed in.
Choose aliases when:
- One team owns the whole path from training to serving.
- Models are large and copy time or storage cost matters.
- You want the simplest mental model, with one list of versions and a few named pointers.
Common errors
- Copying by alias and expecting it to follow. The alias resolves once. Later moves of the source alias do not affect the copy.
- Forgetting to set the alias on the destination. The copy creates a version but no
champion. Serving code that loadsmodels:/iris-classifier-prod@championthen fails because the alias does not exist. - Assuming tags travel with the copy. Re-apply any tags that downstream checks depend on. Verify what your MLflow version copies before relying on it.
- Mixing up version numbers. Staging v3 and production v1 can be the same model. Record the mapping in a tag.
- Treating copy as atomic with the alias update. The copy and the alias change are separate calls. If the script fails between them, production holds a new version that is not yet live. This is safe, but you should make the script idempotent or clean up on retry.
Exercise
Exercise
You copy models:/iris-classifier-staging@champion into iris-classifier-prod, and the call returns version 1. Later, staging's champion alias moves to a newer version. Does production's version 1 change? What must you still do after the copy before serving code that loads models:/iris-classifier-prod@champion works?
Show answer
Production's version 1 does not change. The alias was resolved once at copy time, and the model files were copied into the destination. Before the serving code works, you must set the champion alias on the production registered model, for example with client.set_registered_model_alias("iris-classifier-prod", "champion", "1"), because aliases are not copied.
Exercise
Your team has one registered model with champion and challenger aliases. A new policy says only the release service account may change what runs in production, while data scientists must keep full access to experiment. Which approach fits, and what is the cost?
Show answer
Use separate registered models for staging and production and promote with copy_model_version, then grant write access to the production model only to the release service account. The cost is that promotion copies the model files, version numbers diverge between the two models, and you must record lineage yourself, for example with a promoted_from tag.