Fluxio
← Back to the blog

Gemini streamGenerateContent: Live Replies With SSE

Model tidesEN

§1 Gemini streamGenerateContent sends replies in pieces

A live AI reply feels different from a loading spinner followed by a wall of text. Words appear while the model is still generating its answer, allowing someone to start reading sooner. Gemini provides this behavior through streamGenerateContent, and adding alt=sse requests a response formatted as server-sent events.

For newcomers, the essential idea is simple: your application sends a prompt once, then reads an ongoing HTTP response instead of waiting for the complete answer. Streaming mainly improves perceived responsiveness. It does not guarantee that the model finishes generating the entire answer faster.

§2 alt=sse selects the event format

SSE stands for server-sent events. It is a text-based format that lets a server send successive events over an open HTTP response. With Gemini, those events carry JSON describing generated content and related response information.

The streamGenerateContent method chooses streaming generation, while alt=sse chooses the SSE representation. The query parameter alone does not turn a regular generation endpoint into a streaming endpoint. You need the streaming method as well.

Although people often describe this as token-by-token output, an event is not necessarily one token or one word. A chunk can contain several characters, multiple words, or information other than visible text. Build your interface around incoming text chunks, not assumed token boundaries.

§3 The native Gemini streaming request

For example, a native request using Gemini 2.5 Flash can target /v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse on Google's Gemini API host. Send a POST request with a JSON body containing your conversation content. For API-key authentication, supply your key through the x-goog-api-key header.

  • – Set the request content type to application/json and include the user's prompt in the contents structure.
  • – Keep the API key on your backend rather than embedding it in browser code.
  • – Check the HTTP status before treating the response body as a successful event stream.
  • – Read the response incrementally instead of calling a helper that waits for the whole body.

The browser's built-in EventSource interface is not a direct match for this request because it does not provide the POST body and custom headers you need. A backend HTTP client, a streaming fetch request, or an appropriate SDK is usually the practical choice.

§4 Reading SSE events without breaking the reply

An SSE event commonly contains a data: field, and a blank line separates events. Network reads do not necessarily align with these boundaries. One read might contain several events, or only part of one event.

Use an SSE parser or maintain a buffer that preserves incomplete data between reads. Decode UTF-8 incrementally, assemble complete events, and then parse their JSON payloads. In text responses, generated text typically appears inside candidate content parts. Inspect those parts rather than assuming every event contains text at a fixed position.

Append new text to the existing answer as it arrives. Also inspect finish reasons and available usage metadata. Do not assume Gemini's native stream ends with an OpenAI-style completion marker, and do not treat an interrupted connection as proof that generation completed successfully.

§5 Keeping Gemini live replies reliable

Your own infrastructure can accidentally hide streaming. A reverse proxy, hosting platform, or application layer may buffer the response until enough data accumulates. Test the entire path from the model to the user's screen, not just the upstream request.

Give users a stop control, cancel upstream work when appropriate, and distinguish an interrupted answer from a completed one. If you retry after partial output, avoid silently appending a fresh answer to the old text. That can create duplicated or contradictory replies.

§6 Fluxio for an OpenAI-style streaming workflow

Fluxio fits when you prefer a streaming-first, pay-per-token relay and an OpenAI-style client workflow. Its OpenAI-style API base is https://api.fluxioapi.com/v1. Keep that integration separate from the native Gemini request: a compatible client's streaming option is not the same interface as Google's streamGenerateContent endpoint with alt=sse.

Sign up for Fluxio to try streaming replies.

Create a Fluxio account →

More from the current