Skip to main content

A small, production-minded SSE wrapper for FastAPI-based AI agents and chatbots.

Project description

fastapi-sse-wrapper

A small, professional SSE (Server-Sent Events) wrapper for FastAPI, designed to help developers build streaming AI agent and chatbot backends.


✨ Features

  • Named channels with multiple client connections
  • Broadcast and targeted messages
  • Message history & replay for reconnection support
  • Keepalive pings and recommended HTTP headers
  • Authentication hooks (pluggable)
  • Minimal dependencies (FastAPI + Uvicorn)
  • Typed public API and examples

📦 Installation

pip install fastapi-sse-wrapper

🚀 Quick Start

Create SSE Manager and Endpoint

from fastapi import FastAPI
from fastapi_sse.core import SSEManager
from fastapi_sse.events import SSEEvent

app = FastAPI()
manager = SSEManager(app=app)

# Register SSE endpoint for channel "chat"
app.add_api_route("/sse/chat/{client_id}", manager.endpoint("chat"), methods=["GET"])

Send Events

@app.post("/send/{channel}")
async def send_message(channel: str, payload: dict):
    ch = manager.channel(channel)
    ev = SSEEvent(data=payload, event="message")
    await ch.send(ev)
    return {"status": "ok", "id": ev.id}

Connect Client

Browser client using EventSource:

const es = new EventSource("http://127.0.0.1:8000/sse/chat/myclient");
es.onmessage = e => console.log("msg", e.data);
es.addEventListener("message", e => console.log("event:message", e.data));

🧠 Example: Streaming AI Agent Tokens

@app.post("/agent/send/{conversation_id}")
async def agent_send(conversation_id: str, payload: dict):
    ch = manager.channel("agent")
    start = SSEEvent(data={"cid": conversation_id, "status": "started"}, event="agent.start")
    await ch.send(start)

    for token in ["Hello", ",", " this", " is", " streamed"]:
        await asyncio.sleep(0.5)
        ev = SSEEvent(data={"cid": conversation_id, "token": token}, event="agent.token")
        await ch.send(ev)

    done = SSEEvent(data={"cid": conversation_id, "status": "done"}, event="agent.done")
    await ch.send(done)
    return {"started": True}

🔒 Authentication

Use an auth hook:

from fastapi_sse.middleware import token_auth_hook_factory

def token_lookup(token: str):
    return "user1" if token == "secret" else None

manager = SSEManager(app, auth_hook=token_auth_hook_factory(token_lookup))

📂 Project Structure

fastapi-sse-wrapper/
├─ fastapi_sse/
│  ├─ core.py         # Manager + connections
│  ├─ events.py       # SSEEvent model
│  ├─ middleware.py   # Auth hook helpers
│  ├─ backends.py     # In-memory history backend
│  └─ utils.py
├─ examples/
│  ├─ simple_app.py
│  └─ agent_integration.py
├─ tests/
└─ README.md

⚡ Roadmap

  • Redis backend for multi-worker support
  • Advanced reconnection + replay strategies
  • Rate limiting and access control
  • TypeScript/JS client SDK

📜 License

MIT License.

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

fastapi_sse_wrapper-0.1.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

fastapi_sse_wrapper-0.1.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_sse_wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.2

File hashes

Hashes for fastapi_sse_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 823c8034bf918acc046adcf85ba3c5a5af838ae4d288459e279c3654ce61c5e8
MD5 f9e3552556b42ac77fdae8f5e97e4dfe
BLAKE2b-256 d67822305bdc4b4193177be3b00bea1e43d8aaade804d5045d0513755565a2d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_sse_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1fa66e1e8804bee26b594810af307340b260a6b43c0dddc0f758f67b6a2f359
MD5 bd3af360d0b74cb67dde13028ab74de4
BLAKE2b-256 64b8845af641698848fe7e8d5806df0235df595ad58d8df75c2c3de1589ae893

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