---
title: Model Serving quickstart
description: Log a scikit-learn model, serve it on a Model Serving endpoint, and query it from one Databricks notebook with the Python SDK.
canonical_url: https://docs.databricks.com/gcp/en/machine-learning/model-serving/model-serving-quickstart
---

# Model Serving quickstart

Log, serve, and query a scikit-learn model from one notebook with the Python SDK.

## <a id="requirements" />Requirements

* A workspace in a [supported region](https://docs.databricks.com/gcp/en/machine-learning/model-serving/model-serving-limits.md#regions).
* CREATE MODEL and USE SCHEMA on the schema, and USE CATALOG on the catalog, to register a new model in Unity Catalog. See [Manage model lifecycle in Unity Catalog](https://docs.databricks.com/gcp/en/machine-learning/manage-model-lifecycle/index.md).

## <a id="log-serve-and-query-the-model" />Log, serve, and query the model

Import the notebook, or follow the steps:

#### Model Serving quickstart notebook

[Get notebook](https://docs.databricks.com/notebooks/source/machine-learning/model-serving-quickstart.html)

### <a id="log-the-model" />Log the model

Install the packages:

```python
%pip install -U "mlflow>=3" "databricks-sdk>=0.102.0" scikit-learn pandas
dbutils.library.restartPython()
```

Run the cells in order in one notebook. The sample runs on serverless compute. The install cell finishes without errors and restarts Python. The sample uses the `name` argument of `log_model`, which needs MLflow 3 or above.

A Unity Catalog model name has three levels, `catalog.schema.model`, so replace `<catalog>` and `<schema>` with a catalog and schema where you have the privileges in [Requirements](https://docs.databricks.com/gcp/en/machine-learning/model-serving/model-serving-quickstart.md#requirements).

```python
# Set these once. Replace the placeholders with your own names.
CATALOG = "<catalog>"
SCHEMA = "<schema>"
MODEL = "quickstart_model"
ENDPOINT_NAME = "model-serving-quickstart"
FULL_MODEL_NAME = f"{CATALOG}.{SCHEMA}.{MODEL}"
```

The sample fits a model on four made-up rows where y = 2x. If a model of that name already exists, the log step adds a version to it.

```python
import pandas as pd
import mlflow.sklearn
from mlflow.models import infer_signature
from sklearn.linear_model import LinearRegression

# Four rows of made-up data where y = 2x.
X = pd.DataFrame({"x": [1, 2, 3, 4]})
y = [2, 4, 6, 8]
model = LinearRegression().fit(X, y)
signature = infer_signature(X, model.predict(X))

info = mlflow.sklearn.log_model(
    sk_model=model,
    name="model",
    signature=signature,
    registered_model_name=FULL_MODEL_NAME,
)
MODEL_VERSION = str(info.registered_model_version)
print("Registered model:", FULL_MODEL_NAME)
print("Version:", MODEL_VERSION)
```

The cell prints the following:

```text
Registered model: <catalog>.<schema>.quickstart_model
Version: 1
```

Without a signature and an input example, `log_model` raises an error that begins with this text.

```text
Model passed for registration did not contain any signature metadata.
```

**Solution:** Build the signature with `infer_signature` and pass it as `signature=`, as the log cell above does. Afterward, the log cell prints the registered model name and its version.

### <a id="create-the-endpoint" />Create the endpoint

Create a CPU endpoint of size Small. The `scale_to_zero_enabled` field sets whether its compute scales down to zero. `WorkspaceClient()` takes its credentials from the notebook. Endpoint names are unique in a workspace, so change `ENDPOINT_NAME` if that name is taken.

```python
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import EndpointCoreConfigInput, ServedEntityInput, ServingModelWorkloadType

w = WorkspaceClient()
endpoint = w.serving_endpoints.create_and_wait(
    name=ENDPOINT_NAME,
    config=EndpointCoreConfigInput(
        name=ENDPOINT_NAME,
        served_entities=[
            ServedEntityInput(
                entity_name=FULL_MODEL_NAME,
                entity_version=MODEL_VERSION,
                workload_type=ServingModelWorkloadType.CPU,
                workload_size="Small",
                scale_to_zero_enabled=True,
            )
        ],
    ),
)
print("state.ready:", endpoint.state.ready)
print("state.config_update:", endpoint.state.config_update)
```

The cell prints the following:

```text
state.ready: EndpointStateReady.READY
state.config_update: EndpointStateConfigUpdate.NOT_UPDATING
```

The `create_and_wait` call returns when `config_update` is `NOT_UPDATING`, and its default timeout is 20 minutes, after which it raises `TimeoutError`. `state.ready` is `READY` only when every served entity is ready.

Pass `workload_type` as the SDK enum `ServingModelWorkloadType.CPU`, since the string `"CPU"` raises `AttributeError`.

To use the Serving UI instead, see [Create in the Serving UI](https://docs.databricks.com/gcp/en/machine-learning/model-serving/create-manage-serving-endpoints.md#create-serving-ui). To use REST instead, see [Create with the REST API](https://docs.databricks.com/gcp/en/machine-learning/model-serving/create-manage-serving-endpoints.md#create-rest-api).

### <a id="query-the-endpoint" />Query the endpoint

Send two inputs, 1 and 5, as a table in `dataframe_split` format: `columns` lists the column names, and `data` lists the rows.

```python
import time
from databricks.sdk.errors import NotFound
from databricks.sdk.service.serving import DataframeSplitInput

# The endpoint can take a few seconds to accept requests after the create call returns.
for attempt in range(12):
    try:
        response = w.serving_endpoints.query(
            name=ENDPOINT_NAME,
            dataframe_split=DataframeSplitInput(columns=["x"], data=[[1], [5]]),
        )
        break
    except NotFound:
        time.sleep(10)
else:
    raise TimeoutError("The endpoint didn't accept the request within two minutes.")
print(response.as_dict())
```

The cell prints the following:

```text
{'predictions': [2.0, 10.0], 'served-model-name': 'quickstart_model-1'}
```

The model returns one prediction per row: 2.0 for 1 and 10.0 for 5.

### <a id="delete-the-endpoint-and-the-model" />Delete the endpoint and the model

Optionally, delete the endpoint and the model. You can't undo deleting the endpoint.

```python
delete_result = w.serving_endpoints.delete(name=ENDPOINT_NAME)
print("delete returned:", repr(delete_result))
```

The cell prints the following:

```text
delete returned: None
```

The delete call returns `None`, and getting the endpoint afterward raises `ResourceDoesNotExist`. Then delete the model version that this quickstart registered. The cell also deletes the registered model when no other version is left:

```python
from mlflow import MlflowClient

client = MlflowClient()
client.delete_model_version(name=FULL_MODEL_NAME, version=MODEL_VERSION)
print("Deleted model version:", MODEL_VERSION)
if not client.search_model_versions(f"name='{FULL_MODEL_NAME}'"):
    client.delete_registered_model(name=FULL_MODEL_NAME)
    print("Deleted registered model:", FULL_MODEL_NAME)
```

The cell prints the following:

```text
Deleted model version: 1
Deleted registered model: <catalog>.<schema>.quickstart_model
```

## Additional resources

* To serve your own Python code, see [Package your model for Model Serving](https://docs.databricks.com/gcp/en/machine-learning/model-serving/deploy-custom-python-code.md).
* For GPU workload types, see [Compute type](https://docs.databricks.com/gcp/en/machine-learning/model-serving/custom-models.md#compute-type).
* To prepare an endpoint for production traffic, see [Optimize Model Serving endpoints for production](https://docs.databricks.com/gcp/en/machine-learning/model-serving/production-optimization.md).
