Skip to main content

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_search for 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 to doc_qa.py and run:
For interactive mode:

Full Code

Example Output

With web search enrichment:

Structured Data Extraction from Documents

Extract specific fields from a document into a typed JSON structure:
Combine document content with structured outputs to build reliable document processing pipelines. The JSON schema ensures consistent output regardless of document format variations.
Very large documents are truncated to stay within token limits. For large files, consider splitting into sections and processing each separately.

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 pdfplumber or PyPDF2 to extract text first.
  • Conversation history for multi-turn sessions does not re-read the file — the document content is included in the first message.