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 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/stream returns text/event-stream
  • emits SSE events: token, done, error

Features

  • Token streaming over SSE: incremental token events + final done event (or error).
  • 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
  • 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_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
  • Persistence
    • FASTAPI_AI_CHAT_SQLITE_PATH (default: ./chat_history.sqlite3)
  • Optional provider base URL overrides
    • OPENAI_BASE_URL / FASTAPI_AI_CHAT_OPENAI_BASE_URL
    • ANTHROPIC_BASE_URL / FASTAPI_AI_CHAT_ANTHROPIC_BASE_URL
    • GEMINI_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 with claude)
  • Gemini: gemini-1.5-pro (or any model starting with gemini)

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_from_env

app = create_app_from_env()

Or pass configuration explicitly:

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: token with JSON: {"type":"token","token":"..."} (many times)
  • event: done with JSON:
    • {"type":"done","message":{"role":"assistant","content":"..."}, "provider":"...", "usage":{...}, "conversationId":"..."}
  • event: error with JSON: {"type":"error","error":{"message":"..."}}

Endpoints

  • POST /chat/stream: stream assistant tokens (SSE)
  • GET /chat/history?userId=...: get message history for a user
  • POST /chat/clear: clear a user’s history
  • GET /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

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-1.0.0.tar.gz (14.9 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-1.0.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_ai_chat-1.0.0.tar.gz.

File metadata

  • Download URL: fastapi_ai_chat-1.0.0.tar.gz
  • Upload date:
  • Size: 14.9 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-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c7e9125c557cd42770591d7ac46bd7252451b34c98a2c3616e3cee62b6700b80
MD5 b1d8de3dbcada088d3942ad53d31d77e
BLAKE2b-256 8102d7a226d30d18175033158a9fdb8f9a596f72618eca1a10d2d919b162c2ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_ai_chat-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be41489162b3b77401ff2035f3f3d2ca7dd1861b16501632b81e666421048181
MD5 6a22868dc3776650ada21d9a6ac43ccb
BLAKE2b-256 fddd356b23784aae2fa203f48bb2d909b6c194cedde4ac8a3c6f9b3e5aae807e

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