Skip to main content
The CodeChunker splits code into chunks based on its structure, leveraging Abstract Syntax Trees (ASTs) to create contextually relevant segments.

Overview

Supported Languages

Each language is identified by the key used with get_language(key) and get_parser(key).

General-Purpose Programming Languages

Web, UI & Markup

Config, Build & DevOps

Systems, GPU & Low-level

API Reference

To use the CodeChunker via the API, check out the API reference documentation.

Installation

CodeChunker requires additional dependencies for code parsing. You can install it with:
For installation instructions, see the Installation Guide.

Initialization

Automatic language detection using Magika can impact performance. For best results, specify the language directly via the language parameter.

Parameters

str
required
The programming language of the code. Accepts languages supported by tree-sitter-language-pack.
Union[str, Callable, Any]
default:"character"
Tokenizer or token counting function to use for measuring chunk size.
int
default:"2048"
Maximum number of tokens per chunk.
bool
default:"False"
Whether to include AST node information (Note: with the base Chunk type, node information is not stored).

Usage

Single Code Chunking

Batch Chunking

Using as a Callable

Return Type

CodeChunker returns chunks as Chunk objects:
As of version 1.3.0, CodeChunker returns the base Chunk type instead of the specialized CodeChunk type. This simplifies integration with other chunkers and refineries.