Document Q&A
Ask questions about a document using the Agent API. Send a native document input, or read text locally for the text-processing workflow below.Send a native document input
Install the Perplexity SDK, setPERPLEXITY_API_KEY, and save a UTF-8 text document as report.txt in your working directory. These examples send the file as base64 in an input_file content part inside a user message. No file upload operation or file ID is needed.
The SDK examples were verified with Python perplexityai==0.43.8 and TypeScript @perplexity-ai/perplexity_ai@0.38.8.
status: "completed" and a message containing the document summary. With raw HTTP, inspect output[] for a message and its output_text content parts.
For a remote document, use file_url instead of file_data. Send exactly one of these fields. Format, source, and model restrictions still apply; see Working with Files before switching from a text file to PDF or Office documents.
Read text locally
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
The workflow below reads text locally and includes it in the prompt. It does not parse binary PDF or Office files. Use the native document input above for supported document formats, or combine extracted text with
web_search for context 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.