Sub topic 8
Creating an endpoint for a registered model
This chapter shows how to turn a registered model version into a live Model Serving endpoint with the Databricks SDK. It assumes you have finished the earlier chapters. You should have a model registered as main.default.iris_classifier with at least one version, and you should know how aliases and privileges work. You also need the databricks-sdk package and a workspace with Model Serving enabled.
What an endpoint is
A serving endpoint is a named, managed REST service. You tell Databricks which model version to serve and how much compute to give it. Databricks builds a container for the model, starts it, and exposes a URL that accepts scoring requests.
An endpoint has three parts that matter when you create one:
- A name. It is unique in the workspace and becomes part of the URL.
- A config. The config lists one or more served entities.
- A served entity. This is one model version plus its compute settings.
The endpoint name does not have to match the model name. Pick a name that describes the use, such as iris-endpoint.
Before you create the endpoint
Three conditions must hold, and most creation failures trace back to one of them.
- The model must be registered in Unity Catalog with a signature. The earlier chapters covered this. Without a signature, registration fails, so an unsigned model never reaches this step.
- You must be able to use the model. You need
USE CATALOGonmain,USE SCHEMAonmain.default, andEXECUTEon the model. The identity that creates the endpoint is the one checked. - You must be allowed to create endpoints in the workspace. This is a workspace-level entitlement, separate from Unity Catalog privileges.
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`;Creating the endpoint with the SDK
The Databricks SDK for Python wraps the REST API. The method create_and_wait sends the create request and then polls until the endpoint is ready or fails.
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import EndpointCoreConfigInput, ServedEntityInput
w = WorkspaceClient()
w.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,
)
]
),
)WorkspaceClient() with no arguments reads credentials from the environment or from a configuration profile. Inside a Databricks notebook it uses the notebook's own identity. Outside a notebook, set DATABRICKS_HOST and DATABRICKS_TOKEN, or configure a profile in ~/.databrickscfg.
What each field does
| Field | Meaning |
|---|---|
| `name` | The endpoint name. It appears in the URL path `/serving-endpoints/<name>/invocations`. |
| `entity_name` | The three-level model name in Unity Catalog. |
| `entity_version` | The model version number, passed as a string. |
| `workload_size` | The concurrency the endpoint can handle: `Small`, `Medium` or `Large`. |
| `scale_to_zero_enabled` | When true, the endpoint releases its compute after a period with no traffic. |
The next chapter covers workload size and scale to zero in detail. For now, Small with scale to zero is a cheap choice for development.
Why the version is a number, not an alias
Look at the entity_version field. You pass a version number here. You do not pass the alias champion. Aliases are resolved by MLflow when you call load_model, but the serving configuration pins a concrete version.
This has a practical consequence. If you later move the champion alias from version 1 to version 2, the running endpoint keeps serving version 1. Moving an alias does not update an endpoint. To change what the endpoint serves, you update its config, which is the subject of a later chapter.
You can still use the alias as a lookup step in your own code:
import mlflow
from mlflow import MlflowClient
mlflow.set_registry_uri("databricks-uc")
client = MlflowClient()
champion = client.get_model_version_by_alias("main.default.iris_classifier", "champion")
print(champion.version)You would then pass champion.version as entity_version. That keeps the alias as the single source of truth for which version should go live, while the endpoint stays pinned to a known version.
What happens while the endpoint builds
Creation is not instant. create_and_wait blocks for several minutes, because Databricks does the following in order:
- Validates that the model version exists and that you can use it.
- Builds a container image containing the model and its dependencies. The dependencies come from the requirements that MLflow recorded when the model was logged.
- Provisions compute of the requested size.
- Starts the container and runs a readiness check.
The endpoint reports its state as NOT_UPDATING and READY when it finishes. While it is working, the config state is IN_PROGRESS.
The last transition matters. When you update a ready endpoint and the new config fails, the previous config keeps serving traffic. A failed first creation has no earlier config to fall back on, so the endpoint stays unusable until you fix the cause.
Checking the endpoint state
You can read the state at any time. This is useful if you create the endpoint without waiting, using create instead of create_and_wait.
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
endpoint = w.serving_endpoints.get("iris-endpoint")
print(endpoint.state.ready) # EndpointStateReady.READY when usable
print(endpoint.state.config_update) # EndpointStateConfigUpdate.NOT_UPDATING when idleThe create method returns a waiter object right away. You can call .result() on it later to block, or poll get yourself. Polling is the better choice in a pipeline that does other work while the build runs.
Common errors at creation time
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that the endpoint name already exists | Endpoint names are unique per workspace | Choose another name, or update the existing endpoint |
| Permission denied on the model | The creator lacks `EXECUTE`, `USE SCHEMA` or `USE CATALOG` | Grant the three privileges shown earlier |
| Model version not found | Wrong `entity_name` or a version that does not exist | Check the three-level name and list versions |
| Build fails during dependency install | A pinned package cannot be resolved in the serving environment | Read the build logs and fix the logged requirements |
| Endpoint stays in `IN_PROGRESS` for a long time | Large model or large dependencies | Wait, then check the build logs before retrying |
The build logs are your main tool. In the workspace, open Serving, select the endpoint, and open the Logs tab. It has build logs and service logs. Build failures appear in the build logs. Failures after start-up, such as an exception in the model's code, appear in the service logs.
Dependency mismatches
The most common build failure comes from the environment in which the model was logged. MLflow records the packages it detected, such as scikit-learn==1.4.2. The serving image tries to install exactly those versions. If a version is not available for the serving Python runtime, the build fails.
You can inspect what was recorded before you create the endpoint:
import mlflow
mlflow.set_registry_uri("databricks-uc")
path = mlflow.artifacts.download_artifacts(
artifact_uri="models:/main.default.iris_classifier/1",
dst_path="/tmp/iris_model",
)
print(open(f"{path}/requirements.txt").read())If the file lists a package you do not need, or one that cannot be installed, log a new model version with an explicit pip_requirements list and serve that version instead.
Listing and deleting endpoints
Two more calls are worth knowing during development.
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
for e in w.serving_endpoints.list():
print(e.name, e.state.ready)
w.serving_endpoints.delete("iris-endpoint")Deleting an endpoint removes the URL and stops billing for its compute. It does not touch the registered model. Deleting is hard to undo in practice, because any client that calls the URL gets an error until you recreate the endpoint with the same name.
A creation checklist
Run through this list before you create an endpoint for a new model:
- The model is registered under a three-level name and has a signature.
- The creating identity has
USE CATALOG,USE SCHEMAandEXECUTE. - You have chosen an exact version number, for example by resolving an alias.
- You have picked a workload size and decided on scale to zero.
- The endpoint name is unused and describes the purpose.
Exercise
You move the alias champion from version 1 to version 2 of main.default.iris_classifier. An endpoint was created earlier with entity_version="1". Which version does the endpoint serve now, and what must you do to serve version 2?
Show answer
It still serves version 1. Serving configs pin a concrete version, and moving an alias does not change them. To serve version 2, submit an updated endpoint config with entity_version="2", for example by looking up the alias with get_model_version_by_alias and passing the returned version number.
Exercise
The first creation of an endpoint fails during the build with a dependency installation error. Where do you look first, and why is there no older version still serving?
Show answer
Open the endpoint's Logs tab and read the build logs, which show the failing package install. No older version serves because this was the first config. Only an update to an already ready endpoint keeps the previous config running when the new one fails.