# chunk.Chunk


A segment of text extracted from a document.


Usage

``` python
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](chunk.Chunk.md#raghilda.chunk.Chunk) objects from a document, the store embeds and indexes them, and retrieval returns them (as [RetrievedChunk](chunk.RetrievedChunk.md#raghilda.chunk.RetrievedChunk)s) 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: str | None`  
Optional heading context showing the document hierarchy at this chunk's position (e.g., the Markdown headings that apply).

`origin: str | None`  
Origin of the parent document this chunk belongs to.

`attributes: dict[str, Any] | None`  
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()](#from_any) | Coerce a chunk from another library into a raghilda [Chunk](chunk.Chunk.md#raghilda.chunk.Chunk). |

------------------------------------------------------------------------


### from_any()


Coerce a chunk from another library into a raghilda [Chunk](chunk.Chunk.md#raghilda.chunk.Chunk).


Usage

``` python
from_any(chunk)
```


This is the interoperability entry point for chunks produced outside raghilda, for example by [chonkie](https://github.com/chonkie-inc/chonkie) or your own chunker. It accepts either:

- an object satisfying the [ChunkLike](types.ChunkLike.md#raghilda.types.ChunkLike) protocol (it has `text`, `start_index`, and `end_index`; `char_count`, `context`, `origin`, and `attributes` are read when present), or
- an [IntoChunk](types.IntoChunk.md#raghilda.types.IntoChunk) object that exposes a [to_chunk()](types.IntoChunk.md#raghilda.types.IntoChunk.to_chunk) method returning a [Chunk](chunk.Chunk.md#raghilda.chunk.Chunk).


#### Parameters


`chunk: ChunkLike | IntoChunk`  
The chunk-like or [IntoChunk](types.IntoChunk.md#raghilda.types.IntoChunk) object to convert.


#### Returns


`Chunk`  
A raghilda [Chunk](chunk.Chunk.md#raghilda.chunk.Chunk) with the fields copied from the source object.


#### Raises


`TypeError`  
If [chunk](types.ChunkerLike.md#raghilda.types.ChunkerLike.chunk) is neither [ChunkLike](types.ChunkLike.md#raghilda.types.ChunkLike) nor [IntoChunk](types.IntoChunk.md#raghilda.types.IntoChunk), or if a [to_chunk()](types.IntoChunk.md#raghilda.types.IntoChunk.to_chunk) method does not return a [Chunk](chunk.Chunk.md#raghilda.chunk.Chunk).
