14. How to convert input and output of subgraph

subgraph How to convert input and output

subgraph state end parent graph It can be completely independent of the state.

In other words, there may not be a state keys that overlap between the two graphs.

In this case subgraph You need to convert the input before calling, and the output before returning.

Preferences

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

# Load API key information
load_dotenv()
 True 
# 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")
 Start tracking LangSmith. 
[Project name] 
CH17-LangGraph-Modules 

graph Wow subgraph Justice

Three as follows graph Let me define.

  • parent graph

  • parent graph To be called by child subgraph

  • child graph To be called by grandchild subgraph

grandchild Justice

Visualize the graph.

child Justice

grandchild_graph Separate function of call call_grandchild_graph ) Is wrapped.

This function converts the input state before calling the grandchild graph, and converts the output of the grandchild graph back to the child graph state.

If there is no such conversion grandchild_graph directly .add_node If you pass it to, LangGraph will get an error because there is no state key shared between the child and grandchild states.

important

child subgraph Wow grandchild subgraph has parent graph Your own not shared with Independent state Please note that you have.

parent Justice

Visualize the graph.

child_graph Separate function of call call_child_graph Wrapped in, this function converts the input state before calling the child graph and converts the output of the child graph back to the parent graph state.

Without conversion child_graph directly .add_node If you pass it to, LangGraph will get an error because there is no State Key shared between parent and child status. So, I'll run the parent graph to see if the child and grandson subgraphs are called correctly.

Last updated