Sub topic 11
Registering a Model in Unity Catalog
This chapter assumes you can log runs and models with MLflow, as covered in the earlier chapters. You should also know what a run ID is and how to open a notebook on Databricks. Here you will take a model logged to a run and register it in Unity Catalog, so other people and jobs can find and use it by name.
Why register a model
A logged model lives inside one run. That is fine for exploration, but a run ID is a poor way to share a model. The model registry gives a model a stable name and a numbered version history. Unity Catalog is the Databricks governance layer, so a registered model also gets the same permissions, lineage and auditing as your tables.
The three-level name
Unity Catalog organizes objects in three levels: catalog, schema, then object. A registered model name follows the same pattern.
<catalog>.<schema>.<model_name>For example, main.default.iris_classifier means the model iris_classifier in the default schema of the main catalog. You need permission to create models in that schema, and use permission on the catalog and schema above it.
Point MLflow at Unity Catalog
Tell MLflow which registry to use before you register anything. Do this once per notebook session.
import mlflow
mlflow.set_registry_uri("databricks-uc")Recent Databricks ML runtimes may already default to Unity Catalog. Setting the URI explicitly keeps the notebook correct on any runtime.
Register the model
mlflow.register_model takes a model URI and a name. The URI has the form runs:/<run_id>/<artifact_path>. The artifact path must match the one you used in log_model, which was "model" earlier in this tutorial.
model_name = "main.default.iris_classifier"
version = mlflow.register_model(f"runs:/{run.info.run_id}/model", model_name)
print(version.name, version.version)The call does two things. If the model name does not exist, it creates the registered model. It then creates a new version that points at the logged artifacts. Calling it again with another run adds version 2, then 3, and so on. Versions are immutable, so you never edit one. You register a new one instead.
Why the signature matters
Unity Catalog requires a model to have a signature. This is the input and output schema you logged with infer_signature in the previous chapter. If the run's model has no signature, registration fails with an error that mentions the signature. The fix is to log the model again with signature=signature, then register the new run.
Check the result
You can confirm the registration in the Catalog Explorer under your catalog and schema, where the model appears with its versions. You can also check from code.
from mlflow import MlflowClient
client = MlflowClient()
registered = client.get_registered_model(model_name)
print(registered.name)
for mv in client.search_model_versions(f"name = '{model_name}'"):
print(mv.version, mv.run_id)Each version keeps its source run_id. That link lets you go from a deployed version back to the parameters, metrics and code state of the run that produced it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a missing signature | Model logged without a signature | Log with `infer_signature` output, then register again |
| Name not found or invalid | Name has fewer than three parts | Use `catalog.schema.model` |
| Permission denied | Missing `CREATE MODEL` or `USE SCHEMA` rights | Ask the schema owner for access |
| Model lands in the wrong registry | Registry URI not set | Call `mlflow.set_registry_uri("databricks-uc")` |
Exercise
You call mlflow.registermodel(f"runs:/{run.info.runid}/model", "iris_classifier") and it fails. Give two things to check, and say what version number you get if you register the same name again after a successful first registration.
Show answer
First, the name must have three parts, such as main.default.iris_classifier, because Unity Catalog requires catalog.schema.model. Second, the registry URI must be databricks-uc, and the run's model must have a signature. After a successful first registration, registering the same name again creates version 2, because each call adds a new immutable version under the same registered model.