File readers (
definable.readers) extract content from files attached to agent messages for LLM processing. They are distinct from Knowledge readers (definable.knowledge.readers), which convert raw sources into Document objects for the RAG pipeline.Quick Example
Architecture
The readers module uses a layered design:- Parsers — stateless format converters:
bytes → List[ContentBlock]. Never do I/O. - ParserRegistry — priority-based mapping from format to parser.
- BaseReader — orchestrator:
File → bytes → detect format → parse → ReaderOutput. - Providers — AI-backed readers (e.g., MistralReader) that handle their own API I/O.
Built-in Parsers
| Parser | Formats | Dependency |
|---|---|---|
TextParser | .txt, .md, .csv, .json, .py, .js, +40 more | None |
PDFParser | .pdf | pypdf>=4.0.0 |
DocxParser | .docx | python-docx>=1.0.0 |
PptxParser | .pptx | python-pptx>=1.0.0 |
XlsxParser | .xlsx | openpyxl>=3.1.0 |
OdsParser | .ods | odfpy>=1.4.0 |
RtfParser | .rtf | striprtf>=0.0.26 |
HTMLParser | .html, .htm | None |
ImageParser | .png, .jpg, .gif, .bmp, .tiff, .webp, .svg, +more | None |
AudioParser | .mp3, .wav, .ogg, .flac, .m4a, .webm | None |
ContentBlock
Content extraction producesContentBlock objects — the multimodal output unit:
| Field | Type | Description |
|---|---|---|
content_type | str | "text", "image", "table", "audio", or "raw" |
content | str | bytes | Extracted content |
mime_type | str | None | MIME type of the content |
metadata | dict | Parser-specific metadata |
page_number | int | None | Page number (for paginated formats) |
| Method | Description |
|---|---|
as_text() | String representation of the content |
as_message_content() | OpenAI-format content part for message construction |
ReaderOutput
Every file read returns aReaderOutput:
| Field | Type | Description |
|---|---|---|
filename | str | Name of the file |
blocks | List[ContentBlock] | Extracted content blocks |
mime_type | str | None | Detected MIME type |
page_count | int | None | Number of pages (PDF, DOCX, PPTX) |
word_count | int | None | Word count of extracted text |
truncated | bool | Whether content was truncated |
error | str | None | Error message if reading failed |
metadata | dict | Additional metadata |
| Method | Description |
|---|---|
as_text(separator="\n\n") | Concatenated text from all blocks |
as_messages() | OpenAI-format message content list |
content | Property — backwards-compatible alias for as_text() |
BaseReader
The main orchestrator that resolves files to parsed content:Constructor
Reader configuration (file size limits, encoding, timeout).
Custom parser registry. When
None, a default registry with all available parsers is created.Methods
| Method | Description |
|---|---|
register(parser, priority=100) | Register a parser (returns self for chaining) |
get_parser(file) | Get the parser that handles a file, or None |
read(file) | Read a file synchronously |
aread(file) | Read a file asynchronously |
aread_all(files) | Read multiple files concurrently |
Agent Integration
Three ways to enable file readers on an agent:run(..., files=[...]), it automatically extracts content from each file and injects it into the prompt before calling the model.
ReaderConfig
Configure reader behavior:Creating a Custom Parser
SubclassBaseParser and implement three methods:
MistralReader
AI-backed OCR provider using the Mistral OCR API. Handles PDFs, DOCX, PPTX, and images with high-quality extraction.Mistral API key. Falls back to
MISTRAL_API_KEY env var.OCR model to use.
Include base64-encoded images in output blocks.
Fall back to local parsers for formats Mistral doesn’t support.
.pdf, .docx, .pptx, .png, .jpg, .jpeg, .avif
Requires
mistralai: pip install 'definable[mistral-ocr]'Parser Options
PDFParser
DocxParser
XlsxParser
OdsParser
ParserRegistry
The registry maps formats to parsers with priority-based dispatch:0. User-registered parsers default to priority 100. Higher priority wins when multiple parsers handle the same format.
ReadersConfig
Configure the readers integration on the agent viaAgentConfig:
Standalone Usage
UseBaseReader without an agent for file processing pipelines:
Stream Events
When using streaming, file reads emit events:| Event | Key Fields | Description |
|---|---|---|
FileReadStarted | file_count | File reading began |
FileReadCompleted | file_count, files_read, files_failed, duration_ms | File reading finished |