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
- Dual Requirements: Emit only when both line and token minimums are met.
- Clean Starts: New chunks begin at the first non-breaking token.
- Trailing Breaks Merge: Line breaks at the boundary are absorbed into the previous chunk.
- 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 splitlines_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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| chunkle-0.3.1.tar.gz | 3.8 kB | Without provenance | Details for chunkle-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| chunkle-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for chunkle-0.3.1-py3-none-any.whl |
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
|
Security for chunkle 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.