14. How to convert input and output of subgraph

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

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

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

Copy

Visualize the graph.

Copy

Copy

Copy

child Justice

Copy

Copy

Copy

Copy

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

Copy

Visualize the graph.

Copy

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.

Copy

Copy

Last updated