API & Backend

Real-Time AI Streaming with FastAPI and Google Gemini API (SSE)

Learn how to build low-latency Server-Sent Events (SSE) streaming endpoints in FastAPI using the official Google GenAI SDK.

3 min

Why SSE Instead of Standard JSON Responses?

Waiting 5 to 10 seconds for an LLM to generate an entire response causes high perceived latency. Server-Sent Events (SSE) stream tokens to the frontend the instant they are generated, reducing Time to First Token (TTFT) to under 300ms.

main.py
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from google import genai

app = FastAPI()
client = genai.Client()

@app.get("/api/stream")
async def stream_ai_response(prompt: str):
    async def event_generator():
        response = client.models.generate_content_stream(
            model="gemini-3.7-flash",
            contents=prompt,
        )
        for chunk in response:
            if chunk.text:
                yield f"data: {chunk.text}\n\n"

    return StreamingResponse(event_generator(), media_type="text/event-stream")