# semchunk

> Split text into semantically meaningful chunks for embedding and retrieval

- Category: Retrieval & Knowledge (RAG) / Parsing & Ingestion
- Language: Python
- Repository: https://github.com/isaacus-dev/semchunk
- Canonical page: https://ai-tldr.dev/tools/semchunk/

## In short

semchunk is a Python library for splitting text into smaller chunks while keeping as much local semantic context as possible. It uses a hierarchical chunking algorithm and works with any tokenizer or token counter, including those from Tiktoken and Transformers. It sits in the Parsing & Ingestion category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python.

## Overview

semchunk is a Python library for splitting text into smaller chunks while keeping as much local semantic context as possible. It uses a hierarchical chunking algorithm and works with any tokenizer or token counter, including those from Tiktoken and Transformers.

It is aimed at developers building retrieval-augmented generation (RAG) pipelines who need to break long documents into chunks before embedding and indexing them. You pass a tokenizer name or a custom token-counting function plus a target chunk size, and semchunk returns the chunks.

Within the parsing and ingestion stage of a RAG system, semchunk handles the splitting step. It supports chunk overlap, chunk offsets, multiprocessing for large batches, and an optional AI-powered chunking mode that uses Isaacus enrichment models.

## What it does

- Works with any tokenizer or token counter, including Tiktoken encodings, Transformers tokenizers, OpenAI model names, or a custom function
- Returns chunk offsets when you pass offsets=True, so you can map chunks back to the source text
- Supports chunk overlap by a ratio (values < 1) or an absolute token count (values >= 1)
- Optional multiprocessing via the processes argument to speed up chunking large batches of texts
- Optional AI-powered chunking using Isaacus enrichment models such as kanon-2-enricher
- Available on PyPI and conda-forge, with a community Rust port (semchunk-rs)

## Getting started

Install semchunk from PyPI, then build a chunker by passing a tokenizer and a target chunk size.

1. Install semchunk

   Install with pip (or uv). It is also available on conda-forge.

```bash
pip install semchunk
```

2. Build a chunker and chunk text

   Create a chunker with chunkerify() by passing a tokenizer name (here an OpenAI Tiktoken encoding) and a chunk size, then call it on your text. Passing a list of texts returns a list of lists of chunks.

```python
import semchunk

chunk_size = 4  # tokens per chunk
text = 'The quick brown fox jumps over the lazy dog.'

chunker = semchunk.chunkerify('cl100k_base', chunk_size)

assert chunker(text) == ['The quick brown fox', 'jumps over the', 'lazy dog.']
```

3. Get offsets and overlap (optional)

   Pass offsets=True to also return where each chunk starts and ends, and overlap to overlap chunks by a ratio (< 1) or an absolute number of tokens (>= 1).

```python
chunks, offsets = chunker(text, offsets=True, overlap=0.5)
```

## When to use it

- Splitting long documents into chunks before embedding them for a RAG pipeline
- Preparing text for a vector database while keeping local semantic context intact
- Chunking with the same tokenizer your embedding or LLM uses, so chunk sizes match the model's token budget
- Processing large batches of documents quickly using multiprocessing
