Skip to main content

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/stream returns text/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_KEY or FASTAPI_AI_CHAT_OPENAI_API_KEY
  • ANTHROPIC_API_KEY or FASTAPI_AI_CHAT_ANTHROPIC_API_KEY
  • GEMINI_API_KEY or FASTAPI_AI_CHAT_GEMINI_API_KEY
  • FASTAPI_AI_CHAT_SQLITE_PATH (default: ./chat_history.sqlite3)

Streaming protocol (SSE)

The server emits frames separated by a blank line (\n\n):

  • event: token with JSON: {"type":"token","token":"..."} (many times)
  • event: done with JSON: {"type":"done","message":{"role":"assistant","content":"..."}, "provider":"...", "conversationId":"..."}
  • event: error with 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 with claude)
  • Gemini: gemini-1.5-pro (or any model starting with gemini)

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

fastapi_ai_chat-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

fastapi_ai_chat-0.1.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

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

Hashes for fastapi_ai_chat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ff740a5329db668f0c7d3cf06f6b9921ee623e1e4475190f4f9f65a3f9a6e067
MD5 974ffe1d8ffe614ee22f693ea1e5eb24
BLAKE2b-256 936b72ec696d04c6f25de0904c7e2292f7ad751827f5652329536d095e4f8e15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_ai_chat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc0738a6332506e49f5390eda4b406ce89600e7f38063e051c64cc04d2f1ee31
MD5 1c9eb05b5902e804fe8cb5e2503cf1df
BLAKE2b-256 abe00c2ecc399642928460f208f43bcf207c6af6e1edf72910fe5d6efa320131

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