Gemini generateContent and streamGenerateContent
Choose how Gemini delivers its answer
Gemini generateContent and streamGenerateContent share a basic purpose: send content to a model and receive generated output. The difference is delivery. generateContent returns one response after generation, while streamGenerateContent delivers response chunks as they become available. Start with the complete-response method, then add streaming when your interface needs incremental output. ([ai.google.dev](https://ai.google.dev/api?utm_source=openai))
Follow these numbered steps to build a small text-generation integration before adding conversation history or production safeguards.
Prepare credentials for your first Gemini request
1. Create a Gemini API key for direct access through Google, and store it on your server rather than embedding it in browser code. Keep credentials outside source control and load them through your deployment environment. Google's API key guidance explains authentication and key protection. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/api-key))
For this walkthrough, use Gemini 2.5 Flash if it is available to your account. Verify the exact model identifier through your provider rather than assuming a display name can be pasted into a request.
Build a minimal generateContent request
2. Send a POST request to the model resource ending in :generateContent. For Google's v1beta REST API, the relative path follows /v1beta/models/{model}:generateContent. Authenticate with the x-goog-api-key header and set Content-Type to application/json. The request body contains contents, whose entries contain parts. ([ai.google.dev](https://ai.google.dev/api/generate-content))
A minimal text body is {"contents":[{"role":"user","parts":[{"text":"Explain HTTP caching in three sentences."}]}]}. Keep this first prompt short and specific. Save the request body so you can reuse exactly the same input when comparing complete and streamed responses. ([ai.google.dev](https://ai.google.dev/api/generate-content))
Read the complete Gemini response safely
3. Inspect the response before extracting text. Native REST responses place generated content inside candidates and their content.parts. Do not assume candidates always exists or that every part contains text. Check promptFeedback when the prompt is blocked, and inspect finishReason to understand why generation stopped. ([ai.google.dev](https://ai.google.dev/api/generate-content))
The Google Gen AI JavaScript SDK offers ai.models.generateContent and a response.text convenience property. Choose either SDK helpers or direct REST for your first implementation; mixing their response conventions makes debugging harder. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/generate-content/text-generation))
Switch the same prompt to streamGenerateContent
4. Change the REST method suffix to :streamGenerateContent and request Server-Sent Events with ?alt=sse. Keep the same contents body. In the JavaScript SDK, the corresponding method is ai.models.generateContentStream, which you consume asynchronously as chunks arrive. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/generate-content/text-generation))
Append incoming text to your interface and accumulate it separately if you need a final saved answer. Treat chunks as partial results, not complete messages. Streaming changes when output becomes visible; do not treat it as a promise that the entire request finishes sooner. ([ai.google.dev](https://ai.google.dev/api?utm_source=openai))
Preserve conversation history and handle interrupted output
5. For follow-up questions, include the relevant earlier user and model turns in contents. These generation methods do not automatically remember a previous independent request. SDK chat helpers manage history for you, but send that history with subsequent turns. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/generate-content/text-generation))
Before shipping, test the failure paths deliberately.
- Inspect authentication and request errors before retrying unchanged inputs. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/troubleshooting))
- Use bounded exponential backoff for retryable conditions such as rate limiting or temporary unavailability. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/troubleshooting))
- Mark an interrupted stream as incomplete instead of presenting partial text as a finished answer.
Choose Relay365 when a shared integration fits
6. If you work across model families, consider Relay365 as a pay-as-you-go relay desk. It supports workflows using Claude Code, Codex, Gemini CLI, and OpenAI SDK clients. Its API base is https://api.api365relay.com; OpenAI clients add /v1.
Keep protocol compatibility separate from model access. An OpenAI-compatible request is not the same payload as Gemini-native generateContent. Confirm Relay365's supported routes, model identifiers, and streaming behavior before adapting this walkthrough. If a shared relay fits your workflow, create a Relay365 account and begin with one small request. Members can also earn a referral bounty when people they invite pay.
Further desk bulletins
| Date | Bulletin title | Filed under | Language |
|---|---|---|---|
| 2026-10-24 | Prepaid Wallets vs Monthly Passes: Do the Math | Model watch | EN |
| 2026-10-24 | Context Windows: What Actually Fits Inside | Model watch | EN |
| 2026-10-24 | Picking a Model for Your Customer Support Bot | Model watch | EN |
| 2026-10-24 | Which Model Should Summarisation Traffic Go To? | Model watch | EN |
| 2026-10-24 | Claude Fable 5.1: When the Top Tier Is Worth It | Model watch | EN |
| 2026-10-24 | Hunyuan 4 for Chinese Support Drafts: 7 Steps | Model watch | EN |
Curious how it runs on your own code? Sign up →