Chunkers
A chunker splits files into the passages that are embedded and searched. Indexing uses the composite chunker by default. This page lists the chunkers, the MIME dispatch, the contract of a chunker and the PDF and OCR behaviour.
Indexing uses the composite chunker as its default dispatch path. Each chunker claims one or more MIME types: handles resolves a path's MIME type via the stdlib mimetypes.guess_type and tests membership in the chunker's declared set, so .md and .MD (both text/markdown) route to the same chunker. The registry is contributor-extensible, so you can register your own splitter for other formats or strategies without touching the core:
from duckdb_vectors import register_chunker
register_chunker("mychunker", lambda variant: MyChunker(variant))A chunker owns the folder walk, not just text splitting. The contract has three parts:
- Traversal —
chunk_folder(folder, recursive=True)walks the tree and yields chunks for every file the chunker claims, setting each chunk'ssourceto its path relative to the folder root. - Per-file handling — a chunker declares a
mimetypesfrozenset of the MIME types it claims (the markdown chunker claimstext/markdown), and the basehandles(path)matches a path's MIME type against it; a subclass may still overridehandleswhen MIME detection is ambiguous.chunk_file(path)reads one file and yields its chunks.chunk(text, source)stays available for direct text input. - Lifecycle hooks — three begin/end pairs with no-op defaults fire in a strict nesting order:
begin_indexing/end_indexingwrap a whole run,begin_folder/end_folderwrap each directory, andbegin_file/end_filewrap each handled file.end_indexingalways runs, even if indexing raises.
Custom chunkers subclass FilesystemChunker and implement only the mimetypes declaration and chunk_file; the base provides MIME-based matching, the recursive walk, the relative-source assignment, and the no-op hooks. The pdf chunker is the second consumer: it claims application/pdf, loads a local OCR engine once in begin_indexing, and emits one chunk per page from chunk_file (see PDF ingestion).
| Chunker | MIME type(s) | Use for |
|---|---|---|
| MarkdownChunker | text/markdown | heading-scoped sections with word-window splitting |
| PdfChunker | application/pdf | per-page text with local OCR for scanned pages |
| CompositeChunker | dispatches by MIME type | default; dispatches a mixed folder to the matching chunker per file in one pass |
The markdown chunker copies a page's front matter into each chunk's metadata, except the reserved sections key: a mapping from a section_path to metadata for that section's chunks only, merged into them key by key. An entry that matches no section is logged as a warning. The tagged retrieval guide shows how to index it as a tag.
The chunkers also add the reserved keys document.title, document.path and section.headings to the metadata of every chunk. They are defined in the tags reference.
Indexing a mixed folder in one pass
A real folder often mixes formats — some .md, some .pdf. The composite chunker is the default indexing path, so index_folder handles a mixed folder in a single pass with no selector: it dispatches each file to the first registered chunker that claims its MIME type, so markdown files go to the markdown chunker and PDFs go to the PDF chunker, with ids byte-identical to what each chunker would produce on its own.
from duckdb_vectors import VectorStore, index_folder
store = VectorStore(db_path="vectors.duckdb")
report = index_folder(store, "documents", recursive=True)The composite builds its members from every other registered chunker, so coverage grows with the registry. There are two ways to extend format coverage: open a PR against an existing chunker (for example to teach the PDF chunker a new OCR path), or register a new chunker with register_chunker for a format no chunker claims yet. A newly registered chunker is picked up by the composite automatically — no extra wiring.
PDF ingestion
The pdf chunker reads a PDF one page at a time. The composite default already dispatches .pdf files to it, but you can force PDF-only indexing with the code-level chunker= override:
from duckdb_vectors import PdfChunker, VectorStore, index_folder
store = VectorStore(db_path="vectors.duckdb")
report = index_folder(store, "pdfs", chunker=PdfChunker(), recursive=True)Each chunk is page-scoped: metadata["page"] carries the 1-based page number and the chunk id is {source}#p{N}:{pos}, so re-indexing upserts in place even as pages shift. A page's text is split into the same overlapping word windows the markdown chunker uses, and one chunk is emitted per window.
A born-digital page carries an embedded text layer that is extracted directly. A scanned page has no usable text layer, so when a page's embedded text is shorter than the min-chars threshold (default 16, tunable on the chunker) the page is rasterised and read with a local OCR engine. OCR runs locally through onnxruntime — the same runtime fastembed already uses — so there is no cloud call on the indexing path. RapidOCR may fetch its ONNX models once on first real use; bake them into your build image to keep indexing self-contained.
To use a different OCR engine, register your own chunker. For example, a Tesseract-backed variant through pytesseract is a drop-in alternative you can register yourself:
from duckdb_vectors import register_chunker
register_chunker("pdf-tesseract", lambda variant: MyTesseractPdfChunker())See also
- Tags reference: the reserved metadata keys that the chunkers add.
- Embedders reference: the other half of indexing.