Sub topic 11
Registering a model in Unity Catalog
A logged model lives inside one run's artifacts. That works while you are experimenting, but it is a poor way to hand a model to someone else. A run ID says nothing about which model is approved, who may use it, or which version is newer. The model registry fills that gap. On Databricks, the registry is part of Unity Catalog, the governance layer that also manages tables and volumes.
This chapter assumes you can log a model with a signature and an input example, and that you have a run object from mlflow.start_run. It also assumes you know basic Unity Catalog naming, such as catalog and schema. The next chapter covers aliases and loading in detail. Here we focus on getting a version into the registry.
What registration creates
Registering a model does two things:
- It creates a registered model, if one with that name does not exist yet. The registered model is a named container.
- It creates a model version inside that container. The version points at the logged model files and copies them into governed storage.
Versions are numbered 1, 2, 3 and so on. A version is immutable. If you register again under the same name, you get a new version and the old one stays unchanged.
The three-level name
A registered model in Unity Catalog has a three-level name: catalog.schema.model. For example, main.default.iris_classifier is the model iris_classifier in the default schema of the main catalog.
Because the model is a Unity Catalog object, it follows the same rules as other objects:
- The catalog and schema must already exist. Registration creates the model, not the schema.
- Access is granted with Unity Catalog privileges, not with experiment permissions.
- The model appears in Catalog Explorer under its schema.
To register a model you need these privileges:
| Privilege | On what | Why |
|---|---|---|
| `USE CATALOG` | The catalog | Lets you see into the catalog |
| `USE SCHEMA` | The schema | Lets you see into the schema |
| `CREATE MODEL` | The schema | Lets you create a registered model |
If the model already exists and you only add versions, you need to own it or have the privileges your admin granted on it. Check with your workspace admin before assuming main.default is writable. Many workspaces restrict it, and a schema you own is a safer choice.
Pointing MLflow at Unity Catalog
MLflow has two separate settings: the tracking URI, which says where runs are stored, and the registry URI, which says where models are registered. On Databricks, tracking is already configured for you. The registry URI may default to the legacy workspace registry on older runtimes, so set it explicitly:
import mlflow
mlflow.set_registry_uri("databricks-uc")Without this line, a three-level name may be rejected or sent to the wrong registry. Recent runtimes default to Unity Catalog, but setting it costs one line and removes the doubt.
Registering in one step
The simplest route is to pass registered_model_name when you log the model. This script is complete. It trains, logs and registers in one go.
import mlflow
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
mlflow.set_registry_uri("databricks-uc")
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model = LogisticRegression(max_iter=200).fit(X_train, y_train)
signature = infer_signature(X_test, model.predict(X_test))
with mlflow.start_run(run_name="log-and-register") as run:
mlflow.sklearn.log_model(
model,
artifact_path="model",
signature=signature,
input_example=X_test[:5],
registered_model_name="main.default.iris_classifier",
)
print(run.info.run_id)This is convenient, but it registers every run that executes it. In a tuning loop that would create dozens of versions, most of them poor. Prefer this form for a final training job, and use the two-step form below when you want to choose a run first.
Registering a run you already logged
The two-step form separates logging from registering. You log the model during experimentation, compare runs, and then register only the winner. Registration takes a model URI of the form runs:/<run_id>/<artifact_path>.
import mlflow
mlflow.set_registry_uri("databricks-uc")
model_name = "main.default.iris_classifier"
model_uri = f"runs:/{run.info.run_id}/model"
version = mlflow.register_model(model_uri, model_name)
print(version.name, version.version)The model at the end of the URI must match the artifact_path you used in log_model. If you logged with artifact_path="clf", the URI ends in /clf. A mismatch is the most common cause of a "model not found" failure.
register_model returns a ModelVersion object. Its version attribute is a string such as "1". Keep it, because later calls such as setting an alias need it.
On a large model, registration can take a while because the files are copied. By default register_model waits until the version is ready. Pass await_registration_for=0 if you do not want to wait, but then check the version status before using it.
The signature requirement
Unity Catalog requires every model version to have a signature. A signature describes the model's input and output schema. If the logged model has no signature, registration fails with an error saying the model must have a signature.
The fix is to log the model again with a signature. You cannot add one to a model that is already registered. This is why the earlier chapter on logging models stressed infer_signature. An input example is not required by Unity Catalog, but it lets you test the model quickly and is good practice.
Inspecting and annotating a version
After registering, use MlflowClient to read back what the registry holds and to attach metadata. Tags and descriptions help colleagues understand a version without opening the run.
from mlflow import MlflowClient
client = MlflowClient()
model_name = "main.default.iris_classifier"
mv = client.get_model_version(model_name, version.version)
print(mv.version, mv.status, mv.run_id)
client.set_model_version_tag(
model_name, version.version, "validated_by", "data-science-team"
)
client.update_model_version(
model_name, version.version, description="Logistic regression, C=1.0, iris"
)
for v in client.search_model_versions(f"name = '{model_name}'"):
print(v.version, v.tags)Each version keeps its run_id. This is the link back to the run that produced it, with its parameters, metrics and recorded environment. Following that link from a version to a run is how you answer "how was this trained?" months later.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a missing signature | Model logged without `signature` | Re-log with `infer_signature` |
| Name rejected as invalid | Two-level name, or registry URI not set | Use `catalog.schema.model` and call `set_registry_uri("databricks-uc")` |
| Permission denied | Missing `CREATE MODEL`, `USE SCHEMA` or `USE CATALOG` | Ask the schema owner for a grant, or use a schema you own |
| Catalog or schema not found | Name misspelled or object does not exist | Check Catalog Explorer and create the schema first |
| Model artifact not found | `runs:/` URI path differs from `artifact_path` | Use the same artifact path as in `log_model` |
| Dozens of unwanted versions | `registered_model_name` used inside a loop | Register only the selected run |
Design choices
Decide how many registered models you need. A model's versions should be comparable: the same task, the same input schema, and the same meaning of the output. If the input schema changes in a breaking way, consider a new registered model instead of a new version. Consumers that load by version or alias would otherwise break.
Decide where models live. Putting models for different environments in different catalogs, such as dev, staging and prod, lets you control access with catalog-level grants. The names then differ by catalog, and promotion means copying a version from one name to another.
Keep names stable. Renaming is possible but breaks any code that refers to the old name.
Exercise
A teammate's script logs a model with artifact_path="clf" and then calls mlflow.register_model(f"runs:/{run.info.run_id}/model", "iris_classifier") after mlflow.set_registry_uri("databricks-uc"). Give two reasons the call can fail and say how to fix each.
Show answer
First, the model URI ends in /model but the artifact path was clf, so MLflow cannot find the model files. Use runs:/{run.info.run_id}/clf. Second, iris_classifier is not a three-level name, and Unity Catalog requires catalog.schema.model, for example main.default.iris_classifier. A third possible cause, if the model was logged without a signature, is also a failure, and the fix there is to log the model again with infer_signature.