Tools · practice
Several Tool Calls in One Reply
Some questions need more than one tool. Lesson 6’s demonstration ended with one: Compare the notes on lists and dictionaries. The model asked for two notes in a single reply, with two calls in tool_calls. This is often called parallel tool calls: the model asks for several tools at once instead of one per round, which saves a whole request and reply for each extra tool.
One assistant message, one result per call
The calls from Lesson 6’s demonstration were:
[
{"id": "call_1_1", "name": "read_note", "arguments": '{"note_id": "03-lists"}'},
{"id": "call_1_2", "name": "read_note", "arguments": '{"note_id": "06-dictionaries"}'},
]
The second request carries one assistant message with both calls, which your assistant_turn already builds from the whole list, followed by one tool message per call:
[
{"role": "assistant", "content": None, "tool_calls": [...both calls...]},
{"role": "tool", "tool_call_id": "call_1_1", "content": "# Lists\n\n..."},
{"role": "tool", "tool_call_id": "call_1_2", "content": "# Dictionaries\n\n..."},
]
Every call needs its result. A model that asked for two things and heard back about one cannot tell what happened to the other, and real services reject a request where a call has no tool message. The ids tell the model which result is which, so they must match. Keep the results in the order of the calls as well: it costs nothing and makes the conversation easy to read.
“Parallel” describes the request, not your program. Your program can run the calls one after another in a loop, and here it does.
One bad call does not stop the others
Since Lesson 6, run_tool returns an error string for a bad call instead of raising. That matters more with several calls. If the model asked for 03-lists and 99-magic together, the first result is the note and the second is Error: There is no note called 99-magic. .... Both go back, and the model still gets the note it could have. A run_tool that raised would lose both.
Build tool_messages
tool_messages(calls, tools) runs every call and returns the tool messages:
Start with an empty list.
Loop over
calls. For each call, get the result withrun_tool(call, tools)and appendtool_result(call, result).Return the list.
Use your own run_tool and tool_result; together they already handle a single call, so tool_messages only needs the loop. Pass tools through, as run_tool does.
Run it
Run python tools.py. The demonstration runs the two calls with your tool_messages, prints each result’s id and length, then sends the assistant message and both results back:
Two calls in one reply:
[{'id': 'call_1_1', 'name': 'read_note', 'arguments': '{"note_id": "03-lists"}'}, {'id': 'call_1_2', 'name': 'read_note', 'arguments': '{"note_id": "06-dictionaries"}'}]
call_1_1: 1226 characters
call_1_2: 1197 characters
Answer: Lists keep items in order and you reach them by position, such as scores[0]. Dictionaries map keys to values and you reach a value by its key, such as ages["Ana"]. Both are mutable, and both have a copy() method for an independent copy. (03-lists, 06-dictionaries)
The study assistant with tools:
You: What do the notes say about copying a list?
Run stopped at the next function to build: answer_with_tools is built in Lesson 8.
The answer names both notes. Two calls, two results, one more request.
A reply asks for two tools. How many messages does your program add to the conversation before the next request?
The model asks for 03-lists and 99-magic in one reply. What should tool_messages return?
Task
In tools.py, replace the unfinished body of tool_messages(calls, tools).
Loop over
calls, in order.For each call, get its result with
run_tool(call, tools)and addtool_result(call, result)to a new list.Return the list: one tool message per call, in the order of the calls.
A call that fails still gets its error result, and the calls after it still run. Run python tools.py to see two notes answered in one round, then check your work.