How to Stream Gemini Responses in Node.js
Use the JavaScript SDK to stream a Gemini answer token by token instead of waiting for the full reply.
For chat interfaces and CLIs, waiting for a full Gemini response feels slow. Streaming prints text as it is generated, which makes the output appear instantly. This guide uses the official @google/genai package in Node to stream a response chunk by chunk.
What you need
- Node.js 20 or newer
- A Gemini API key in GEMINI_API_KEY
- A project set to ES modules (type module in package.json)
- About 6 minutes
Step 1: Install the JavaScript SDK
Add the official Google GenAI package to your project. It works in both ES module and CommonJS projects, but the example below uses modern import syntax.
npm install @google/genaiStep 2: Use the streaming method
Instead of generateContent, call generateContentStream. It returns an async iterable, so you loop over chunks with for await and write each piece to stdout as it arrives.
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({}); // reads GEMINI_API_KEY
const stream = await ai.models.generateContentStream({
model: "gemini-2.5-flash",
contents: "Explain what a race condition is, briefly.",
});
for await (const chunk of stream) {
process.stdout.write(chunk.text);
}
process.stdout.write("\n");Step 3: Run and watch it stream
Run the file. Rather than appearing all at once, the explanation types itself out across the terminal as Gemini produces it.
Result
Your Node program now renders Gemini output progressively. In a web app you would forward each chunk over a server-sent events stream or a websocket so the browser updates live, exactly the way a chat UI feels.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31