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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chunkle-0.3.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chunkle-0.3.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file chunkle-0.3.1.tar.gz.

File metadata

  • Download URL: chunkle-0.3.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.13 Darwin/25.5.0

File hashes

Hashes for chunkle-0.3.1.tar.gz
Algorithm Hash digest
SHA256 07f21c5e030c5f35696f957faeffe8f1a15aaa704fc2683daeca43f7a0e5415b
MD5 0fccc394895bccd7f07895e8a606eba1
BLAKE2b-256 db73e791fbc8affdcaafd727385a51d396e8dc1d39270dbd6e34cd753d5db3ae

See more details on using hashes here.

File details

Details for the file chunkle-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: chunkle-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.13 Darwin/25.5.0

File hashes

Hashes for chunkle-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 728eaefcac01f2e765c1e29e0f9b59747bf65583acdf37ab8327127651303c72
MD5 ad0651264619adea2233ca29915fb881
BLAKE2b-256 cfe6c4c4b069a5d3cc6856fc4e18c24e02c81c1eb04e677806659ad4b66428bb

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page