benbrandt/text-splitter

Split text into semantic chunks, up to a desired chunk size. Supports calculating length by characters and tokens, and is callable from Rust and Python.

What it solves

Large language models have strict limits on how much text they can process at once. This project solves the problem of breaking long documents into smaller, manageable pieces—or "chunks"—that fit within these limits while keeping the text meaningful.

How it works

Instead of just cutting text at arbitrary points, this tool uses a hierarchy of semantic boundaries. It attempts to keep related information together by prioritizing natural breaks, such as sentences, paragraphs, or code syntax, before falling back to smaller units like words or characters. You can define your chunk size by character count or by token count using popular tokenizers like those from Hugging Face or OpenAI.

Who it’s for

This is for developers building applications that need to feed large documents, codebases, or Markdown files into LLMs. It is designed for those who need high-performance, semantically aware text splitting in Rust or Python environments.

Highlights

  • Supports plain text, Markdown, and various programming languages via tree-sitter.
  • Allows custom chunk sizes based on character count or specific token limits.
  • Uses Unicode-standard rules for identifying word and sentence boundaries.
  • Provides flexible chunk capacity, including the ability to set a range for chunk size.
  • Offers both a native Rust crate and Python bindings.

Related

  • Project
  • Project
  • Project
  • Project
  • Project