agrag.chunking.extras.CodeChunker
Bases: _ExtraSpanChunker
Cuts source code along its syntax tree.
Needs the chunk-code extra. The parser reports byte offsets, and this
chunker converts them to character offsets, so chunk text equals the source
slice for non-ASCII code too.
Attributes:
- language (
str) – A tree-sitter language name, or"auto"to detect it. - chunk_size (
int) – The largest chunk size, counted withtokenizer. - tokenizer (
str) – The tokenizer that counts size."character"counts characters.
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 only, so the extra is not needed to build.
- settings – Return the strategy name and every setting as JSON-safe data.
- spans – Return character spans, converted from the parser's byte spans.
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 withchunkerandchunker_hashset. A strategy that setschunkeritself keeps its value.
Raises:
ChunkingError– The strategy returned chunks that break the contract.
chunk_size
chunk_size: int = Field(default=256, gt=0)
fingerprint
fingerprint() -> str
Return the hash of settings(), 16 hex characters.
language
language: str = 'auto'
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 only, so the extra is not needed to build.
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.
spans
spans(text:str) -> list[tuple[int, int]]
Return character spans, converted from the parser's byte spans.
strategy
strategy: str
The strategy name, "code".
tokenizer
tokenizer: str = DEFAULT_TOKENIZER