Why proxy at all
Two reasons your production app probably wants a Gemini proxy in front:
- Hide the API key. The browser never sees credentials. The blast radius of a compromised browser becomes "can call your proxy" instead of "can call Gemini directly with your key."
- Add business logic. Auth, rate-limiting, request validation, response logging, model selection — all in one server-side place.
The pattern
FastAPI's StreamingResponse + httpx.AsyncClient.stream(). Open a stream to Gemini, forward chunks to the client, close when done. Total: ~30 lines for a working proxy.
SSE vs raw passthrough
Two designs:
- Raw passthrough: forward Gemini's bytes directly. Cheapest, but client must speak Gemini's exact format.
- Re-emit as your own SSE format: parse each chunk, extract
text, emit your owndata: {"text": "..."}events. More work but decouples client from Gemini's evolving schema.