Sub topic 11
Sending scoring requests over REST
This chapter covers how a client calls a serving endpoint once it is ready. It assumes you can read a basic HTTP request and JSON, and that the iris-endpoint endpoint from the previous chapter exists and is in the Ready state. Authentication is covered in detail in the previous chapter on tokens. Here you only need a valid token in the DATABRICKS_TOKEN environment variable.
The invocations URL
Every serving endpoint exposes one scoring URL:
https://<workspace-host>/serving-endpoints/<endpoint-name>/invocationsYou send an HTTP POST request to this URL. The body is JSON, and the response is JSON. The endpoint name is the name you chose at creation, here iris-endpoint.
Two headers are required:
Authorization: Bearer <token>identifies the caller.Content-Type: application/jsontells the server how to parse the body.
A first request
The model was logged with four numeric input columns. The request below sends one row in the dataframe_split format.
curl -X POST "https://<workspace-host>/serving-endpoints/iris-endpoint/invocations" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"dataframe_split": {"columns": ["sepal length (cm)", "sepal width (cm)", "petal length (cm)", "petal width (cm)"], "data": [[5.1, 3.5, 1.4, 0.2]]}}'A successful call returns HTTP 200 and a JSON object with a predictions field:
{"predictions": [0]}The value 0 is the class index for the first iris species. There is one prediction for each row you sent, in the same order.
Payload formats
The endpoint accepts several JSON shapes. They all describe the same table of inputs.
| Key | Shape | Use it when |
|---|---|---|
| `dataframe_split` | `columns` list plus `data` list of rows | You have a pandas DataFrame. This is the format used in this chapter. |
| `dataframe_records` | List of objects, one per row | You want each row to be self-describing. |
| `inputs` | Tensor-style list or dict | The model takes arrays rather than named columns. |
| `instances` | List of rows or dicts | You follow the TensorFlow Serving convention. |
Send only one of these keys per request. The same single row in dataframe_records looks like this:
{"dataframe_records": [{"sepal length (cm)": 5.1, "sepal width (cm)": 3.5, "petal length (cm)": 1.4, "petal width (cm)": 0.2}]}Pandas can produce either format for you. df.to_json(orient="split") yields the body for dataframe_split, and df.to_dict(orient="records") yields the list for dataframe_records.
How the signature shapes the request
The signature you logged earlier is the contract for the request. The serving container uses it to check column names and types before the model runs. This explains most request failures:
- A misspelled or missing column name is rejected. The iris column names contain spaces and units, so copy them exactly.
- A value of the wrong type, such as a string where the signature says
double, is rejected. - Extra columns not in the signature are usually an error as well, so send only what the model was logged with.
If you are unsure what the model expects, open the model version in Unity Catalog and read its signature, or look at the input example stored with the model.
Calling from Python
The requests library gives you the same call in a script. Reading the token and host from the environment keeps secrets out of the code.
import os
import requests
host = os.environ["DATABRICKS_HOST"] # for example https://adb-123.azuredatabricks.net
token = os.environ["DATABRICKS_TOKEN"]
payload = {
"dataframe_split": {
"columns": [
"sepal length (cm)",
"sepal width (cm)",
"petal length (cm)",
"petal width (cm)",
],
"data": [[5.1, 3.5, 1.4, 0.2], [6.7, 3.0, 5.2, 2.3]],
}
}
response = requests.post(
f"{host}/serving-endpoints/iris-endpoint/invocations",
headers={"Authorization": f"Bearer {token}"},
json=payload,
timeout=60,
)
response.raise_for_status()
print(response.json()["predictions"])Passing json=payload sets the Content-Type header for you. Two rows return two predictions, such as [0, 2].
Common errors
| Status | Typical cause | What to check |
|---|---|---|
| 400 | Payload does not match the signature | Column names, types, and the payload key |
| 401 | Missing or invalid token | The `Authorization` header and token expiry |
| 403 | Caller lacks permission on the endpoint | The endpoint's permissions for that user or service principal |
| 404 | Wrong endpoint name or host | The URL path and the workspace host |
| 429 | Too many requests | Reduce the rate and retry with backoff |
| 5xx | Server or model error | The endpoint logs, covered in the troubleshooting chapter |
Exercise
Your request to iris-endpoint returns HTTP 400. The body uses dataframesplit with the columns "sepallength", "sepalwidth", "petallength" and "petal_width". The token is valid. What is the most likely cause, and how do you fix it?
Show answer
The column names do not match the signature. The model was logged with names such as "sepal length (cm)", including spaces and units. Replace the columns list with the exact logged names, or read the signature on the model version to confirm them, then resend the request.
Exercise
Why does passing json=payload to requests.post make an explicit Content-Type header unnecessary?
Show answer
The requests library serializes the dictionary to JSON and sets Content-Type to application/json automatically.