Sub topic 4
Reading Tool-Call Items in the Response
This chapter assumes you can call the Responses API with an API key and read JSON. It also assumes you know the basic idea of function calling. Here you learn to read the list of items that a built-in tool request returns, so you can debug and audit what the provider did.
Output is a list of items
With a function tool, your code sees a tool call and has to run it. With a built-in tool, the provider runs the tool on its side. The response still records what happened. It does this in response.output, a list of typed items in the order they were produced.
response.output_text is a convenience property. It joins the text of the final message. It hides everything else, so use response.output when you need to know how the answer was produced.
Common item types
| Item type | What it tells you |
|---|---|
| `web_search_call` | The provider searched the web. It has an `id` and a `status`. |
| `code_interpreter_call` | The provider ran model-written code in its sandbox. |
| `file_search_call` | The provider queried your vector stores. |
| `message` | The final assistant text, plus annotations such as citations. |
A typical web search response has a web_search_call item first and a message item last. The tool call comes before the text that depends on it.
Walking the list
This code loops over the items and branches on type. It assumes response came from a request with the web_search tool enabled.
for item in response.output:
if item.type == "web_search_call":
print("search call:", item.status)
elif item.type == "message":
for part in item.content:
for ann in getattr(part, "annotations", []) or []:
if ann.type == "url_citation":
print(ann.title, ann.url)Two details matter here. First, message content is itself a list of parts, so you need a nested loop. Second, annotations may be missing or empty, so the code uses getattr with a fallback. Without it, a response that has no citations raises an error.
Common errors
- Reading only
output_text. You cannot tell whether the model used the tool or answered from memory. Check for a tool-call item. - Assuming one item per request. The model may search several times, so expect several
web_search_callitems. - Indexing by position.
response.output[0]is not always the message. Branch ontypeinstead. - Ignoring
status. A call with a status other thancompletedmeans the answer may rest on missing results. - Treating unknown types as errors. Providers add item types over time. Skip the ones you do not handle.
Why this matters
Logging the tool-call items gives you an audit trail. You can count searches for cost control, confirm that the model used file search before answering a handbook question, and show users the sources behind a claim.
Exercise
Your code prints response.output[0].content and fails with an AttributeError on a web search request. What is the likely cause, and how do you fix it?
Show answer
The first item is a web_search_call, which has no content field. The message item comes later. Loop over response.output and check item.type == "message" before reading content.