Basic streaming
Useai.generateStream() instead of ai.generate(). It returns an object with two properties:
stream— an async iterable ofGenerateResponseChunkobjects.response— aPromise<GenerateResponse>that resolves when generation is complete.
- TypeScript
- Python
- Go
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. UsegenerateStream() the same way you would outside of a flow:
Streaming structured output
You can stream structured output by combininggenerateStream() with an output.schema. Each chunk’s output property contains the partial JSON parsed so far:
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 withai.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.
