Sub topic 6
Citations and annotations: showing your sources
This chapter assumes you can call the Responses API, read a response object, and have already enabled the web search tool. Here you will learn how the provider attaches source information to answer text, and how to show it to users.
What an annotation is
When web search runs, the final answer text comes with metadata. In the OpenAI Responses API, this metadata is a list called annotations on each text part of a message output item. Each annotation points at a span of the text and names the source behind it.
For web search, the annotation type is url_citation. It carries a title, a url, and start_index and end_index values. The two indexes mark the range of characters in the text that the source supports.
Anthropic does this differently. A text block carries a citations list, and each entry holds the page URL, the title, and a cited_text snippet. The idea is the same, but the field names differ.
Reading citations
Walk the output items, find the message, and read the annotations on each part. This is the same loop you may have used to inspect tool calls.
for item in response.output:
if 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)The getattr call with a default guards against parts that have no annotations, so the loop does not fail on them.
Showing sources to users
A common task is to turn the indexes into numbered footnotes. Insert markers from the end of the text backward. If you insert from the start, each insertion shifts the later indexes and the markers land in the wrong place.
def add_footnotes(text, annotations):
cites = [a for a in annotations if a.type == "url_citation"]
for n, ann in reversed(list(enumerate(cites, start=1))):
text = text[:ann.end_index] + f" [{n}]" + text[ann.end_index:]
sources = [f"[{n}] {a.title}: {a.url}" for n, a in enumerate(cites, start=1)]
return text + "\n\n" + "\n".join(sources)Common errors
- Dropping the citations.
response.output_textreturns only the plain text. If you print it alone, your users never see the sources. - Assuming one citation per claim. A sentence may have several annotations, and some text may have none. Do not treat missing citations as proof that a claim is wrong, or present cited claims as verified.
- Editing text before using indexes. The indexes refer to the original string. Change the text first and they no longer line up.
- Trusting the link blindly. A citation shows where the model found a claim. It does not prove that the page is accurate. Show the source so the reader can judge it.
Exercise
Why should you insert footnote markers into the answer text starting from the last annotation and moving backward?
Show answer
Each annotation's startindex and endindex refer to positions in the original text. Inserting a marker shifts every later position by the marker's length. Working from the end means earlier positions stay valid while you insert.