Skip to main content

Streaming Responses with Gemini

Get responses as they are generated, useful for real-time applications.

import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });

const result = await model.generateContentStream(
"Tell a fairy tale about a wise owl."
);

for await (const chunk of result.stream) {
process.stdout.write(chunk.text());
}

Streaming Options

  • chunkSize: Size of response chunks
  • timeout: Maximum time to wait for chunks

Best Practices for Streaming

  1. Implement proper error handling
  2. Consider implementing a timeout
  3. Handle partial responses appropriately