Sub topic 6
Sending the result back to the model
This chapter assumes you can read JSON, write basic Python, and know that chat APIs take a list of role-tagged messages. It also assumes you have seen a tool definition and the call the model returns when it wants one.
Why the result must go back
The model only proposes a tool call. Your code runs it. The model has no idea what happened until you tell it. To get a final answer, you append the result to the conversation and call the model again. The model then reads the whole history, including the result, and writes its reply.
The shape of a result message
A result is a message with the tool role. It carries two things: the id of the call it answers, and the content.
[
{"role": "user", "content": "What is the weather in Oslo?"},
{"role": "assistant", "tool_calls": [
{"id": "call_1", "name": "get_weather", "arguments": {"city": "Oslo"}}
]},
{"role": "tool", "tool_call_id": "call_1", "content": "{\"temperature\": 7, \"unit\": \"celsius\"}"},
{"role": "assistant", "content": "It is 7 degrees Celsius in Oslo."}
]The tool_call_id links the result to the request. This matters most when the model makes several calls in one turn, because the ids are the only way to tell which result belongs to which call.
Providers use different wrappers. Some use a tool role. Others put a tool_result block inside a user message. The idea is the same in all of them: same id, then content.
Rules to follow
- Append the assistant message that contains the tool calls before you append any results. Most providers reject a result with no matching request.
- Answer every call. If the model made three calls, send three results.
- Send the content as a string. Use
json.dumpsfor structured data. - Keep the original order of the history. Do not edit earlier messages.
Building the result in code
This function takes one call and its outcome, and returns the message to append. The outcome has the same shape as the dispatcher returns: is_error and content.
def tool_message(call, outcome):
return {
"role": "tool",
"tool_call_id": call["id"],
"content": outcome["content"],
}Inside the loop, you use it like this:
messages.append(reply)
for call in reply.get("tool_calls", []):
outcome = run_tool_call(call)
messages.append(tool_message(call, outcome))Some providers have an explicit error flag on the result. Where one exists, pass is_error through. Where it does not, put the error text in the content, as the next chapters show.
Common errors
| Symptom | Likely cause |
|---|---|
| API error about a missing tool result | You skipped one of several parallel calls |
| API error about an unknown call id | The id was mistyped, or the assistant message was not appended first |
| The model repeats the same call | The result was empty or unclear, so it tried again |
| The model invents data | The result was truncated or left out of the history |
Keep results small and clear
The model reads the result as text, and it counts against your context window and cost. Return only the fields the model needs. Trim large payloads and name fields plainly. Include units, such as "unit": "celsius", so the model does not guess.
Exercise
The model makes two tool calls in one turn, with ids calla and callb. What messages do you append before calling the model again?
Show answer
First the assistant message containing both tool calls. Then two tool messages, one with toolcallid calla and one with toolcallid callb, each holding that call's result as a string.
Exercise
Why does the model need the toolcallid in the result?
Show answer
The id links each result to the request it answers. Without it, the model cannot tell which result belongs to which call, especially when several calls happen in one turn.