Sub topic 2
Working with Versions and Aliases
Assume you know basic Python, MLflow runs, Unity Catalog catalogs and schemas, and REST APIs. This chapter follows a registered model from version management to a Model Serving endpoint.
Versions and aliases
A registered model is identified by a three-level name:
catalog.schema.model
Each successful registration creates a model version. A version is an immutable snapshot of the model files and metadata. An alias is a changeable label that points to one version. For example, champion can identify the version currently approved for production.
import mlflow
from mlflow import MlflowClient
mlflow.set_registry_uri("databricks-uc")
client = MlflowClient()
model_name = "main.default.iris_classifier"
client.set_registered_model_alias(model_name, "champion", 1)
model = mlflow.pyfunc.load_model(
f"models:/{model_name}@champion"
)Clients use the alias instead of a hard-coded version number. To release version 2, move the alias:
client.set_registered_model_alias(model_name, "champion", 2)| Reference | Meaning | Best use |
|---|---|---|
| `models:/main.default.iris_classifier/1` | Exact version 1 | Reproducible tests |
| `models:/main.default.iris_classifier@champion` | Version behind `champion` | Production clients |
Granting the privileges models need
Users need access to the namespace before they can use the model. A group that calls the model usually needs catalog and schema usage plus model execution permission.
GRANT USE CATALOG ON CATALOG main TO `data-scientists`;
GRANT USE SCHEMA ON SCHEMA main.default TO `data-scientists`;
GRANT EXECUTE ON MODEL main.default.iris_classifier TO `data-scientists`;Creation and version management may require additional ownership or model-management privileges. Grant only the permissions required by each group.
What Model Serving provides
Databricks Model Serving runs a registered model behind a managed HTTPS endpoint. Databricks manages the serving infrastructure, authentication integration, and endpoint lifecycle. You choose the model version and compute settings.
Creating an endpoint for a registered model
The Databricks SDK can create an endpoint for a specific version:
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import (
EndpointCoreConfigInput,
ServedEntityInput,
)
workspace = WorkspaceClient()
workspace.serving_endpoints.create_and_wait(
name="iris-endpoint",
config=EndpointCoreConfigInput(
served_entities=[
ServedEntityInput(
entity_name="main.default.iris_classifier",
entity_version="1",
workload_size="Small",
scale_to_zero_enabled=True,
)
]
),
)Exercise
Why should a production client usually load a model through an alias instead of version 1 directly?
Show answer
An alias lets operators move the client to a new approved version without changing the client code.