Enable Streaming for Near-Instant Response Feedback
Reduce waiting times by streaming responses in your custom AI apps.
Implement the 'generateContentStream' method in your Gemini API calls to display text to users in real-time as it is generated.
The Scenario
You are building a custom internal tool for your team to summarize long meeting transcripts and want the interface to feel fast and responsive.
Before & after
Manually Waiting for a full text generation often results in a 'loading' state where the user sees nothing until the entire paragraph is complete, taking 15-30 seconds.
Using the Gemini API with streaming, chunks of the response appear as they are generated, letting the user start reading immediately. This reduces perceived latency to about 2-3 seconds for the first word.
The Prompt
You are a developer using Node.js. Implement a streaming response using the Gemini SDK:
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI([YOUR_API_KEY]);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
async function run() {
const prompt = "[DESCRIBE_YOUR_TASK_HERE]";
const result = await model.generateContentStream(prompt);
for await (const chunk of result.stream) {
const chunkText = chunk.text();
process.stdout.write(chunkText);
}
}
run();Streaming is particularly useful for longer creative writing or technical explanations where the user can begin consuming the output while the rest is being calculated.
Source
Release notes | Gemini API | Google AI for Developers"we'll use streaming for faster interactions we'll also take a look"
