The Registry is a central hub for versioning and staging models. It allows you to separate the Training phase from the Deployment phase.
1. Registering a Model Programmatically¶
Instead of using the UI, you can register models directly from code.
import mlflow
from mlflow.tracking import MlflowClient
model_name = "Iris_Classifier"
run_id = "your_run_id_here"
model_uri = f"runs:/{run_id}/model"
# Register the model
result = mlflow.register_model(model_uri, model_name)
print(f"Registered model version: {result.version}")2. Managing Versions and Aliases¶
Aliases (like @champion or @challenger) are the modern way to point to specific versions without changing your code every time a new model is trained.
client = MlflowClient()
# Set an alias for a specific version
client.set_registered_model_alias(model_name, "champion", "1")
# Transitioning stages (Legacy method)
client.transition_model_version_stage(
name=model_name,
version=1,
stage="Production"
)3. Loading by Alias¶
This is the gold standard for production. Your inference code stays exactly the same.
# Load the current champion
model = mlflow.pyfunc.load_model(f"models:/{model_name}@champion")