Skip to main content

Simple Kafka-based reliable AI streaming

Project description

Reliable AI Streams

Simple Kafka-based reliable streaming for AI applications. Handles reconnections, replays, and ensures no message loss.

Features

  • ✅ Reliable streaming (survives page refreshes)
  • ✅ Replay from any point
  • ✅ Simple API
  • ✅ Production-ready
  • ✅ Type-safe

Installation

pip install reliable-ai-streams

Quick Start

from reliable_ai_streams import Publisher, Subscriber, Chunk

# Publish
with Publisher() as pub:
    chunk = Chunk(
        conversation_id="chat-123",
        content="Hello!",
        type="text"
    )
    pub.publish(chunk)

# Subscribe
with Subscriber() as sub:
    for chunk in sub.subscribe("chat-123"):
        print(chunk.content)
        if chunk.type == "finish":
            break

Configuration

Set via environment variables:

KAFKA_BOOTSTRAP_SERVERS=localhost:9092
KAFKA_TOPIC_PREFIX=ai-stream

Or pass directly:

from reliable_ai_streams import Config, Publisher

config = Config(bootstrap_servers="kafka:9092")
publisher = Publisher(config)

FastAPI Example

from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from reliable_ai_streams import Publisher, Subscriber

app = FastAPI()
publisher = Publisher()
subscriber = Subscriber()

@app.on_event("startup")
def startup():
    publisher.connect()
    subscriber.connect()

@app.get("/stream/{conversation_id}")
def stream(conversation_id: str):
    def events():
        for chunk in subscriber.subscribe(conversation_id):
            yield f"data: {chunk.to_json().decode()}\n\n"
    
    return StreamingResponse(events(), media_type="text/event-stream")

License

MIT


## Installation & Usage

```bash
# Development setup
git clone <your-repo>
cd reliable-ai-streams

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install
pip install -e .

# Or with dev dependencies
pip install -e ".[dev]"

# Setup environment
cp .env.example .env
# Edit .env with your Kafka settings

# Run example
python examples/basic_example.py

# Run tests (requires Kafka running)
pytest

Project details


Download files

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

Source Distribution

reliable_ai_streams-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

reliable_ai_streams-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file reliable_ai_streams-0.1.0.tar.gz.

File metadata

  • Download URL: reliable_ai_streams-0.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for reliable_ai_streams-0.1.0.tar.gz
Algorithm Hash digest
SHA256 879b91ea303988713ea6b0ec20a5c9e0e04f05785c5fac0cf414fdad6619f32f
MD5 beb1fbb1467c088919b3fbd32740e225
BLAKE2b-256 968e69cf99646460f06c9530b470fd70073e2adf6ce5880f99cd575540b2dfa1

See more details on using hashes here.

File details

Details for the file reliable_ai_streams-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for reliable_ai_streams-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0ca40dd6203ddeb84612c7d1df5f7f71f5c54ea48c117805af52d1544f04c00
MD5 df007afdb918d539623ab57dc7723909
BLAKE2b-256 db4675a0ffb985d0e5112d59c2e899c1f6ea175c6a09b917d6e0fcaeebec9f5f

See more details on using hashes here.

Supported by

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