Tools · practice
Read a Tool Call from the Response
Your descriptions made the scripted model reply with finish_reason "tool_calls". This lesson reads what it asked for, and turns each request into a plain dictionary that the rest of the chapter can work with.
Where the calls are in the reply
A reply that asks for tools has the same outer shape as any other: response.choices[0].message is the message, and response.choices[0].finish_reason is "tool_calls". Two attributes of the message are different from a text reply:
message = response.choices[0].message
message.content # None: there is no answer yet
message.tool_calls # a list of calls, here with one item
call = message.tool_calls[0]
call.id # "call_1_1"
call.function.name # "read_note"
call.function.arguments # '{"note_id": "03-lists"}'
Each call has three things you need:
id, a string that names this one call. The service makes it up. The result you send back in Lesson 5 must carry the same id, so the model can tell which result answers which call.function.name, the tool the model wants, as a string.function.arguments, the arguments as a JSON string, not a dictionary. The model wrote that text, and nothing guarantees yet that it is valid JSON.
On a text reply it is the other way round: content holds the answer and tool_calls is None. Some services send an empty list instead of None, and if not message.tool_calls: is true for both.
Plain dictionaries
read_tool_calls(response) copies what matters out of the client’s objects into dictionaries:
[{"id": "call_1_1", "name": "read_note", "arguments": '{"note_id": "03-lists"}'}]
Dictionaries are easy to print, to compare in a check, and to build by hand. Lesson 6’s demonstration uses hand-built calls in exactly this form, and the checks for later lessons pass calls like this to your functions without a client anywhere.
Keep arguments as the string. Decoding it is where a model’s mistake shows up, as text that is not JSON or JSON that is not an object. Lesson 6 handles that in one place, run_tool, where it can turn the mistake into a message for the model. Decoding here would make read_tool_calls crash on the same text.
Build read_tool_calls
read_tool_calls(response) takes a reply from the client:
Read
response.choices[0].message.tool_calls.When it is
Noneor empty, return[].Otherwise, loop over the calls and return a new list with one dictionary per call, in the same order:
{"id": call.id, "name": call.function.name, "arguments": call.function.arguments}.
Run python tools.py. The demonstration prints the calls in the reply to the Lesson 1 question, then sends Which notes are there? with your tools, a question the scripted model answers with a list_notes call:
The tool calls in the reply:
[{'id': 'call_1_1', 'name': 'read_note', 'arguments': '{"note_id": "03-lists"}'}]
The tool calls for Which notes are there?:
[{'id': 'call_1_1', 'name': 'list_notes', 'arguments': '{}'}]
Run stopped at the next function to build: TOOLS is written in Lesson 4.
Both calls have the id call_1_1, because each is the first call of the first tool round in its own conversation. The list_notes call still has arguments: the text '{}', an empty JSON object, because a tool with no arguments still gets one.
What is response.choices[0].message.tool_calls[0].function.arguments in a tool-call reply?
Your code prints response.choices[0].message.content for a reply whose finish_reason is "tool_calls". What does it print?
Task
In tools.py, replace the unfinished body of read_tool_calls(response).
Read
response.choices[0].message.tool_calls.When it is
Noneor an empty list, return[].Otherwise return a new list with one dictionary per call, in the same order:
{"id": call.id, "name": call.function.name, "arguments": call.function.arguments}.
Keep arguments exactly as the string the model sent: do not decode it. Run python tools.py to see the calls for two questions, then check your work.