ChunkedDocument¶
New in v2.0
- class ChunkedDocument¶
Retrieval-ready chunks and document-level structure returned by
Document.to_chunks(). This object implements the sequence protocol over its chunks: uselen(cd), integer indexing, iteration, or slicing. Integer indexing returns a chunk; slicing returns a list of chunks. Chunk ids arec{n}in reading order, socd.get("c3")addresses the same chunk ascd[3].The object also keeps layout elements and table, figure, and section views. These views are linked to their owning chunks through ids. Element ids have the form
p{page}.b{box}(1-based page number and 0-based box index). Thehierarchyproperty represents sections as a tree; its root has level 0.- text¶
All chunk text joined in reading order with blank lines between chunks. The value is computed lazily and cached.
- elements¶
A list containing every parsed layout box, including headers and footers excluded from chunk text. Each element has an id, page and box indices, box class, bounding box, canonical text, and a flag indicating whether it is a header or footer.
- tables¶
A list of table views. Each table is linked to its owning chunk when one exists, and provides its id, source element id, page, bounding box, canonical text, optional caption and section id, and token count. The canonical text is Markdown or HTML according to the table output mode.
- figures¶
A list of figure and formula views, linked to their owning chunk when one exists. Views include source location, extracted text when available, caption and section links, and image data when it was extracted.
- sections¶
A list of section views with title, heading level, page range, path, child chunk ids, token count, and section body text.
- hierarchy¶
Sections arranged as a tree of section nodes. The root node has level 0 and no section id.
- params¶
A read-only mapping of the parameters used to create this object.
- diagnostics¶
A dictionary of extraction and ingestion facts: chunk, element, table, figure, section, and page counts; pages without chunks; reasons for an empty result; figures without extracted text; degenerate tables; and the number of excluded header/footer units. This reports facts only; consumers decide how to act on them.
- get(id: str, default=...)¶
Return a chunk, table, figure, section, or element by its public id:
c{n},t{n},f{n},s{n}, orp{page}.b{box}. RaisesKeyErrorif the id is unknown and no default is provided; otherwise returnsdefaultfor an unknown id.
- to_dicts(*, include_tagged: bool = True) list[dict]¶
Return the chunks as flat, JSON-safe dictionaries. Each dictionary contains
id,text,content_hash, andmetadata; it also containstagged_contentunlessinclude_tagged=False.
- to_json(*, include_tagged: bool = True, **json_kwargs) str¶
Serialize the result of
to_dicts()as JSON.include_taggedcontrols whether tagged content is included; additional keyword arguments are passed tojson.dumps().
- reassemble_chunks(**params) ChunkedDocument¶
Create a new
ChunkedDocumentby assembling chunks again from the retained parsed units; the document does not need to be parsed again. Accepts onlymax_tokens,min_tokens,breakpoint_threshold,table_mode,merge_small_chunks, andrespect_section_starts. Other chunking parameters and parse options raiseValueErrorbecause they require a newDocument.to_chunks()call. The original object is unchanged.
- to_langchain_documents(*, doc_id: str | None = None)¶
Return the chunks as LangChain
Documentobjects. Requires the optionallangchain-corepackage. Ifdoc_idis provided, it is prefixed to each exported chunk id to make ids unique across documents.
- to_llama_nodes(*, doc_id: str | None = None)¶
Return the chunks as LlamaIndex
TextNodeobjects. Requires the optionalllama-index-corepackage. Ifdoc_idis provided, it is prefixed to each exported chunk id to make ids unique across documents; the original chunk id is retained in node metadata.
