Minimal FastAPI backend for streaming AI chat over SSE (token/done/error), compatible with ai-chat-kit
Project description
fastapi-ai-chat
Minimal FastAPI backend library for streaming AI chat over Server-Sent Events (SSE).
Designed to be wire-compatible with ai-chat-kit's React adapter:
POST /chat/streamreturnstext/event-stream- emits SSE events:
token,done,error
Install
Local editable install (recommended during development):
python -m pip install -e Backend/fastapi-ai-chat
From PyPI (after you publish):
python -m pip install "fastapi-ai-chat[server]"
Run
Create a small server file (or use the included example):
python -m pip install "fastapi-ai-chat[server]"
python -m uvicorn examples.server:app --reload --port 8000
Environment variables (optional):
OPENAI_API_KEYorFASTAPI_AI_CHAT_OPENAI_API_KEYANTHROPIC_API_KEYorFASTAPI_AI_CHAT_ANTHROPIC_API_KEYGEMINI_API_KEYorFASTAPI_AI_CHAT_GEMINI_API_KEYFASTAPI_AI_CHAT_SQLITE_PATH(default:./chat_history.sqlite3)
Streaming protocol (SSE)
The server emits frames separated by a blank line (\n\n):
event: tokenwith JSON:{"type":"token","token":"..."}(many times)event: donewith JSON:{"type":"done","message":{"role":"assistant","content":"..."}, "provider":"...", "conversationId":"..."}event: errorwith JSON:{"type":"error","error":{"message":"..."}}
Test streaming:
curl -N -X POST "http://localhost:8000/chat/stream" \
-H "Content-Type: application/json" \
-d '{"userId":"u1","messages":[{"role":"user","content":"Hello streaming"}]}'
Selecting a model/provider
Send params.model in the request body:
- OpenAI:
gpt-4.1(default if omitted) - Anthropic:
claude-3-5-sonnet(or any model starting withclaude) - Gemini:
gemini-1.5-pro(or any model starting withgemini)
If you do not configure provider keys, the backend will fall back to a deterministic mock stream unless a real model was explicitly requested.
Publish to PyPI
Build:
python -m pip install -U build twine
python -m build Backend/fastapi-ai-chat
Upload:
- Recommended: PyPI Trusted Publishing
- Or token-based:
python -m twine upload Backend/fastapi-ai-chat/dist/*
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_ai_chat-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_ai_chat-0.1.0.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff740a5329db668f0c7d3cf06f6b9921ee623e1e4475190f4f9f65a3f9a6e067
|
|
| MD5 |
974ffe1d8ffe614ee22f693ea1e5eb24
|
|
| BLAKE2b-256 |
936b72ec696d04c6f25de0904c7e2292f7ad751827f5652329536d095e4f8e15
|
File details
Details for the file fastapi_ai_chat-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_ai_chat-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc0738a6332506e49f5390eda4b406ce89600e7f38063e051c64cc04d2f1ee31
|
|
| MD5 |
1c9eb05b5902e804fe8cb5e2503cf1df
|
|
| BLAKE2b-256 |
abe00c2ecc399642928460f208f43bcf207c6af6e1edf72910fe5d6efa320131
|