Skip to main content
The RecursiveChunker is a chunker that recursively chunks documents into smaller chunks. It is a good choice for documents that are long but well structured, for example, a book or a research paper.

API Reference

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

Installation

The RecursiveChunker is included in the base installation of Chonkie. No additional dependencies are required.
If you would like to use custom tokenizers in JavaScript, please install the @chonkiejs/token library

Initialization

The RecursiveChunker uses RecursiveRules to determine how to chunk the text. The rules are a list of RecursiveLevel objects, which define the delimiters and whitespace rules for each level of the recursive tree. Find more information about the rules in the Additional Information section.
You can also initialize the RecursiveChunker using a recipe. Recipes are pre-defined rules for common chunking tasks. Find all available recipes on our Hugging Face Hub here.
Recipes are supported on Python only

Parameters

Union[str, Callable, Any]
default:"character"
Tokenizer to use. Can be a string identifier or a tokenizer instance
int
default:"2048"
Maximum number of tokens per chunk
RecursiveRules
default:"RecursiveRules()"
Rules to use for chunking.
int
default:"12"
Minimum number of characters per chunk

Usage

Single Text Chunking

Batch Chunking

Using as a Callable

Return Type

The RecursiveChunker returns chunks as Chunk objects:

Additional Information

The RecursiveChunker uses the RecursiveRules class to determine the chunking rules. The rules are a list of RecursiveLevel objects, which define the delimiters and whitespace rules for each level of the recursive tree.
You can pass in custom rules to the RecursiveChunker, or use the default rules. The default rules are designed to be a good starting point for most documents, but you can customize them to your needs.
RecursiveLevel expects the list of custom delimiters to not include whitespace. If whitespace as a delimiter is required, you can set the whitespace parameter in the RecursiveLevel class to True. Note that if whitespace = True, you cannot pass a list of custom delimiters.