Session 5· 12· 10 min

Read a Tool Call

What you'll learn
  • Access response.choices[0].message.tool_calls
  • Parse the function name and JSON arguments
  • Understand that the model proposes — your code executes

When the model decides to use a tool, the assistant message has no content — it has tool_calls instead. Each tool_call is a request to run a function with specific arguments.

12_read_single_tool_call.py
msg = response.choices[0].message

if msg.tool_calls:                                             ①
    for call in msg.tool_calls:                                 ②
        print("Function:", call.function.name)                  ③
        args = json.loads(call.function.arguments)              ④
        print("Arguments:", args)
else:
    print("No tool called:", msg.content)
Always check if tool_calls exists — sometimes the model just answers in plain text.
tool_calls is a LIST. Parallel calls (lesson 15) put multiple in one response.
call.function.name is the tool the model wants to run.
call.function.arguments is a JSON STRING. You still have to json.loads() it.
$ python 12_read_single_tool_call.py
Code Check
What is the type of call.function.arguments?
Recap — what you just learned
  • tool_calls lives on the assistant message when the model wants to call something
  • It is a LIST of tool_call objects — one per function request
  • call.function.arguments is a JSON string; always json.loads() it
Next up: 13 — Execute Tool