LangGraph Streaming Outputs

Open in Colabarrow-up-rightOpen in GitHubarrow-up-right

Overview

LangGraph Streaming Outputs explores the step-by-step streaming capabilities within LangGraph.

This approach allows developers to observe and interact with each graph processing stage in real-time, offering insights into the internal workings and progress of graph computations.

  • Step-by-Step Streaming The streaming functionality breaks down the graph execution into individual steps, providing detailed outputs at each stage. This makes it easier to debug and understand how data flows through the graph, as well as how decisions are made along the way.

  • Real-Time Feedback By leveraging streaming outputs, users receive immediate feedback from the system. This real-time interaction can greatly enhance the debugging process and the ability to fine-tune graph configurations on the fly.

  • Enhanced Transparency and Control Streaming outputs offer greater transparency into the execution of LangGraph pipelines. Users can monitor each transformation or computation, enabling more precise control over the process and facilitating a deeper comprehension of complex graph-based workflows.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setuparrow-up-right for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorialarrow-up-right for more details.

Alternatively, environment variables can also be set using a .env file.

[Note]

  • This is not necessary if you've already set the environment variables in the previous step.

Step-by-step Streaming Output with LangGraph

This time, we'll take a closer look at the stream() output function in LangGraph.

LangGraph's streaming output function provides the ability to stream each step of the graph.

Note: The LangGraph example below is identical to the example from the previous section.

png

The stream Method of StateGraph

The stream method provides the ability to stream graph steps for a single input.

Parameters

  • input (Union[dict[str, Any], Any] ) : Input to the graph

  • config (Optional[RunnableConfig] ) : Execution configuration

  • stream_mode (Optional[Union[StreamMode, list[StreamMode]]] ) : Output streaming mode

  • output_keys (Optional[Union[str, Sequence[str]]] ) : Keys to stream

  • interrupt_before (Optional[Union[All, Sequence[str]]] ) : Nodes to interrupt before execution

  • interrupt_after (Optional[Union[All, Sequence[str]]] ) : Nodes to interrupt after execution

  • debug (Optional[bool] ) : Whether to output debug information

  • subgraphs (bool) : Whether to stream subgraphs

Returns

  • Iterator[Union[dict[str, Any], Any]] : Outputs from each step of the graph. The output format depends on stream_mode .

Key Features

  1. Processes graph execution in a streaming manner according to the given configuration

  2. Supports various streaming modes (values , updates , debug)

  3. Manages callbacks and error handling

  4. Handles recursion limits and interruption conditions

Streaming Modes

  • values : Outputs the current state values at each step

  • updates : Outputs only state updates at each step

  • debug : Outputs debug events at each step

We set up the config and proceed with streaming output.

Advanced Streaming Options

Advanced streaming options in LangGraph allow for more fine-grained control over how graph execution outputs are handled. By configuring parameters like output_keys, stream_mode, interrupt_before, and interrupt_after, users can tailor the streaming behavior to their specific debugging and monitoring needs.

The output_keys Option

The output_keys option is used to specify which keys to stream.

You can specify it in list format, and it must be one of the keys defined in channels.

Tip

  • If a large number of State keys is output at each step, this is useful when you only want to stream some of them.

The stream_mode Option

The stream_mode option is used to specify the streaming output mode.

  • values: Outputs the current state values at each step

  • updates: Outputs only state updates at each step (default)

stream_mode = "values"

In values mode, the current state values of each step are output.

Note

event.items()

  • key: Key of the State

  • value: The value corresponding to the key of the State

stream_mode = "updates"

In updates mode, only the updated State for each step is output.

  • The output is a dictionary whose key is the node name, and values are the updated outputs.

Note

event.items()

  • key: The node name

  • value: The output of that node in dictionary form (it can have multiple key-value pairs).

The interrupt_before and interrupt_after Options

The interrupt_before and interrupt_after options are used to specify when to interrupt streaming.

  • interrupt_before: Interrupt streaming before the specified node

  • interrupt_after: Interrupt streaming after the specified node

Last updated