Sub topic 4
Registering a model in Unity Catalog
This chapter assumes you can train a scikit-learn model, log it with MLflow, and work in a Databricks notebook. It also assumes you know that Unity Catalog organizes tables as catalog, schema and table. Here you register a logged model in Unity Catalog, which gives it a permanent name and a version number.
What registering does
Logging a model saves it as an artifact of one MLflow run. Registering adds that artifact to the model registry under a stable name. Each registration under the same name creates a new numbered version. Other people and services refer to the model by name and version, not by run ID.
Unity Catalog treats a registered model like other governed objects. Its name has three levels: catalog.schema.model. For example, main.default.iris_classifier is the model iris_classifier in the default schema of the main catalog.
Log and register in one step
The mlflow.set_registry_uri("databricks-uc") call is required. Without it, MLflow uses the older workspace model registry, which does not accept three-level names. The signature is also required, because Unity Catalog rejects models that lack one.
import mlflow
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
# Use Unity Catalog as the model registry
mlflow.set_registry_uri("databricks-uc")
X, y = load_iris(return_X_y=True, as_frame=True)
model = RandomForestClassifier(n_estimators=50, random_state=0).fit(X, y)
# Unity Catalog requires a signature for registered models
signature = infer_signature(X, model.predict(X))
with mlflow.start_run():
mlflow.sklearn.log_model(
model,
artifact_path="model",
signature=signature,
input_example=X.head(5),
registered_model_name="main.default.iris_classifier",
)The registered_model_name argument does the registration. If the model does not exist yet, MLflow creates it and adds version 1. If it already exists, MLflow adds the next version. You can check the result in Catalog Explorer, under the schema's Models section.
Register a model you already logged
If you logged a model earlier without a name, register it from its run URI. Replace <run_id> with the ID of the run that logged the model.
import mlflow
mlflow.set_registry_uri("databricks-uc")
result = mlflow.register_model(
model_uri="runs:/<run_id>/model",
name="main.default.iris_classifier",
)
print(result.name, result.version)The run must have logged the model with a signature. Otherwise registration fails.
Privileges you need
To register a model you need these privileges:
| Privilege | On | Why |
|---|---|---|
| `USE CATALOG` | The catalog | See the catalog |
| `USE SCHEMA` | The schema | See the schema |
| `CREATE MODEL` | The schema | Create the model and its versions |
If you own the schema, you already have them. Granting privileges to other users comes in a later chapter.
Common errors
- Missing signature. The error message says the model must have a signature. Fix it by passing
signature=tolog_model. - Permission denied. You lack
CREATE MODELon the schema, orUSE CATALOGorUSE SCHEMAabove it. Ask the schema owner to grant them. - Name or registry mismatch. The error mentions an invalid name or a missing registry. Call
mlflow.set_registry_uri("databricks-uc")before logging, and check that the catalog and schema exist.
Exercise
You run log_model with registered_model_name="main.default.iris_classifier" and get an error saying the model has no signature. What change fixes it, and why does Unity Catalog require it?
Show answer
Compute a signature with infer_signature(X, model.predict(X)) and pass it as signature= to log_model. Unity Catalog requires a signature so that it records the model's expected input and output schema. Serving and other consumers can then check that requests match that schema.
Exercise
A colleague's registration fails with a permission error, though the catalog and schema exist. Name the three privileges to check.
Show answer
USE CATALOG on the catalog, USE SCHEMA on the schema, and CREATE MODEL on the schema.