Sub topic 2
Citations and Annotations in Built-in Provider Tools
Assume you know basic Python, JSON request bodies, API keys, and function calling. Built-in tools run on the provider’s servers. Your application declares a tool, and the provider returns tool results in the response.
Citations and annotations
A web-search answer can include URL annotations. Display these citations with the generated text so users can inspect the sources.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "web_search"}],
input="What are the latest stable Python releases?"
)
print(response.output_text)
for item in response.output:
if item.type == "message":
for part in item.content:
for annotation in getattr(part, "annotations", []) or []:
if annotation.type == "url_citation":
print(annotation.title, annotation.url)Inspecting output items also helps you audit whether the model searched, what status the search had, and which sources supported the answer.
Controlling search and computation
Limit web search when possible. Domain filters restrict results to trusted sites. Location settings improve local queries. Usage limits cap the number of searches and reduce cost and latency. Exact option names differ between providers, so check the provider’s API reference.
Code execution lets the model write and run Python in a provider-managed sandbox. Use it for arithmetic, data transformation, and analysis instead of asking the model to calculate in text.
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "code_interpreter", "container": {"type": "auto"}}],
instructions="Use Python for arithmetic.",
input="What is the sum of the squares from 1 through 1000?"
)
print(response.output_text)A sandbox can receive input files and produce generated files. Keep sensitive files out unless the provider’s retention and access rules meet your requirements.
Preparing documents for file search
File search retrieves relevant passages from your own documents. First, upload documents into a vector store. A vector store indexes content so that semantically related text can be found.
store = client.vector_stores.create(name="handbook")
with open("handbook.pdf", "rb") as file:
client.vector_stores.files.upload_and_poll(
vector_store_id=store.id, file=file
)
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "file_search",
"vector_store_ids": [store.id],
"max_num_results": 3}],
input="How many vacation days do new employees receive?"
)
print(response.output_text)max_num_results tunes retrieval. Lower values reduce context and cost, but may omit useful evidence. File citations in the response let you identify the source document.
Exercise
Why should an application inspect response output items instead of displaying only the final text?
Show answer
Output items reveal tool calls, statuses, citations, and retrieved evidence, which supports debugging and auditing.