Skip to main content

agrag.chunking.parent_child.ParentChildChunker

Bases: Chunker

Cuts a document into parent chunks and cuts each parent into child chunks.

Extraction runs on the parents, which have level=1. The children have level=0 and a parent_id, and only the children are embedded and searched. A search hit returns the child with its parent attached.

A child never crosses a parent boundary, because the child strategy cuts each parent alone. Text of a parent that the child strategy leaves out gets a child of its own, so all non-blank text can be found by search. A parent that the child strategy leaves without any piece gets one child with the span of the parent.

The chunker returns all parents, then all children. Indexes count from 0 at each level.

Attributes:

  • parent (SerializeAsAny[SpanChunker]) – The strategy that cuts the document into parents.
  • child (SerializeAsAny[SpanChunker]) – The strategy that cuts each parent into children.

Functions:

  • chunk – Split a document into chunks.
  • fingerprint – Return the hash of settings(), 16 hex characters.
  • model_copy – Copy the chunker, validating any changed setting.
  • model_post_init – Compute the fingerprint once, after the settings are validated.
  • settings – Return the strategy name and every setting as JSON-safe data.

child​

child: SerializeAsAny[SpanChunker] = Field(default_factory=_default_child)

chunk​

chunk(document:Document) -> list[Chunk]

Split a document into chunks.

Every chunk has non-empty text, indexes run from 0 without gaps, and a chunk with text provenance has text equal to document.text at its offsets.

Parameters:

  • document (Document) – The document to split.

Returns:

  • list[Chunk] – The chunks, in document order, each with chunker and chunker_hash set. A strategy that sets chunker itself keeps its value.

Raises:

  • ChunkingError – The strategy returned chunks that break the contract.

fingerprint​

fingerprint() -> str

Return the hash of settings(), 16 hex characters.

model_config​

model_config = ConfigDict(frozen=True, extra='forbid')

model_copy​

model_copy(*, update:Mapping[str, Any] | None = None, deep:bool = False) -> Self

Copy the chunker, validating any changed setting.

A plain copy keeps the fingerprint and the splitter of the original. A copy with changes is built again from its configuration.

model_post_init​

model_post_init(context:Any) -> None

Compute the fingerprint once, after the settings are validated.

parent​

parent: SerializeAsAny[SpanChunker] = Field(default_factory=_default_parent)

settings​

settings() -> dict[str, Any]

Return the strategy name and every setting as JSON-safe data.

A setting that is itself a chunker appears as that chunker's settings.

strategy​

strategy: str

The strategy name, "parent-child".