Visual Citations with PageIndex Agent

PageIndex Agent searches your documents by navigating the outline tree and searching pages — it picks the strategy on its own. It can cite not just the page an answer came from, but the exact region on the page. Ask a question, get a bounding box, and draw it on the page image.

  1. Ask with citations="cite_with_boxes".
  2. Resolve the citations to get page IDs and boxes.
  3. Draw the box on the page image.

Set up

pip install -U pageindex requests
import os
from pageindex import PageIndexClient

os.environ["PAGEINDEX_API_KEY"] = "Your PageIndex API key here"
os.environ["OPENAI_API_KEY"] = "Your OpenAI API key here"

client = PageIndexClient(index="cloud", chat="gpt-6.1-terra")
doc_id = client.submit_document("apple-10k-2024.pdf", wait=True)["doc_id"]

Ask with citations

With citations="cite_with_boxes", the agent cites its source with a <cite> tag that includes the page number and a bounding box:

answer = client.chat(
    "What was Apple's total revenue in fiscal year 2024?",
    doc_id=doc_id,
    citations="cite_with_boxes",
)
print(answer)
Apple's total net revenue for fiscal year 2024 was $391.0 billion. <cite doc="apple-10k-2024.pdf" page="27" box="0.04,0.13,0.84,0.17"/>

Resolve the citations

resolve_citations() turns the <cite> tags into structured data with document IDs:

resolved = client.resolve_citations(answer)
print(resolved["citations"][0])
{'anchor': 'pageindex-citation-01', 'index': 1, 'document': 'apple-10k-2024.pdf',
 'doc_id': '...', 'page': 27, 'box': [0.04, 0.13, 0.84, 0.17]}

box is [left, top, right, bottom] as fractions of the page's width and height.

Draw the box on the page

Fetch the page image and highlight the cited region with highlight_region():

import requests
from pageindex import highlight_region

c = resolved["citations"][0]
url = client.get_page_image(c["doc_id"], c["page"])
image = highlight_region(requests.get(url).content, c["box"], scale=1)
image.save("cited-page.png")

The saved image shows the page with the evidence highlighted — a row in a table, a line in a paragraph, a bar in a chart.

Result

The answer traces back to a region on the page. Users can verify the answer by looking at the highlighted source.

Previous: Multi-document search | Back to Cookbook