Skip to main content

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, set PERPLEXITY_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.
A successful response has 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_search for 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 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.