Skip to content

Sending results back

Read · 1 of 3

The reply the model expects

After a tool_use, the model is waiting. You answer with a user message whose content is a list of tool_result blocks: {"type": "tool_result", "tool_use_id": ..., "content": ...}.

The tool_use_id must match the id the model gave. If it asked for three tools at once, all three results go in the same message. Miss one and the API rejects the request.

Read this lesson as text

Every tool_use gets exactly one tool_result with the same id, all in one user message.

The reply the model expects

After a tool_use, the model is waiting. You answer with a user message whose content is a list of tool_result blocks: {"type": "tool_result", "tool_use_id": ..., "content": ...}.

The tool_use_id must match the id the model gave. If it asked for three tools at once, all three results go in the same message. Miss one and the API rejects the request.

Wrong key

calls = [{"type": "tool_use", "id": "call_1", "name": "add", "input": {"a": 2, "b": 3}}]
results = []
for call in calls:
    value = call["input"]["a"] + call["input"]["b"]
    results.append({"type": "tool_result", "tool_use_id": call["tool_use_id"], "content": str(value)})
print(results)

Which line crashes?

Show the answer

Line 5. The tool_use block's id is under id. tool_use_id is the name of the field in the *result*. Line 5 should read call["id"].

Exercise: Answer every call

Input: one line of JSON, the model's content blocks (some text, some tool_use with tool add or mul and inputs a, b).

Run every tool and print the user message to send back, as one line of json.dumps: {"role": "user", "content": [tool_result blocks in the same order]}. The result content is the number as text.