06. Node's step-by-step streaming output

This time LangGrpah stream() Proceed with a little more detailed explanation of the output function.

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

Note: The LangGraph example below is the same as the example in the previous section.

Copy

# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv

# Load API key information
load_dotenv()

Copy

 True 

Copy

# Set up LangSmith tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging

# Enter a project name.
logging.langsmith("CH17-LangGraph-Modules")

Copy

Copy

StateGraph stream method

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

parameter

  • 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]]]): Key to stream

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

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

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

  • subgraphs (bool): Whether subgraph streaming

return value

  • Iterator[Union[dict[str, Any], Any]]: Output of each step of the graph. Output form stream_mode Depends on

Main features

  1. Stream graph execution according to entered settings

  2. Support for various streaming modes ( values , updates , debug )

  3. Callback management and error handling

  4. Treatment of recursive restrictions and suspension conditions

Streaming mode

  • values : Output the current state value for each step

  • updates : Output only status updates for each step

  • debug : Output of debug events at each stage

Copy

config Set up and proceed to streaming output.

Copy

Copy

output_keys option

output_keys The option is used to specify the key to stream.

can be specified in list format, One of the keys defined in the channels Should be.

Tip

  • This is useful if you have a lot of State key outputs every step, and you only want to stream some.

Copy

Copy

Copy

Copy

Copy

Copy

stream_mode option

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

  • values : Output the current state value for each step

  • updates : Output only status updates for each step (default)

stream_mode = "values"

values Mode outputs the current state value for each step.

Reference

event.items()

  • key : State key value

  • value : Value for State's key

Copy

Copy

stream_mode = "updates"

updates Mode only exports updated State for each step.

  • The output is the node name as key, and the updated value is values dictionary is.

Reference

event.items()

  • key : Name of Node

  • value : Output value (dictionary) at that node stage. That is, it is a dictionary with multiple key-value pairs.

Copy

Copy

interrupt_before Wow interrupt_after option

interrupt_before Wow interrupt_after Options are used to specify when streaming stops.

  • interrupt_before : Stop streaming before the specified node

  • interrupt_after : Stop streaming after the specified node

Copy

Copy

Copy

Copy

Last updated