In this categoryGemini ยท 24
- How to Install the Gemini CLI and Run Your First PromptStart
- How to Get a Gemini API Key from Google AI Studio
- How to Call the Gemini API from Python
- How to Give Gemini CLI Project Context with a GEMINI.md File
- How to Choose the Right Gemini Model for Coding Tasks
- How to Fix a Bug in Your Codebase with the Gemini CLI
- How to Set Up Gemini Code Assist in VS Code
- How to Stream Gemini Responses in Node.js
- How to Feed a Long Document into Gemini's Long Context
- How to Run the Gemini CLI Non-Interactively in Scripts
- How to Analyze a Video File with the Gemini API
- How to Connect an MCP Server to the Gemini CLI
- How to Fix Common Gemini CLI Authentication Errors
- How to Cache Long Context in the Gemini API to Cut Costs
- How to Summarize a YouTube Video with Gemini
- How to Analyze PDFs and Docs in the Gemini Chat App
- How to Extract Data from Images and Screenshots with Gemini
- How to Generate a Short Video with Veo in Gemini
- How to Ground Gemini Answers in Your Own Documents
- How to Use Gemini Inside Google Docs to Draft Faster
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
16:12
3:58:44
12:00
09:00
16:10
12:25New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.