Skip to main content
Streaming lets your application display or process model output progressively — useful for chat interfaces, long-form generation, and real-time feedback.

Basic streaming

Use ai.generateStream() instead of ai.generate(). It returns an object with two properties:
  • stream — an async iterable of GenerateResponseChunk objects.
  • response — a Promise<GenerateResponse> that resolves when generation is complete.

The GenerateResponseChunk object

Each chunk delivered by the stream is a GenerateResponseChunk with the following properties:

Using onChunk callback

As an alternative to async iteration, you can pass an onChunk callback directly to generate(). This is useful when you want the final response object but also want to react to chunks:
onChunk and generateStream() both stream the same underlying chunks. Use generateStream() when you want async iteration syntax; use onChunk when you only need a side effect and still want the final Promise<GenerateResponse>.

Streaming within a flow

Streaming works inside flows. Use generateStream() the same way you would outside of a flow:

Streaming structured output

You can stream structured output by combining generateStream() with an output.schema. Each chunk’s output property contains the partial JSON parsed so far:
For streaming structured output with the jsonl format, each chunk contains a complete JSON object on its own line. This is useful for streaming arrays of items one element at a time.

Streaming in a web server

When deploying flows as HTTP endpoints, you can stream the response to the client using server-sent events (SSE) or chunked transfer encoding. The Genkit flow server handles this automatically when a client sends a streaming request.

Prompt streaming

Prompts defined with ai.definePrompt() or loaded from .prompt files also support streaming via the .stream() method:

Structured output

Stream typed JSON output chunk by chunk.

Flows

Define streamable flows with typed stream schemas.

Agents

Stream multi-turn agent loops.

Deployment

Deploy streaming flows to production.