Zoom In with PageIndex Agent
PageIndex Agent finds answers by freely choosing between outline navigation and page search. After it cites a region on a page, zoom into that region — a table, a chart, a figure — to extract precise details or ask follow-up questions.
- Ask a question and get a citation with a bounding box.
- Crop the cited region from the page image.
- Ask a focused follow-up on the cropped region.
Set up
pip install -U pageindex requests
import os, io
from pageindex import PageIndexClient
from PIL import Image
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"]
Get a citation with a bounding box
Ask a question that points to a table or chart:
answer = client.chat(
"Show me the revenue breakdown",
doc_id=doc_id,
citations="cite_with_boxes",
)
resolved = client.resolve_citations(answer)
c = resolved["citations"][0]
print(f"Page {c['page']}, box {c['box']}")
Crop the cited region
Fetch the page image and crop to the cited box:
import requests
url = client.get_page_image(c["doc_id"], c["page"])
img = Image.open(io.BytesIO(requests.get(url).content))
w, h = img.size
box = c["box"] # [left, top, right, bottom] as fractions
cropped = img.crop((box[0] * w, box[1] * h, box[2] * w, box[3] * h))
Ask a focused follow-up
Send the cropped region back to the agent for precise extraction:
import base64
buf = io.BytesIO()
cropped.save(buf, format="JPEG")
answer = client.chat(
"Extract all rows from this table as JSON.",
images=[base64.b64encode(buf.getvalue()).decode()],
)
print(answer)
Why zoom in
- Higher resolution — the model sees the table or chart at full detail, not as a small part of a full page.
- Fewer tokens — a cropped region is a smaller image.
- Focused extraction — ask precise questions about one table, one chart, one paragraph.
Result
The agent finds the page and the region. Zoom in extracts the detail. Combine them for precise extraction from long documents.
Previous: Visual citations | Back to Cookbook