Skip to main content

chunkle

Smart token-based chunking that respects both line and token limits while preserving clean starts.

GitHub: https://github.com/allen2c/chunkle Pypi: https://pypi.org/project/chunkle/

Install

pip install chunkle

Quick Start

from chunkle import chunk

# Basic usage
for piece in chunk(text, lines_per_chunk=20, tokens_per_chunk=500):
    print(piece)

# Custom limits
chunks = list(chunk(text, lines_per_chunk=5, tokens_per_chunk=100))

How It Works (Token-based)

flowchart TD
    A["📝 Start encoding to tokens"] --> B["📊 Accumulate token ids<br/>Track newline-token count"]
    B --> C{"✅ Both limits met?<br/>(lines ≥ min AND tokens ≥ min)"}
    C -->|No| B
    C -->|Yes| D{"🔀 Current token is breaking?"}
    D -->|Yes| E["🟡 Arm emit (should_emit=True)"] --> F{"🔤 Next token non-breaking?"}
    D -->|No| G{"🚀 Force beyond multiplier?"}
    G -->|Yes| H{"🔡 Token boundary is whitespace?"}
    H -->|Yes| E
    H -->|No| B
    G -->|No| B
    F -->|Yes| I["✂️ Emit buffer; new chunk starts meaningful"]
    F -->|No| B
    I --> J{"📄 More tokens?"}
    J -->|Yes| B
    J -->|No| K["🏁 Emit remaining buffer (merge trailing breaks)"]

Rules

  1. Dual Requirements: Emit only when both line and token minimums are met.
  2. Clean Starts: New chunks begin at the first non-breaking token.
  3. Trailing Breaks Merge: Line breaks at the boundary are absorbed into the previous chunk.
  4. Force Emit (2x multiplier): When exceeding thresholds×multiplier, force emit only if current token boundary is whitespace.

Examples

English Text:

text = "Hello world!\nThis is a test.\nAnother line here."
chunks = list(chunk(text, lines_per_chunk=1, tokens_per_chunk=8))
# Result: ['Hello world!\nThis is a test.\n', 'Another line here.']

English Text (force split):

text = " ".join(["This is a long sentence without newlines."] * 4)
chunks = list(chunk(text, lines_per_chunk=1, tokens_per_chunk=8, force_chunk_over_threshold_times=2))
# ['This is a long sentence without newlines. This is a long sentence without new', 'lines. This is a long sentence without newlines. This is a long sentence', ' without newlines.']

API

def chunk(
    content: str,
    *,
    lines_per_chunk: int = 20,
    tokens_per_chunk: int = 500,
    force_chunk_over_threshold_times: int = 2,
    encoding: tiktoken.Encoding | None = None,
) -> Generator[str, None, None]:

Parameters:

  • content: Text to split
  • lines_per_chunk: Minimum lines per chunk (default: 20)
  • tokens_per_chunk: Minimum tokens per chunk (default: 500)
  • force_chunk_over_threshold_times: Force emit multiplier (default: 2)
  • encoding: Custom tiktoken encoding (default: gpt-4o-mini)

License

MIT © 2025 Allen Chou

Metadata

Release files for chunkle 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chunkle 0.3.1
File Size Uploaded
chunkle-0.3.1.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chunkle 0.3.1
File Interpreter ABI Platform
chunkle-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / chunkle-0.3.1.tar.gz

Download URL chunkle-0.3.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
07f21c5e030c5f35696f957faeffe8f1a15aaa704fc2683daeca43f7a0e5415b
BLAKE2b-256 checksum
How to use checksums
db73e791fbc8affdcaafd727385a51d396e8dc1d39270dbd6e34cd753d5db3ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.12.13 Darwin/25.5.0

Release files / chunkle-0.3.1-py3-none-any.whl

Download URL chunkle-0.3.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
728eaefcac01f2e765c1e29e0f9b59747bf65583acdf37ab8327127651303c72
BLAKE2b-256 checksum
How to use checksums
cfe6c4c4b069a5d3cc6856fc4e18c24e02c81c1eb04e677806659ad4b66428bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.12.13 Darwin/25.5.0

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page