Sub topic 4
Calling a served model from Python
This chapter assumes you know basic Python, have used chat messages with a large language model, and can run a Databricks notebook. It covers how to call a chat model on a serving endpoint with the OpenAI-compatible client. The previous chapter introduced Foundation Model APIs and serving endpoints.
The working model
A serving endpoint is a named, authenticated REST service in your workspace. Databricks exposes chat models through the same request shape as the OpenAI chat completions API. You can therefore use the familiar client.chat.completions.create(...) call. The Databricks SDK builds a client that already knows your workspace URL and credentials, so you don't handle tokens yourself.
A first call
from databricks.sdk import WorkspaceClient
# Name of a chat model serving endpoint in your workspace
ENDPOINT_NAME = "<your-chat-endpoint-name>"
w = WorkspaceClient()
client = w.serving_endpoints.get_open_ai_client()
response = client.chat.completions.create(
model=ENDPOINT_NAME,
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what a vector index is in two sentences."},
],
max_tokens=200,
)
print(response.choices[0].message.content)Replace the placeholder with the name of an endpoint from the Serving page of your workspace. Inside a Databricks notebook, WorkspaceClient() picks up credentials automatically. Outside Databricks, it reads standard configuration such as environment variables or a configuration profile.
What each part does
modelis the endpoint name, not a model family name. This is the most common source of "not found" errors.messagesis a list of role and content pairs. Thesystemmessage sets behavior.usermessages carry the question.assistantmessages hold earlier replies when you pass conversation history.max_tokenscaps the length of the reply. If the answer is cut off mid-sentence, raise it.response.choices[0].message.contentholds the generated text.choicesis a list because the API can return several candidates, but you usually get one.
The response also carries a usage field with prompt and completion token counts. Those counts are what you will later track for cost and rate limits.
Multi-turn conversations
The model keeps no memory between calls. To continue a conversation, send the full history each time.
history = [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "What is a Delta table?"},
]
first = client.chat.completions.create(model=ENDPOINT_NAME, messages=history)
history.append({"role": "assistant", "content": first.choices[0].message.content})
history.append({"role": "user", "content": "How is it different from Parquet?"})
second = client.chat.completions.create(model=ENDPOINT_NAME, messages=history)
print(second.choices[0].message.content)Long histories cost more tokens and can exceed the model's context window, so trim or summarize old turns in real applications.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Endpoint not found | You passed a model name instead of the endpoint name | Copy the name from the Serving page |
| Permission denied | Your identity lacks `CAN QUERY` on the endpoint | Ask the endpoint owner to grant it |
| Rate limit error (429) | Too many requests or tokens for the endpoint | Retry with backoff, or lower request volume |
| Reply cut off | `max_tokens` is too low | Increase `max_tokens` |
| Authentication failure outside Databricks | No workspace host or credentials configured | Set the host and token, or use a profile |
Exercises
Exercise
Your call fails with "endpoint not found" even though you used the name of a model you know is available. What is the most likely mistake, and where do you find the correct value?
Show answer
The model argument must be the serving endpoint name, not the model's family name. Find the exact name on the Serving page of the workspace and pass it as model.
Exercise
A user asks "What is a Delta table?" and then "How is it different from Parquet?". The second answer ignores the first question. What change to the code fixes this?
Show answer
The model has no memory between calls. Append the first assistant reply and the new user message to the messages list, then send the whole list in the second call.