LangGraph SDK and LangGraph Server are a part of LangGraph Platform.
Basic usage
Basic usage example:Extended example: streaming updates
Extended example: streaming updates
This is an example graph you can run in the LangGraph API server.
See LangGraph Platform quickstart for more details.Once you have a running LangGraph API server, you can interact with it using
LangGraph SDK
- The
client.runs.stream()
method returns an iterator that yields streamed outputs. 2. Setstream_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.
Supported stream modes
Mode | Description | LangGraph Library Method |
---|---|---|
values | Stream the full graph state after each super-step. | .stream() / .astream() with stream_mode="values" |
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. | .stream() / .astream() with stream_mode="updates" |
messages-tuple | Streams LLM tokens and metadata for the graph node where the LLM is invoked (useful for chat apps). | .stream() / .astream() with stream_mode="messages" |
debug | Streams as much information as possible throughout the execution of the graph. | .stream() / .astream() with stream_mode="debug" |
custom | Streams custom data from inside your graph | .stream() / .astream() with stream_mode="custom" |
events | Stream all events (including the state of the graph); mainly useful when migrating large LCEL apps. | .astream_events() |
Stream multiple modes
You can pass a list as thestream_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.
Stream graph state
Use the stream modesupdates
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.
Example graph
Example graph
Stateful runs
Examples below assume that you want to persist the outputs of a streaming run in the checkpointer DB and have created a thread. To create a thread:If you don’t need to persist the outputs of a run, you can pass
None
instead of thread_id
when streaming.Stream Mode: updates
Use this to stream only the state updates returned by the nodes after each step. The streamed outputs include the name of the node as well as the update.
Stream Mode: values
Use this to stream the full state of the graph after each step.
Subgraphs
To include outputs from subgraphs in the streamed outputs, you can setsubgraphs=True
in the .stream()
method of the parent graph. This will stream outputs from both the parent graph and any subgraphs.
- Set
stream_subgraphs=True
to stream outputs from subgraphs.
Extended example: streaming from subgraphs
Extended example: streaming from subgraphs
This is an example graph you can run in the LangGraph API server.
See LangGraph Platform quickstart for more details.Once you have a running LangGraph API server, you can interact with it using
LangGraph SDKNote that we are receiving not just the node updates, but we also the namespaces which tell us what graph (or subgraph) we are streaming from.
- Set
stream_subgraphs=True
to stream outputs from subgraphs.
Debugging
Use thedebug
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.
LLM tokens
Use themessages-tuple
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-tuple
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.
Example graph
Example graph
- Note that the message events are emitted even when the LLM is run using
.invoke
rather than.stream
.
- The “messages-tuple” stream mode returns an iterator of tuples
(message_chunk, metadata)
wheremessage_chunk
is the token streamed by the LLM andmetadata
is a dictionary with information about the graph node where the LLM was called and other information.
Filter LLM tokens
- To filter the streamed tokens by LLM invocation, you can associate
tags
with LLM invocations. - To stream tokens only from specific nodes, use
stream_mode="messages"
and filter the outputs by thelanggraph_node
field in the streamed metadata.
Stream custom data
To send custom user-defined data:Stream events
To stream all events, including the state of the graph:Stateless runs
If you don’t want to persist the outputs of a streaming run in the checkpointer DB, you can create a stateless run without creating a thread:- We are passing
None
instead of athread_id
UUID.
Join and stream
LangGraph Platform allows you to join an active background run and stream outputs from it. To do so, you can use LangGraph SDK’sclient.runs.join_stream
method:
- This is the
run_id
of an existing run you want to join.
Outputs not buffered
When you use
.join_stream
, output is not buffered, so any output produced before joining will not be received.