All posts
Build a chatbot in FastAPI
Tutorials 10 min read· 10 Aug 2026· By Tutorials

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="sk-tb-...", base_url="https://tokenbaazar.in/api/public/v1")

@app.post("/chat")
def chat(body: dict):
    stream = ai.chat.completions.create(model="openai/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")

Ready to build?

Grab a key, keep your OpenAI SDK, and pay in INR.

Start free