{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# How to transform inputs and outputs of a subgraph\n", "\n", "It's possible that your subgraph state is completely independent from the parent graph state, i.e. there are no overlapping channels (keys) between the two. For example, you might have a supervisor agent that needs to produce a report with a help of multiple ReAct agents. ReAct agent subgraphs might keep track of a list of messages whereas the supervisor only needs user input and final report in its state, and doesn't need to keep track of messages.\n", "\n", "In such cases you need to transform the inputs to the subgraph before calling it and then transform its outputs before returning. This guide shows how to do that.\n", "\n", "## Setup\n", "\n", "First, let's install the required packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install -U langgraph" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Set up LangSmith for LangGraph development
\n", "\n", " Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here. \n", "
\n", "Note
\n", "\n",
" We're wrapping the grandchild_graph
invocation in a separate function (call_grandchild_graph
) that transforms the input state before calling the grandchild graph and then transforms the output of grandchild graph back to child graph state. If you just pass grandchild_graph
directly to .add_node
without the transformations, LangGraph will raise an error as there are no shared state channels (keys) between child and grandchild states.\n",
"
child_graph
`)\n",
"parent.add_node(\"child\", call_child_graph)\n",
"parent.add_node(\"parent_2\", parent_2)\n",
"\n",
"parent.add_edge(START, \"parent_1\")\n",
"parent.add_edge(\"parent_1\", \"child\")\n",
"parent.add_edge(\"child\", \"parent_2\")\n",
"parent.add_edge(\"parent_2\", END)\n",
"\n",
"parent_graph = parent.compile()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note
\n", "\n",
" We're wrapping the child_graph
invocation in a separate function (call_child_graph
) that transforms the input state before calling the child graph and then transforms the output of the child graph back to parent graph state. If you just pass child_graph
directly to .add_node
without the transformations, LangGraph will raise an error as there are no shared state channels (keys) between parent and child states.\n",
"