Live token rendering
Display model output incrementally in a chat interface.
AI Agents · Const
streamEvents exposes an asynchronous stream of runnable lifecycle events while a graph executes. Each event identifies its type, source runnable, and execution ID, enabling real-time progress handling and selective filtering.
streamEvents returns an asynchronous event stream for a runnable or compiled graph. Each event identifies the lifecycle phase, the runnable that emitted it, and its execution ID, allowing applications to observe intermediate progress and filter events such as streamed model output.
streamEvents(input, options, streamOptions?): IterableReadableStream<StreamEvent>| Name | Type | Description |
|---|---|---|
input | null | InputType | CommandType | Input passed to the runnable or compiled graph. |
options | Partial<PregelOptions<...> & { version: "v1" | "v2" }> | Options including the required stream event version, such as version: "v2". |
streamOptions | Omit<EventStreamCallbackHandlerInput, "autoClose"> | Optional callback-handler streaming configuration, excluding autoClose. |
IterableReadableStream<StreamEvent>
name field to identify which runnable produced a notification.run_id to distinguish separate executions of the same runnable.on_chat_model_stream; event names are not arbitrary labels.const stream = await graph.streamEvents(input, { version: "v2" });
for await (const event of stream) {
if (event.event === "on_chat_model_stream") {
process.stdout.write(event.data?.chunk?.content ?? "");
}
}Iterates over runnable events and prints streamed chat-model content as it arrives.
Display model output incrementally in a chat interface.
Show progress from intermediate nodes and tools during a long graph run.
Inspect which runnable emitted each lifecycle notification.
streamEvents provide?streamEvents creates an iterator of StreamEvent objects that reports runnable progress in real time. The event object includes the event name, the emitting runnable's name, and a run ID.
Follow-up: How would you select only generated tokens? Filter for the appropriate model stream lifecycle event, such as on_chat_model_stream, and read its chunk data.
Event names follow the pattern on_[runnable_type]_(start|stream|end). This lets consumers distinguish invocation start, incremental output, and completion for each runnable type.
Follow-up: How can nested execution be correlated? Use each event's run ID and preserve parent-child relationships when the surrounding event data provides them.
streamEvents return and how is it consumed?The method accepts an input and options containing a stream-events version, and returns an IterableReadableStream<StreamEvent>. In JavaScript, consume it with for await...of.
Follow-up: Why choose version v2 in current code? It is an explicitly supported version in the documented signature and should be passed rather than relying on an implicit default.
streamStreamEventEventStreamCallbackHandleron_chat_model_starton_chat_model_streamon_chat_model_end