fastapi-ai-chat
Minimal FastAPI backend for streaming AI chat over Server‑Sent Events (SSE).
It’s designed to be wire‑compatible with ai-chat-kit’s React FastAPI adapter:
POST /chat/streamreturnstext/event-stream- emits SSE events:
token,done,error
Features
- Token streaming over SSE: incremental
tokenevents + finaldoneevent (orerror). - Provider routing by model name:
- OpenAI for most models (default:
gpt-4.1) - Anthropic for models starting with
claude - Gemini for models starting with
gemini
- OpenAI for most models (default:
- Mock streaming fallback: deterministic mock stream when no provider keys are configured (great for UI/dev).
- Conversation persistence (optional): SQLite chat history + basic conversation CRUD endpoints.
- CORS enabled: defaults to
*for local dev (configurable).
Install
From PyPI:
python -m pip install "fastapi-ai-chat[server]"
Quickstart (run the example server)
cd Backend/fastapi-ai-chat
python -m uvicorn examples.server:app --reload --port 8000
Configuration (environment variables)
This library intentionally does not load .env files for you; it reads process env vars (or you can pass keys directly to create_app(...)).
- Provider keys
OPENAI_API_KEYorFASTAPI_AI_CHAT_OPENAI_API_KEYANTHROPIC_API_KEYorFASTAPI_AI_CHAT_ANTHROPIC_API_KEYGEMINI_API_KEYorFASTAPI_AI_CHAT_GEMINI_API_KEY
- Persistence
FASTAPI_AI_CHAT_SQLITE_PATH(default:./chat_history.sqlite3)
- Optional provider base URL overrides
OPENAI_BASE_URL/FASTAPI_AI_CHAT_OPENAI_BASE_URLANTHROPIC_BASE_URL/FASTAPI_AI_CHAT_ANTHROPIC_BASE_URLGEMINI_BASE_URL/FASTAPI_AI_CHAT_GEMINI_BASE_URL
Example use
1) Stream tokens (curl)
curl -N -X POST "http://localhost:8000/chat/stream" \
-H "Content-Type: application/json" \
-d '{
"userId":"u1",
"messages":[{"role":"user","content":"Hello streaming"}]
}'
2) Pick a real 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)
Example:
curl -N -X POST "http://localhost:8000/chat/stream" \
-H "Content-Type: application/json" \
-d '{
"userId":"u1",
"messages":[{"role":"user","content":"Write a haiku about SSE."}],
"params":{"model":"gpt-4.1"}
}'
If you don’t configure provider keys, the backend falls back to a deterministic mock stream unless a real model was explicitly requested via params.model.
3) Embed in your own FastAPI app
from fastapi_ai_chat import create_app
app = create_app(
openai_api_key="...", # or None / "" to disable OpenAI
anthropic_api_key="...",
gemini_api_key="...",
sqlite_path="./chat_history.sqlite3",
cors_allow_origins=["http://localhost:5173"],
)
API
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":"...", "usage":{...}, "conversationId":"..."}
event: errorwith JSON:{"type":"error","error":{"message":"..."}}
Endpoints
POST /chat/stream: stream assistant tokens (SSE)GET /chat/history?userId=...: get message history for a userPOST /chat/clear: clear a user’s historyGET /chat/conversations?userId=...: list conversations (SQLite mode)GET /chat/conversation?userId=...&conversationId=...: fetch one conversation (SQLite mode)POST /chat/conversation/create: create/ensure a conversation (SQLite mode)POST /chat/conversation/delete: delete a conversation (SQLite mode)
Publish to PyPI (maintainers)
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/*
Author
- Name: Shrikant Jagtap
- Email: shrijagtap11@gmail.com
Metadata
Release files for fastapi-ai-chat 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fastapi_ai_chat-1.0.2.tar.gz | 14.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_ai_chat-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.3 kB
Release files / fastapi_ai_chat-1.0.2.tar.gz
| Download URL | fastapi_ai_chat-1.0.2.tar.gz |
|---|---|
| Size | 14.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9e7b8824abbc8bbe16b707d398cae01464c465d90524da2cf0e0380df68b6d2e
|
|
BLAKE2b-256 checksum How to use checksums |
c9b9a741320e361026d05842246312abeb4aa8200464458ed410d073a91b4f15
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Release files / fastapi_ai_chat-1.0.2-py3-none-any.whl
| Download URL | fastapi_ai_chat-1.0.2-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f9093d8a2710e8726672104d6ac80b8efe3d7e8c397da4926dbab8c83f17cb7f
|
|
BLAKE2b-256 checksum How to use checksums |
0461de6b95eece1857336bef1c93bfe5e7301e73eeb92576c6fb3c9249747f22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Security for fastapi-ai-chat 1.0.2
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.