generateContentStream()
Streams the response token by token as it is generated. Use for long outputs or real-time interfaces to reduce perceived latency.
Syntax
gemini-api
const stream = await model.generateContentStream(prompt)
for await (const chunk of stream.stream) { ... }Example
gemini-api
const stream = await model.generateContentStream(
"Write a comprehensive guide to TypeScript generics."
);
for await (const chunk of stream.stream) {
process.stdout.write(chunk.text());
}
// Wait for the full response if needed
const fullResponse = await stream.response;
console.log("Total tokens:", fullResponse.usageMetadata?.totalTokenCount);