stream()
or astream()
methods:
Mode | Description |
---|---|
values | Streams the full value of the state after each step of the graph. |
updates | Streams the updates to the state after each step of the graph. If multiple updates are made in the same step (e.g., multiple nodes are run), those updates are streamed separately. |
custom | Streams custom data from inside your graph nodes. |
messages | Streams 2-tuples (LLM token, metadata) from any graph nodes where an LLM is invoked. |
debug | Streams as much information as possible throughout the execution of the graph. |
stream()
or astream()
methods with stream_mode="updates"
. This emits an event after every agent step.
For example, if you have an agent that calls a tool once, you should see the following updates:
stream_mode="messages"
:
get_stream_writer
inside your tool, you won’t be able to invoke the tool outside of a LangGraph execution context.stream_mode=["updates", "messages", "custom"]
:
.stream()
(sync) and .astream()
(async) methods to yield streamed outputs as iterators.
Extended example: streaming updates
stream()
method returns an iterator that yields streamed outputs.
stream_mode="updates"
to stream only the updates to the graph state after each node. Other stream modes are also available. See supported stream modes for details.
stream_mode
parameter to stream multiple modes at once.
The streamed outputs will be tuples of (mode, chunk)
where mode
is the name of the stream mode and chunk
is the data streamed by that mode.
updates
and values
to stream the state of the graph as it executes.
updates
streams the updates to the state after each step of the graph.values
streams the full value of the state after each step of the graph.subgraphs=True
in the .stream()
method of the parent graph. This will stream outputs from both the parent graph and any subgraphs.
The outputs will be streamed as tuples (namespace, data)
, where namespace
is a tuple with the path to the node where a subgraph is invoked, e.g. ("parent_node:<task_id>", "child_node:<task_id>")
.
subgraphs=True
to stream outputs from subgraphs.Extended example: streaming from subgraphs
subgraphs=True
to stream outputs from subgraphs.debug
streaming mode to stream as much information as possible throughout the execution of the graph. The streamed outputs include the name of the node as well as the full state.
messages
streaming mode to stream Large Language Model (LLM) outputs token by token from any part of your graph, including nodes, tools, subgraphs, or tasks.
The streamed output from messages
mode is a tuple (message_chunk, metadata)
where:
message_chunk
: the token or message segment from the LLM.metadata
: a dictionary containing details about the graph node and LLM invocation.
If your LLM is not available as a LangChain integration, you can stream its outputs using custom
mode instead. See use with any LLM for details.
RunnableConfig
to ainvoke()
to enable proper streaming. See Async with Python < 3.11 for details or upgrade to Python 3.11+..invoke
rather than .stream
.(message_chunk, metadata)
where message_chunk
is the token streamed by the LLM and metadata
is a dictionary with information about the graph node where the LLM was called and other information.tags
with LLM invocations to filter the streamed tokens by LLM invocation.
stream_mode
is set to “messages” to stream LLM tokens. The metadata
contains information about the LLM invocation, including the tags.tags
field in the metadata to only include the tokens from the LLM invocation with the “joke” tag.Extended example: filtering by tags
stream_mode="messages"
and filter the outputs by the langgraph_node
field in the streamed metadata:
(message_chunk, metadata)
where message_chunk
is the token streamed by the LLM and metadata
is a dictionary with information about the graph node where the LLM was called and other information.langgraph_node
field in the metadata to only include the tokens from the write_poem
node.Extended example: streaming LLM tokens from specific nodes
(message_chunk, metadata)
where message_chunk
is the token streamed by the LLM and metadata
is a dictionary with information about the graph node where the LLM was called and other information.
langgraph_node
field in the metadata to only include the tokens from the write_poem
node.
get_stream_writer()
to access the stream writer and emit custom data.stream_mode="custom"
when calling .stream()
or .astream()
to get the custom data in the stream. You can combine multiple modes (e.g., ["updates", "custom"]
), but at least one must be "custom"
.get_stream_writer()
in async for Python < 3.11
In async code running on Python < 3.11, get_stream_writer()
will not work.
Instead, add a writer
parameter to your node or tool and pass it manually.
See Async with Python < 3.11 for usage examples.stream_mode="custom"
to receive the custom data in the stream.stream_mode="custom"
to stream data from any LLM API — even if that API does not implement the LangChain chat model interface.
This lets you integrate raw LLM clients or external services that provide their own streaming interfaces, making LangGraph highly flexible for custom setups.
stream_mode="custom"
to receive the custom data in the stream.Extended example: streaming arbitrary chat model
disable_streaming=True
when initializing the model.
disable_streaming=True
to disable streaming for the chat model.context
parameter.
This limits LangGraph ability to automatically propagate context, and affects LangGraph’s streaming mechanisms in two key ways:
RunnableConfig
into async LLM calls (e.g., ainvoke()
), as callbacks are not automatically propagated.get_stream_writer()
in async nodes or tools — you must pass a writer
argument directly.Extended example: async LLM call with manual config
config
as an argument in the async node function.config
to llm.ainvoke()
to ensure proper context propagation.stream_mode="messages"
to stream LLM tokens.Extended example: async custom streaming with stream writer
writer
as an argument in the function signature of the async node or tool. LangGraph will automatically pass the stream writer to the function.stream_mode="custom"
to receive the custom data in the stream.