Skip to main content

chunkstream 🌊

PyPI version Python versions CI License: MIT Zero Dependency

Zero-dependency, memory-efficient streaming chunk buffer, delimiter-aware frame accumulator, and Server-Sent Events (SSE) parser for Python.

Designed specifically for streaming LLM tokens (OpenAI, Anthropic, Gemini, Ollama, DeepSeek), HTTP chunked transfer decoders, WebSocket packet framing, and async network pipelines.


⚡ Why chunkstream?

When consuming real-world streaming APIs, network sockets deliver data in arbitrary packet slices (e.g. {"mess, age": "hel, lo"}). Handling fragmented chunks, multibyte UTF-8 characters across boundaries, and Server-Sent Events without external dependencies or memory bloat is tedious.

chunkstream solves this with:

  • Zero Dependencies: Pure standard library, compatible with Python 3.10 through 3.14.
  • Dual Sync & Async Buffers: ChunkBuffer for synchronous streams and AsyncChunkBuffer for asyncio event-driven loops.
  • Spec-Compliant SSE Parser: Parses data:, event:, id:, and [DONE] markers across fragmented chunks in both parse_sse and parse_sse_async.
  • Delimiter & Fixed-Length Framers: Split byte streams by arbitrary delimiters (\n, \r\n\r\n, \0, or multipart boundaries).
  • Backpressure & Overflow Protection: Configurable max_size prevents runaway memory allocation on slow consumers.
  • Sliding Stream Window: Lookbehind and deduplication window for stream inspection.

📦 Installation

pip install chunkstream

🚀 Quickstart

1. Parsing Streaming LLM Server-Sent Events (SSE)

Seamlessly parse fragmented chunks from any LLM or HTTP client (httpx, aiohttp, urllib):

from chunkstream import parse_sse

def mock_llm_stream():
    yield b'data: {"id": "c1", "choices": [{"delta": {"con'
    yield b'tent": "Hello"}}]}\n\n'
    yield b'data: {"id": "c1", "choices": [{"delta": {"content": " world!"}}]}\n\n'
    yield b'data: [DONE]\n\n'

for event in parse_sse(mock_llm_stream()):
    if event.is_done:
        print("\n[Generation Complete]")
        break
    print(f"Token: {event.data}")

2. Asynchronous SSE Stream Parsing

import asyncio
from chunkstream import parse_sse_async

async def main():
    async for event in parse_sse_async(async_byte_generator()):
        if event.is_done:
            break
        print(event.data)

asyncio.run(main())

3. Delimiter Framing (ChunkBuffer)

Accumulate incoming network chunks and read complete lines or frames:

from chunkstream import ChunkBuffer

buffer = ChunkBuffer(max_size=1024 * 1024) # 1MB limit

buffer.feed(b"frame_one\0frame_two\0partial_th")

# Read frame by frame
while frame := buffer.read_until(b"\0"):
    print("Frame:", frame.decode())

# Remaining data stays safely buffered
print("Unconsumed:", buffer.read()) # b"partial_th"

4. Sliding Stream Window

Track rolling history over an ongoing stream:

from chunkstream import StreamWindow

window = StreamWindow(size=3)
for item in ["A", "B", "C", "D"]:
    window.feed(item)

print(window.snapshot()) # ['B', 'C', 'D']

🧪 Running Tests

pip install pytest
pytest

📄 License

MIT © 2026 Hamidreza (mrhamidrezam)

Metadata

Release files for chunkstream 0.1.0

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

Source distribution (sdist)

Source distribution for chunkstream 0.1.0
File Size Uploaded Provenance Actions
chunkstream-0.1.0.tar.gz 10.9 kB With provenance Details for chunkstream-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 20.4 kB

Release files / chunkstream-0.1.0.tar.gz

Download URL chunkstream-0.1.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
05d7ff7740c885140c481285b5da4dfe3901d21a264e9203cbbd3df1c5e15ff3
BLAKE2b-256 checksum
How to use checksums
a3a6c52952e2fe0a39d897396e7e9b84d32d050619de6e45ff356f5b63b43527
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Release files / chunkstream-0.1.0-py3-none-any.whl

Download URL chunkstream-0.1.0-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e43d9c404fa34693964419c7596531a9ab1ec4237bdea30dc2dd7997a11248c4
BLAKE2b-256 checksum
How to use checksums
a6e059608a48926f1b73729d84d2c6a63e966a9e790402b9645713125fb61d7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Security for chunkstream 0.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for chunkstream-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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