
Build a chatbot in FastAPI
Python-side streaming with FastAPI and the OpenAI SDK.
FastAPI + StreamingResponse gives you SSE in a few lines. Perfect for Python-heavy stacks.
python
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from openai import OpenAI
app = FastAPI()
ai = OpenAI(api_key="YOUR_TOKENREX_API_KEY", base_url="https://tokenrex.ai/api/public/v1")
@app.post("/chat")
def chat(body: dict):
stream = ai.chat.completions.create(model="gpt-5.4-mini", messages=body["messages"], stream=True)
def gen():
for chunk in stream:
yield f"data: {chunk.model_dump_json()}\n\n"
return StreamingResponse(gen(), media_type="text/event-stream")Models in this article
Review current prices, specifications and source-linked benchmarks.
Build this with TokenRex
TokenRex is an LLM marketplace by Asirex Labs. Choose an enabled model, create a private API key and use the supported endpoint shown in Docs. Check live prices before your first request.