Sub topic 5
Web search: grounding answers in current information
This chapter assumes you can call the Responses API with an API key, read JSON response bodies, and have a basic idea of tool calling. It covers the web search tool: why a model needs it, how to turn it on, and what to check in the result.
Why a model needs web search
A model only knows what was in its training data. It cannot tell you the latest release of a library or today's news. If you ask, it may guess, and the guess can sound confident and still be wrong.
Web search is a built-in tool. The provider runs the search on its servers, reads the results, and writes a final answer grounded in them. Your code makes one request and gets one response. You do not write a search function or call a search API yourself.
Turning it on
You declare the tool in the tools list. The model decides whether the question needs a search.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "web_search"}],
input="What are the most recent stable releases of Python?",
)
print(response.output_text)output_text is a shortcut that joins the text parts of the answer. The answer should now reflect pages the provider fetched, not only the model's training data.
What happens during the request
The search may run more than once if the first results are not enough. All of it happens before you receive anything.
Reading the search calls and citations
The response output is a list of items. A search shows up as a web_search_call item. The answer is a message item whose text parts carry annotations. A url_citation annotation links part of the answer to a source page.
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)Use this loop for two jobs:
- Debugging. If there is no
web_search_callitem, the model answered from memory. That explains stale answers. - Showing sources. Display the title and URL next to the answer so users can check it.
Common errors
- Assuming a search happened. The model chooses whether to search. For questions that need fresh facts, say so in
instructions, or force the tool withtool_choice(covered in a later chapter). - Trusting citations blindly. A citation shows where the text came from. It does not prove the page is correct.
- Dropping the annotations. If you print only
output_text, you lose the sources. - Treating pages as trusted. Web content is untrusted input and can contain text that tries to steer the model. The security chapter covers this.
Another provider, the same idea
Anthropic offers the same capability as a versioned server tool. max_uses caps the searches in one request.
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 3,
}
],
messages=[
{"role": "user", "content": "What are the latest developments in solid-state batteries?"}
],
)
for block in message.content:
if block.type == "text":
print(block.text)Here the response is a list of content blocks, and you loop over the text blocks. Tool names, versions, and response shapes differ between providers, so check each provider's documentation.
Exercise
Your app uses web search, but it keeps returning an outdated answer. Which part of the response do you inspect first, and what does it tell you?
Show answer
Inspect the output items for a web_search_call. If none is present, the model answered from its training data without searching. You can then add an instruction to search for current facts, or force the tool with tool_choice.