Sub topic 4
What the model returns when it wants a tool
This chapter assumes you can read JSON, know that chat APIs take a list of role-tagged messages, and have seen a tool definition with a name, a description, and a JSON Schema for its arguments. Here you will look at the model's side of the exchange: the reply that asks for a tool.
The model proposes, your code runs
A model cannot run your function. When it decides a tool would help, it returns a structured request instead of plain text. That request is part of an assistant message. Nothing happens until your code reads the request and acts on it.
The simplified, provider-neutral shape looks like this:
{
"role": "assistant",
"tool_calls": [
{"id": "call_1", "name": "get_weather", "arguments": {"city": "Oslo"}}
]
}Each tool call has three fields you rely on:
| Field | Meaning |
|---|---|
| id | A unique label for this call. You send it back with the result so the model can match the two. |
| name | Which tool the model wants. It should match one of the names you defined. |
| arguments | The input values, shaped like your JSON Schema. |
Providers differ in details. Some call the field input instead of arguments. Some return arguments as a JSON string that you must parse yourself. Some put tool calls in a content list next to text blocks. Check your provider's reference for the exact names.
Where the request sits in the conversation
The tool call is an assistant message, so it joins the message history like any other turn. Your code must append it before adding the result. The model needs to see its own request when it writes the final answer.
Reading the reply in code
A reply may contain text, tool calls, or both. Always test for tool calls first, and do not assume the text field is filled in.
def read_reply(reply):
calls = reply.get("tool_calls") or []
if not calls:
return {"kind": "final", "text": reply.get("content", "")}
return {"kind": "tools", "calls": calls}
example = {
"role": "assistant",
"tool_calls": [
{"id": "call_1", "name": "get_weather", "arguments": {"city": "Oslo"}}
],
}
print(read_reply(example))This prints a tools result with the one call. If tool_calls is missing or empty, the reply is the final answer.
Common errors
- Treating the reply as text. If you print only the content field, a tool request looks like an empty answer.
- Forgetting to keep the assistant message. The result you send later refers to an id the model can no longer see.
- Trusting the arguments. The model generates them, so they can be missing, mistyped, or invented. The model can also name a tool that does not exist. Later chapters cover validation.
- Assuming one call. A reply can hold several calls, each with its own id.
Exercise
A reply has no text but contains one entry in tool_calls. What should your code do next, and which field links the later result to this request?
Show answer
Append the assistant message to the history, run the requested tool in your own code, and send the result back as a tool message. The id field links the result to the request, so the result must carry the same id (sent as toolcallid in the simplified format).