Fluxio
← Retour au blog

Gemini streamGenerateContent with alt=sse: Step by Step

Courants de modèlesEN

§1 Understand What alt=sse Changes

Gemini streamGenerateContent returns generated content incrementally instead of waiting for one completed response. Adding alt=sse requests server-sent event framing: individual events carry JSON response objects. This walkthrough uses Gemini 2.5 Flash to explain the native REST flow before discussing an alternative relay integration. ([ai.google.dev](https://ai.google.dev/api/generate-content?utm_source=openai))

SSE is a transport format, not a promise that each event contains exactly one token. Build your interface around incoming text fragments rather than counting events as tokens. Google describes the output as streamed response chunks. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/generate-content/text-generation?hl=en))

§2 Prepare the Gemini POST Request

Store your Google API key in a server-side environment variable named GEMINI_API_KEY. Authenticate through the x-goog-api-key header and set Content-Type to application/json. Google's REST examples use this header-based authentication pattern. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/generate-content/text-generation?hl=en))

Target Google's generativelanguage.googleapis.com host with the path /v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse. Send a POST request whose JSON body is {"contents":[{"role":"user","parts":[{"text":"Explain SSE in three short sentences."}]}]}. The contents array holds conversation turns, while parts holds the prompt's content. ([ai.google.dev](https://ai.google.dev/api/generate-content?utm_source=openai))

For a terminal smoke test, use curl with --no-buffer, your authentication header, and that JSON body. Watch events arrive progressively before implementing application rendering; Google's streaming examples use this option to avoid curl's output buffering. ([ai.google.dev](https://ai.google.dev/api/generate-content?utm_source=openai))

§3 Read the POST Response Incrementally

In JavaScript, make the request with fetch, setting method, headers, and the JSON-stringified body. Check response.ok before reading the stream: fetch does not automatically reject a request merely because the server returns an HTTP error status. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch))

Read response.body incrementally and decode its bytes with a streaming text decoder. Avoid response.json() for this response because it consumes the body to completion rather than exposing successive SSE events. Preserve partially decoded input between reads. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch))

§4 Parse Complete SSE Events Before JSON

Keep a text buffer and use a standards-compliant SSE parser. Events end at blank lines, and multiple data fields within one event are joined with newline characters. A network read can contain an incomplete event or several events, so do not treat each read as a complete JSON document. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events))

  • – Recognize SSE line endings, including both LF and CRLF sequences.
  • – Ignore comment lines and collect the event's data fields.
  • – Parse the assembled data payload only after its event boundary arrives.
  • – Retain unfinished input for the next network read.

These rules follow SSE framing rather than assumptions about how network packets arrive. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events))

§5 Append Gemini Text and Inspect Completion Metadata

For each parsed response, inspect candidates and iterate through the selected candidate's content.parts. Append user-facing text parts in order, while handling non-text parts separately and excluding parts marked as thoughts. Do not assume every event contains candidates[0].content.parts[0].text. ([ai.google.dev](https://ai.google.dev/api/generate-content?utm_source=openai))

Track finishReason, inspect promptFeedback when candidates are absent, and retain reported usageMetadata. Do not add repeated cumulative usage reports together. A closed connection alone should not replace checking the model's completion or blocking information. ([ai.google.dev](https://ai.google.dev/api/generate-content?utm_source=openai))

§6 Test Cancellation and Choose the Fluxio Integration

Add cancellation with AbortController and test stopping generation while output is arriving. Also test your actual deployment path for buffering; streaming infrastructure must forward events promptly rather than accumulate the entire response. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch))

Fluxio fits when your product needs a streaming-first, pay-per-token relay with one key across providers. Its supplied integration details describe OpenAI SDK compatibility and token-by-token replies. For an OpenAI-style client, use the Fluxio API host api.fluxioapi.com with the /v1 base path.

Keep that integration separate from this native Google walkthrough. Do not assume changing the hostname preserves Google's streamGenerateContent route, authentication, or response schema. Use Fluxio's documented model identifier and streaming contract for the chosen client.

Sign up for Fluxio to build your streaming experience.

Créer un compte Fluxio →

Plus d'articles du courant