windlass.providers.chunkers.structural¶
structural
¶
Structure-aware chunking for Markdown and source code.
Generic splitters treat a document as a flat string. These two respect the structure that is already there:
- :class:
MarkdownChunkersplits on headings and prepends the heading path to every chunk, so a chunk that says "the timeout defaults to 30s" retrieves as "Configuration > Networking > the timeout defaults to 30s". That context is frequently the difference between a hit and a miss. - :class:
CodeChunkersplits at function and class boundaries for a dozen languages, so a retrieved chunk is a complete callable rather than its middle eight lines.
Neither has any dependencies.
Example
md = "# Guide\n\nIntro text.\n\n## Setup\n\nRun the installer." chunks = MarkdownChunker(chunk_size=200).split_text(md) "Guide > Setup" in chunks[-1] True
MarkdownChunker
¶
MarkdownChunker(
*,
chunk_size: int = 1500,
overlap: int | None = None,
max_heading_level: int = 3,
include_heading_path: bool = True,
strip_code_fences: bool = True,
**config: Any
)
Bases: Chunker
Heading-aware Markdown splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_size
|
int
|
Target chunk length in characters. Sections longer than this are split further with the recursive splitter. |
1500
|
overlap
|
int | None
|
Characters repeated when a section must be sub-split. |
None
|
max_heading_level
|
int
|
Deepest heading treated as a split point. |
3
|
include_heading_path
|
bool
|
Prepend the breadcrumb ( |
True
|
strip_code_fences
|
bool
|
Keep fenced code blocks intact rather than letting a
|
True
|
**config
|
Any
|
Forwarded to :class: |
{}
|
Example
chunker = MarkdownChunker(chunk_size=500, include_heading_path=False) chunker.split_text("# A\n\ntext")[0] '# A\n\ntext'
Source code in src\windlass\providers\chunkers\structural.py
split_text
¶
Split Markdown into heading-scoped chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The Markdown source. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Chunk strings, each prefixed with its heading path when |
list[str]
|
attr: |
Source code in src\windlass\providers\chunkers\structural.py
CodeChunker
¶
CodeChunker(
*,
language: str = "generic",
chunk_size: int = 800,
overlap: int | None = None,
**config: Any
)
Bases: RecursiveChunker
Language-aware source-code splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
One of the keys in :data: |
'generic'
|
chunk_size
|
int
|
Target chunk length in characters. Code is dense, so a smaller value than for prose usually works better. |
800
|
overlap
|
int | None
|
Characters repeated between chunks. Small values are right for code — duplicating half a function helps nobody. |
None
|
**config
|
Any
|
Forwarded to :class: |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
For an unknown language. |
Example
code = "def a():\n return 1\n\ndef b():\n return 2\n" chunks = CodeChunker(language="python", chunk_size=25).split_text(code) len(chunks) >= 1 True
Source code in src\windlass\providers\chunkers\structural.py
for_path
classmethod
¶
Build a chunker configured for a file's language.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path or name; only the extension is inspected. |
required |
**config
|
Any
|
Forwarded to the constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
CodeChunker
|
A chunker using that language's separators, falling back to |
CodeChunker
|
|
Example
CodeChunker.for_path("app/main.py").language 'python' CodeChunker.for_path("notes.xyz").language 'generic'