chunk.Chunk
A segment of text extracted from a document.
Usage
chunk.Chunk(
text,
start_index,
end_index,
char_count,
context=None,
origin=None,
attributes=None
)Chunks are the fundamental unit for retrieval in RAG applications. Each chunk contains the text content along with positional information that allows mapping back to the original document. A chunker produces Chunk objects from a document, the store embeds and indexes them, and retrieval returns them (as RetrievedChunks) ranked against a query.
Attributes
text: str-
The actual text content of the chunk.
start_index: int-
Character position where this chunk begins in the source document.
end_index: int-
Character position where this chunk ends in the source document.
char_count: int-
Number of characters in this chunk.
context: Optional[str]-
Optional heading context showing the document hierarchy at this chunk’s position (e.g., the Markdown headings that apply).
origin: Optional[str]-
Origin of the parent document this chunk belongs to.
attributes: Optional[dict[str, Any]]- Optional user-defined attributes associated with the chunk. These attributes can be used for retrieval filtering/scoping and downstream prompt/context construction.
Methods
| Name | Description |
|---|---|
| from_any() | Coerce a chunk from another library into a raghilda Chunk. |
from_any()
Coerce a chunk from another library into a raghilda Chunk.
Usage
from_any(chunk)This is the interoperability entry point for chunks produced outside raghilda, for example by chonkie or your own chunker. It accepts either:
- an object satisfying the ChunkLike protocol (it has
text,start_index, andend_index;char_count,context,origin, andattributesare read when present), or - an IntoChunk object that exposes a to_chunk() method returning a Chunk.
Parameters
chunk: Union[ChunkLike, IntoChunk]- The chunk-like or IntoChunk object to convert.
Returns
Chunk- A raghilda Chunk with the fields copied from the source object.
Raises
TypeError- If chunk is neither ChunkLike nor IntoChunk, or if a to_chunk() method does not return a Chunk.