Sub topic 1
What the Model Registry is for
Prerequisites
This chapter assumes you can do the following:
- Write basic Python and work inside a virtual environment.
- Use MLflow Tracking: experiments, runs, parameters, metrics, and artifacts.
- Train and save a simple scikit-learn model.
- Run commands in a terminal.
You do not need a running server yet. The setup comes in a later chapter. This chapter explains the problem the registry solves and the vocabulary you will use for the rest of the tutorial.
The problem: a model file is not a managed asset
MLflow Tracking records what happened during training. Each run stores parameters, metrics, and artifacts. If you log a model with mlflow.sklearn.log_model, the model files become artifacts of that run.
That is enough for experimentation. It is not enough for deployment. Consider the questions a team asks once a model leaves a notebook:
- Which model is serving production traffic right now?
- Which run produced it, and with which data and code?
- Did anyone validate it before it went live?
- If it misbehaves, which earlier model can we go back to?
- Who is allowed to change what is live?
A run ID such as runs:/3f9a.../model answers none of these. It identifies one artifact in one experiment. It has no friendly name and no history. It also carries no record of whether the model was approved. Teams that rely on run IDs usually end up with a spreadsheet, a wiki page, or a hard-coded ID in serving code. All of these drift out of date.
The Model Registry is the MLflow component that fixes this. It turns a logged model artifact into a named, versioned asset with metadata and a movable label for "the one in use".
What the registry gives you
The registry adds four things on top of Tracking.
- A stable name. Consumers ask for
iris-classifier, not for a run ID. - Versioning. Each time you register a model under that name, the registry creates a new numbered version. Versions are never overwritten.
- Lineage. Each version keeps a link to the run that produced it. From a version you can find the parameters, metrics, and code state of that run.
- Annotation and promotion. You can attach descriptions and tags to a model or a version. You can also assign aliases, which are named pointers such as
champion, to mark which version is live.
Together these let serving code say "load whatever is currently champion" while the team decides, in one place, which version that is.
The core objects
The registry has three objects. Keeping them separate in your head prevents most beginner mistakes.
| Object | What it is | Example |
|---|---|---|
| Logged model artifact | Files saved under a run, with an `MLmodel` metadata file | `runs:/<run_id>/model` |
| Registered model | A named container for a model's version history | `iris-classifier` |
| Model version | One numbered entry in that container, linked to a source artifact | `iris-classifier`, version 2 |
A logged model artifact exists whether or not you ever register it. Registering it creates a model version that points back to the artifact. If the registered model name does not exist yet, the registry creates it first, and the new version is number 1.
Tags and aliases attach to these objects:
- A registered model can have a description, tags, and aliases defined on it. Aliases are stored on the registered model and each one points at a single version.
- A model version can have its own description and tags. These are the right place to record facts about one specific build, such as a validation result.
The next diagram shows how the objects relate.
Two points follow from the diagram:
- The version, not the registered model, is the unit that gets deployed. The registered model is only the namespace.
- An alias points to exactly one version at a time. Several aliases can point to the same version. Moving an alias changes what consumers get without creating or editing any version.
The lifecycle in outline
The rest of this tutorial walks through the lifecycle below. Here it is end to end, so each later chapter has a place to fit.
Notice that rollback and promotion are the same operation: moving an alias. That is the main design idea of the registry, and it is why the serving code in the last step never changes.
A first look at the operations
You will run the full workflow in later chapters. A short preview shows how small the API surface is. This sketch assumes a tracking server is already running at http://127.0.0.1:5000 and that the model was already logged in a run.
import mlflow
from mlflow import MlflowClient
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = MlflowClient()
run_id = "<run-id-from-your-training-run>"
# 1. Create a new version of the registered model from a run's artifact.
mv = mlflow.register_model(f"runs:/{run_id}/model", "iris-classifier")
# 2. Promote by pointing an alias at that version.
client.set_registered_model_alias("iris-classifier", "champion", mv.version)
# 3. Consumers load by alias and never see the version number.
model = mlflow.pyfunc.load_model("models:/iris-classifier@champion")Replace the placeholder run ID with a real one before you run it. Three calls cover registration, promotion, and consumption. Every other feature in the registry supports one of these steps.
Aliases versus the older stages
Older MLflow tutorials describe registry stages: None, Staging, Production, and Archived. A version moved through these fixed stages, and code loaded models:/name/Production. Stages are deprecated. Reasons include:
- The four names are fixed, so they cannot describe workflows with more than one environment or with a canary and a main model.
- A stage is a single slot, which forces a particular process on every team.
- Aliases are free-form names you define, and they work the same way for any workflow.
This tutorial uses aliases throughout. A later chapter covers how to migrate stage-based code. If you meet transition_model_version_stage in older code, treat it as legacy.
What the registry does not do
Setting expectations helps you avoid misusing it.
- It does not train or validate models. Your pipeline does that. The registry stores the outcome, for example as a tag.
- It does not deploy anything by itself. Moving an alias changes a pointer. A serving process must load by that alias, and it must reload to see a change. A process that loaded the model at startup keeps using the old version until it restarts or reloads.
- It does not enforce approval by default. Any client that can write to the server can move an alias. Gates and access control come from your automation and from how you deploy the server. A later chapter covers this.
- It does not replace data versioning. A model version links to a run, but it does not store your training data.
Common errors at this stage
- Treating a run ID as a release identifier. Run IDs are fine for debugging, but they are poor deployment handles. Register the model and deploy from the name.
- Confusing the registered model with a version. Setting a tag on the registered model does not annotate a particular build. Use version-level tags for per-build facts.
- Expecting versions to change. A version is fixed once created. To "update" a model, register a new version and move the alias.
- Assuming an alias follows the latest version. An alias only moves when you move it. Registering version 3 does not change what
championpoints to.
Exercise
A teammate says: "We registered version 3 of iris-classifier, so production now uses it." Production code loads models:/iris-classifier@champion, and the champion alias was last set to version 2. Is the teammate right? What single step would make production use version 3 once it is validated, and what would you check first?
Show answer
The teammate is wrong. Registering a version does not move any alias, so champion still points to version 2. Production keeps using version 2. After validation passes, call client.setregisteredmodel_alias("iris-classifier", "champion", "3"). Before that, check that version 3 carries the expected validation tag. Also note that a serving process that loaded the model at startup must reload or restart to pick up the change.
Exercise
Name the three registry objects in the table of core objects and say which one is the unit that is deployed. Which one holds aliases?
Show answer
The three objects are the logged model artifact (files under a run), the registered model (a named container for the version history), and the model version (one numbered entry linked to a source artifact). The model version is the deployable unit. Aliases are defined on the registered model, and each alias points at one version.