Skip to main content

rewind-stream for Python

The Resumable Stream Protocol for Python servers: LLM answers that survive dropped connections, page reloads and multiple tabs. An ASGI app (FastAPI, Starlette, Django, Litestar…), memory and Redis stores, no required dependencies.

Clients are the same as for the TypeScript server: the browser's native EventSource, or the rewind-stream npm client with automatic resume.

FastAPI

from anthropic import AsyncAnthropic
from fastapi import FastAPI, Request
from rewind_stream import MemoryStore, StreamApp
from rewind_stream.starlette import start

app = FastAPI()
client = AsyncAnthropic()
store = MemoryStore()  # RedisStore(...) for several instances
app.mount("/streams", StreamApp(store))  # GET (SSE or snapshot), DELETE (cancel)


@app.post("/chat")
async def chat(request: Request):
    prompt = (await request.json())["prompt"]

    async def answer(cancelled):  # set when a client cancels
        async with client.messages.stream(model="claude-sonnet-5-5", max_tokens=1024, messages=[{"role": "user", "content": prompt}]) as s:
            async for text in s.text_stream:
                if cancelled.is_set():
                    return
                yield {"event": "delta", "data": {"text": text}}

    return await start(answer, store=store, url=lambda sid: f"/streams/{sid}")

start runs the generation in the background, detached from the request: a client that disconnects can resume from /streams/<id> with Last-Event-ID, or read a JSON snapshot with Accept: application/json. Errors raised by the source end the stream with stream.error without exposing their message (pass format_error to change that).

Several instances: RedisStore

import redis.asyncio as redis
from rewind_stream import RedisStore

store = RedisStore(redis.Redis.from_url("redis://localhost:6379", decode_responses=True))

Any client with redis-py's execute_command works. The store uses only basic commands (no Lua, no streams, no blocking reads), and its layout is the same as the TypeScript RedisStore: Python and Node servers can share one Redis and resume each other's streams.

Without a framework

StreamApp(store) is a plain ASGI app, and start_stream(store, source) starts a generation and returns its ID, for any ASGI framework.

Tests

pip install -e ".[test]"
python -m unittest discover -s tests -t .           # REDIS_URL=redis://… adds the RedisStore tests

The TypeScript conformance suite (npx rewind-conform <url>) passes against this server; CI runs it on every change.

Metadata

Release files for rewind-stream 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 rewind-stream 0.1.0
File Size Uploaded Provenance Actions
rewind_stream-0.1.0.tar.gz 14.0 kB With provenance Details for rewind_stream-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 26.9 kB

Release files / rewind_stream-0.1.0.tar.gz

Download URL rewind_stream-0.1.0.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
dd6629f29b1e015f4837034d2153279bea9198a02131f3320567a7049b1964e3
BLAKE2b-256 checksum
How to use checksums
d83a57e80875fa2050a27416aa9480c38056015ee7afcba7692031ea16b4586f
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 9, 2026.

Transparency log

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

Download URL rewind_stream-0.1.0-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0df3c8c419b3c5ec8e68fd1a3d8cc352e260b4b80aa24699851fcb6aaf81c7f6
BLAKE2b-256 checksum
How to use checksums
846a64266bdbdde3c76fb5d05cd6e51089959e3869829d05b208c72d4cb4d6b7
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 9, 2026.

Transparency log

Security for rewind-stream 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 rewind_stream-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 9, 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