Document Q&A
Load a document and ask questions about it using the Agent API. This example shows how to read document content, combine it with web search for enriched answers, and build a multi-turn Q&A session over document content.Features
- Load documents (text, CSV, JSON, markdown) and pass content to the Agent API
- Ask questions grounded in the document content
- Optionally combine with
web_searchfor context enrichment - Multi-turn conversation to drill into specific sections
- Structured output extraction from document content
Pass document content directly in the
input parameter of the Agent API. For text-based formats, read the file and include it in the prompt. Combine with web_search for context enrichment beyond the document.Installation
Usage
Save the full code below todoc_qa.py and run:
Full Code
Example Output
Structured Data Extraction from Documents
Extract specific fields from a document into a typed JSON structure:Limitations
- Very large documents are truncated. The default limit is 50,000 characters (~12,500 tokens).
- Text-based formats (.txt, .csv, .md, .json) work best. For PDFs, use a library like
pdfplumberorPyPDF2to extract text first. - Conversation history for multi-turn sessions does not re-read the file — the document content is included in the first message.