Document API Reference

Every class, method and parameter in blazechunk.loaders.

DocumentChunker

Turns documents into structure-aware chunks. It is deliberately not a BaseChunker subclass: text chunkers map str to chunks, while this maps files to a ChunkedDocument.

DocumentChunker(
    chunker=None,
    *,
    table_chunker=TableChunker(chunk_size=3),
    code_chunker=CodeChunker(chunk_size=2048),
    respect_headings=True,
    merge_small_segments=True,
    min_chunk_size=256,
    max_concurrency=8,
)
ParameterTypeDefaultDescription
chunkerBaseChunker | NoneRecursiveChunker(chunk_size=2048)Chunker for prose; positional or keyword.
table_chunkerBaseChunker | NoneTableChunker(chunk_size=3)Chunker for tables; None routes tables to chunker.
code_chunkerBaseChunker | NoneCodeChunker(chunk_size=2048)Chunker for fenced code; None routes code to chunker.
respect_headingsboolTruePrevent merges sideways across heading boundaries.
merge_small_segmentsboolTrueCombine undersized neighbours.
min_chunk_sizeint256Minimum segment size before looking for a merge partner.
max_concurrencyint | None8Default async document conversion bound.
Note
Passing None explicitly for table or code routing disables that route; omitting the argument uses its default. min_chunk_size < 0 and max_concurrency < 1 raise ValueError.

Methods

loader.chunk(source, *, format=None) -> ChunkedDocument
loader.chunk_batch(sources, *, format=None, on_error="raise") -> list[ChunkedDocument]
loader.chunk_markdown(markdown, *, format="md") -> ChunkedDocument
loader(source, *, format=None) -> ChunkedDocument
  • source accepts str, os.PathLike[str], bytes, or bytearray.
  • format is required for raw CSV bytes and invalid names raise ValueError.
  • chunk_markdown needs no anydoc install and preserves the same structural routing.

on_error

ValueBehaviour
"raise"Propagate the first DocumentError.
"skip"Leave failing documents out; the list is shorter.
"collect"Keep positions aligned with the DocumentError in the failing slot.
results = loader.chunk_batch(paths, on_error="collect")

for path, result in zip(paths, results):
    if isinstance(result, DocumentError):
        log.warning("skipped %s: %s", path, result)
        continue
    index(result.chunks)

ChunkedDocument

The result of chunking one document. Markdown returns alongside chunks because offsets index into it.

AttributeTypeDescription
chunkslist[DocumentChunk]Chunks in document order.
markdownstrConverted document; every offset indexes into this.
formatstrFormat read from, such as pdf.
warningslist[str]Non-fatal notes.
markdown_bytesbytesProperty containing Markdown as UTF-8.

Supports len(), iteration, and integer indexing.

DocumentChunk

AttributeTypeDescription
textstrChunk text.
md_start / md_endintUTF-8 byte offsets into ChunkedDocument.markdown.
kindstrprose, table, code, list, quote, heading or rule.
heading_pathtuple[str, ...]Enclosing headings, outermost first.
langstr | NoneFenced code info string, otherwise None.
source_formatstrSource document format.
token_countintTokens counted by the producing chunker.
is_exactboolWhether text is a verbatim Markdown slice.
Tip
Offsets are bytes, not code points. Slice result.markdown_bytes or multibyte characters can misalign the result.

Errors

DocumentError
├── UnsupportedDocument
│   └── ScannedDocumentError
├── MalformedDocument      (.part)
├── EncryptedDocument
└── DocumentResourceLimit  (.limit)
ExceptionRaised when
DocumentErrorBase class for every load failure.
UnsupportedDocumentUnknown format or no extractable text.
ScannedDocumentErrorPDF has no text layer; subclasses UnsupportedDocument.
MalformedDocumentStructurally unusable or missing a required part.
EncryptedDocumentEncrypted or password-protected.
DocumentResourceLimitParser safety limit crossed.
OSErrorFile could not be read; deliberately not wrapped.

Constants

NameContents
SUPPORTED_FORMATSfrozenset of csv, doc, docx, epub, odp, ods, odt, pdf, ppt, pptx, rtf, xlsx.
PASSTHROUGH_FORMATSfrozenset of md, markdown, txt, text.