windlass.interfaces.chunker¶
chunker
¶
The chunker interface.
Chunking is the single highest-leverage decision in a RAG system: it determines what the retriever can find and what the model gets to read. Windlass therefore treats it as a first-class, swappable strategy rather than a helper function.
Implementers override one method, :meth:Chunker.split_text, which works on a
plain string. The base class handles document iteration, metadata propagation,
offset tracking and id assignment — so a custom chunker is genuinely a dozen
lines.
Example
from windlass.providers.chunkers.recursive import RecursiveChunker chunker = RecursiveChunker(chunk_size=60, overlap=0, min_chunk_size=0) len(chunker.chunk_text("word " * 60)) >= 2 True
Chunker
¶
Chunker(
*,
chunk_size: int = 1000,
overlap: int | None = None,
min_chunk_size: int = 50,
keep_separator: bool = True,
name: str | None = None,
**config: Any
)
Bases: Component
Abstract text splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_size
|
int
|
Target size of each chunk, measured in :attr: |
1000
|
overlap
|
int | None
|
How much each chunk repeats from the previous one. Overlap
keeps a fact that straddles a boundary retrievable from either side.
|
None
|
min_chunk_size
|
int
|
Chunks shorter than this are merged into their neighbour rather than emitted — stray one-line fragments pollute retrieval. |
50
|
keep_separator
|
bool
|
Whether the split separator stays in the output. |
True
|
name
|
str | None
|
Component name for traces. |
None
|
**config
|
Any
|
Strategy-specific options. |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
unit |
str
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an explicit |
Example
Implementing a chunker takes one method::
class LineChunker(Chunker):
provider_name = "line"
def split_text(self, text: str) -> list[str]:
return [ln for ln in text.splitlines() if ln.strip()]
Source code in src\windlass\interfaces\chunker.py
split_text
abstractmethod
¶
Split raw text into chunk strings.
The only method a strategy must implement. Return the pieces in reading
order; the base class turns them into :class:Chunk objects with ids,
offsets and inherited metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to split. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Chunk strings in document order. |
Source code in src\windlass\interfaces\chunker.py
asplit_text
async
¶
Async :meth:split_text.
Override this instead when your strategy needs to await something — the semantic chunker calls an embedding model here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to split. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Chunk strings in document order. |
Source code in src\windlass\interfaces\chunker.py
achunk_document
async
¶
Split one document into chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Document
|
The document to split. |
required |
Returns:
| Type | Description |
|---|---|
list[Chunk]
|
Chunks carrying the document's metadata plus |
list[Chunk]
|
|
Source code in src\windlass\interfaces\chunker.py
achunk
async
¶
achunk(
documents: Sequence[Document] | Document, *, concurrency: int | None = None
) -> list[Chunk]
Split many documents concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
Sequence[Document] | Document
|
One document or a sequence of them. |
required |
concurrency
|
int | None
|
Maximum simultaneous splits. Defaults to the global
|
None
|
Returns:
| Type | Description |
|---|---|
list[Chunk]
|
All chunks, grouped by source document in input order. |
Performance
CPU-bound strategies see little benefit from concurrency; strategies
that await a model (semantic, contextual) see a lot.
Source code in src\windlass\interfaces\chunker.py
chunk
¶
chunk_text
¶
Split a bare string, without constructing a Document first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to split. |
required |
metadata
|
dict[str, Any] | None
|
Metadata copied onto every chunk. |
None
|
Returns:
| Type | Description |
|---|---|
list[Chunk]
|
The resulting chunks. |
Example
from windlass.providers.chunkers.recursive import RecursiveChunker RecursiveChunker(chunk_size=100, overlap=10).chunk_text("hello")[0].content 'hello'