diff --git a/docs/docs/concepts/low_level.md b/docs/docs/concepts/low_level.md
index 9c562e093..75ff6d4a3 100644
--- a/docs/docs/concepts/low_level.md
+++ b/docs/docs/concepts/low_level.md
@@ -324,22 +324,31 @@ graph.add_conditional_edges("node_a", continue_to_jokes)
## `GraphCommand`
-Typically, LangGraph separates control flow (edges) from state updates (nodes). However, it is often beneficial to combine the two. For example, you might want to BOTH perform state updates AND decide which node to go next in the SAME node. LangGraph provides a way to combine control flow and node state updates using [`GraphCommand`][langgraph.graph.state.GraphCommand]. To do so, you can return a `GraphCommand` object from a node instead of a state update or `Send` objects.
+It can be useful to combine control flow (edges) and state updates (nodes). For example, you might want to BOTH perform state updates AND decide which node to go to next in the SAME node. LangGraph provides a way to do so by returning a [`GraphCommand`][langgraph.graph.state.GraphCommand] object from node functions:
+
+```python
+def my_node(state: State) -> GraphCommand[Literal["my_other_node"]]:
+ return GraphCommand(
+ # state update
+ update={"foo": "bar"},
+ # control flow
+ goto="my_other_node"
+ )
+```
`GraphCommand` has the following properties:
- - `goto`: optional, name of the node to navigate to next.
- If not specified, the graph will halt after executing the current superstep.
- - `graph`: optional, graph to send the command to. Supported values are:
- - `None`: the current graph (default)
- - `GraphCommand.PARENT`: parent graph.
- - `update`: optional, state update to apply to the graph's state at the current superstep.
- - `send`: optional, list of [`Send`](#send) objects to send to other nodes.
- - `resume`: optional, value to resume execution with. Will be used when `interrupt()` is called.
+| Property | Description |
+| --- | --- |
+| `graph` | Graph to send the command to. Supported values:
- `None`: the current graph (default)
- `GraphCommand.PARENT`: parent graph |
+| `goto` | Name of the node to navigate to next. Can be any node that belongs to the specified `graph` (current or parent). If `goto` not specified, the graph will halt after executing the current superstep. |
+| `update` | State update to apply to the graph's state at the current superstep |
+| `send` | List of [`Send`](#send) objects to send to other nodes |
+| `resume` | Value to resume execution with. Will be used when `interrupt()` is called |
```python
from langgraph.graph import GraphCommand, StateGraph, START
-from typing_extensions import TypedDict, Literal
+from typing_extensions import Literal, TypedDict
class State(TypedDict):
foo: str
@@ -368,6 +377,8 @@ def my_node(state: State) -> GraphCommand[Literal["my_other_node", "__end__"]]:
return GraphCommand(goto="__end__")
```
+Check out this [how-to guide](../how-tos/graph-command.ipynb) for an end-to-end example of how to use `GraphCommand`.
+
## Persistence
LangGraph provides built-in persistence for your agent's state using [checkpointers][langgraph.checkpoint.base.BaseCheckpointSaver]. Checkpointers save snapshots of the graph state at every superstep, allowing resumption at any time. This enables features like human-in-the-loop interactions, memory management, and fault-tolerance. You can even directly manipulate a graph's state after its execution using the
diff --git a/docs/docs/how-tos/graph-command.ipynb b/docs/docs/how-tos/graph-command.ipynb
index 8df59c9f4..b215c9f4a 100644
--- a/docs/docs/how-tos/graph-command.ipynb
+++ b/docs/docs/how-tos/graph-command.ipynb
@@ -13,7 +13,27 @@
"id": "7c0a8d03-80b4-47fd-9b17-e26aa9b081f3",
"metadata": {},
"source": [
- "Typically, LangGraph separates control flow (edges) and state updates (nodes). However, it is often beneficial to combine the two. For example, you might want to BOTH perform state updates AND decide which node to go next in the SAME node. LangGraph provides a way to combine control flow and node state updates using `GraphCommand`. This guide shows how you can do so."
+ "!!! info \"Prerequisites\"\n",
+ " This guide assumes familiarity with the following:\n",
+ " \n",
+ " - [State](../../concepts/low_level/#state)\n",
+ " - [Nodes](../../concepts/low_level/#nodes)\n",
+ " - [Edges](../../concepts/low_level/#edges)\n",
+ " - [GraphCommand](../../concepts/low_level/#graphcommand)\n",
+ "\n",
+ "It can be useful to combine control flow (edges) and state updates (nodes). For example, you might want to BOTH perform state updates AND decide which node to go to next in the SAME node. LangGraph provides a way to do so by returning a `GraphCommand` object from node functions:\n",
+ "\n",
+ "```python\n",
+ "def my_node(state: State) -> GraphCommand[Literal[\"my_other_node\"]]:\n",
+ " return GraphCommand(\n",
+ " # state update\n",
+ " update={\"foo\": \"bar\"},\n",
+ " # control flow\n",
+ " goto=\"my_other_node\"\n",
+ " )\n",
+ "```\n",
+ "\n",
+ "This guide shows how you can do use `GraphCommand` to add dynamic control flow in your LangGraph app."
]
},
{
@@ -60,24 +80,16 @@
},
{
"cell_type": "markdown",
- "id": "71c8bc81-c1b4-46aa-835f-2c2849156594",
+ "id": "6a08d957-b3d2-4538-bf4a-68ef90a51b98",
"metadata": {},
"source": [
- "## Using edges"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "9a81df3a-6489-44da-8a7e-615009ef9f59",
- "metadata": {},
- "source": [
- "Let's first implement the graph with a traditional LangGraph primitives -- nodes and conditional edges. The conditional edge (`route_from_a`) will inspect the state last updated by node A and decide where to go next based on the value of the state key `foo`."
+ "## Control flow with GraphCommand"
]
},
{
"cell_type": "code",
"execution_count": 2,
- "id": "de32d339-3501-4982-a34f-8d3facc53579",
+ "id": "4539b81b-09e9-4660-ac55-1b1775e13892",
"metadata": {},
"outputs": [],
"source": [
@@ -91,142 +103,12 @@
"class State(TypedDict):\n",
" foo: str\n",
"\n",
- "\n",
- "# Define the nodes\n",
- "def node_a(state: State):\n",
- " print(\"Called A\")\n",
- " return {\"foo\": random.choice([\"a\", \"b\"])}\n",
- "\n",
- "def node_b(state: State):\n",
- " print(\"Called B\")\n",
- " return {\"foo\": state[\"foo\"] + \"b\"}\n",
- "\n",
- "def node_c(state: State):\n",
- " print(\"Called C\")\n",
- " return {\"foo\": state[\"foo\"] + \"c\"}\n",
- "\n",
- "# Define the conditional edges\n",
- "def route_from_a(state: State) -> Literal[\"node_b\", \"node_c\"]:\n",
- " if state[\"foo\"] == \"a\":\n",
- " return \"node_b\"\n",
- " else:\n",
- " return \"node_c\""
- ]
- },
- {
- "cell_type": "markdown",
- "id": "87ef1325-d42f-4a6c-81e6-0058b9628b9e",
- "metadata": {},
- "source": [
- "We can now create the StateGraph with the above nodes and conditional edges."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "id": "b6e3044b-d817-4f7e-9e4f-1b3aff109670",
- "metadata": {},
- "outputs": [],
- "source": [
- "builder = StateGraph(State)\n",
- "builder.add_edge(START, \"node_a\")\n",
- "builder.add_node(node_a)\n",
- "builder.add_node(node_b)\n",
- "builder.add_node(node_c)\n",
- "builder.add_conditional_edges(\"node_a\", route_from_a)\n",
- "\n",
- "graph = builder.compile()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "e60c8a11-ce6f-484c-ba2f-936c3d69b120",
- "metadata": {},
- "source": [
- "If we run the graph multiple times, we'd see it take different paths (A -> B or A -> C) based on the random choice in node A."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "id": "9175add8-0c08-48ee-8d70-249c5d209736",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Called A\n",
- "Called C\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{'foo': 'bc'}"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "graph.invoke({\"foo\": \"\"})"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "id": "254eb3a1-bb47-4401-93fb-51a65b6b8e71",
- "metadata": {},
- "outputs": [
- {
- "data": {
- "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAA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zmlvJXhnZOyfV8X3U8leGdk7J9XxfdVSiaxjcc85LzmlvJXhnZOyfV8X3U8leGdk7J9XxfdVSiaxjcc85LzmlvJXhnZOyfV8X3U8leGdk7J9XxfdVSiaxjcc85LzmlvJXhnZOyfV8X3Vu2uz0Fkpu9rdRU9BT7i/oqaJsbS49Z0AHM+texFirFxK4tVVM/UvMiIi5I/9k=",
- "text/plain": [
- ""
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- }
- ],
- "source": [
- "from IPython.display import display, Image\n",
- "\n",
- "display(Image(graph.get_graph().draw_mermaid_png()))"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "0c52be14-d250-4c64-99e2-ce0a201e4523",
- "metadata": {},
- "source": [
- "Now let's reimplement the same graph using `GraphCommand`!"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6a08d957-b3d2-4538-bf4a-68ef90a51b98",
- "metadata": {},
- "source": [
- "## Using GraphCommand"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "37107209-34d6-4414-a54e-cd3ee38e3651",
- "metadata": {},
- "outputs": [],
- "source": [
"# Define the nodes\n",
"\n",
"def node_a(state: State) -> GraphCommand[Literal[\"node_b\", \"node_c\"]]:\n",
" print(\"Called A\")\n",
" value = random.choice([\"a\", \"b\"])\n",
- " # this is a replacement for the logic in route_from_a\n",
+ " # this is a replacement for a conditional edge function\n",
" if value == \"a\":\n",
" goto = \"node_b\"\n",
" else:\n",
@@ -234,9 +116,9 @@
"\n",
" # note how GraphCommand allows you to BOTH update the graph state AND route to the next node\n",
" return GraphCommand(\n",
- " # this is the state update, same as we returned from node A previously\n",
+ " # this is the state update\n",
" update={\"foo\": value},\n",
- " # this is a replacement for route_from_a conditional edge\n",
+ " # this is a replacement for an edge\n",
" goto=goto\n",
" )\n",
"\n",
@@ -257,17 +139,16 @@
"id": "badc25eb-4876-482e-bb10-d763023cdaad",
"metadata": {},
"source": [
- "We can now create the `StateGraph` with the above nodes. But notice that the graph no longer uses conditional edges! This is because control flow is defined inside `node_a`."
+ "We can now create the `StateGraph` with the above nodes. Notice that the graph doesn't have [conditional edges](../../concepts/low_level#conditional-edges) for routing! This is because control flow is defined with `GraphCommand` inside `node_a`."
]
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 3,
"id": "d6711650-4380-4551-a007-2805f49ab2d8",
"metadata": {},
"outputs": [],
"source": [
- "\n",
"builder = StateGraph(State)\n",
"builder.add_edge(START, \"node_a\")\n",
"builder.add_node(node_a)\n",
@@ -290,7 +171,7 @@
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 4,
"id": "eeb810e5-8822-4c09-8d53-c55cd0f5d42e",
"metadata": {},
"outputs": [
@@ -306,12 +187,21 @@
}
],
"source": [
+ "from IPython.display import display, Image\n",
"display(Image(graph.get_graph().draw_mermaid_png()))"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "58fb6c32-e6fb-4c94-8182-e351ed52a45d",
+ "metadata": {},
+ "source": [
+ "If we run the graph multiple times, we'd see it take different paths (A -> B or A -> C) based on the random choice in node A."
+ ]
+ },
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 5,
"id": "d88a5d9b-ee08-4ed4-9c65-6e868210bfac",
"metadata": {},
"outputs": [
@@ -320,16 +210,16 @@
"output_type": "stream",
"text": [
"Called A\n",
- "Called C\n"
+ "Called B\n"
]
},
{
"data": {
"text/plain": [
- "{'foo': 'bc'}"
+ "{'foo': 'ab'}"
]
},
- "execution_count": 9,
+ "execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py
index c9b4199ab..d3f608356 100644
--- a/libs/langgraph/langgraph/graph/state.py
+++ b/libs/langgraph/langgraph/graph/state.py
@@ -89,14 +89,15 @@ class GraphCommand(Generic[N], Command[N]):
"""One or more commands to update a StateGraph's state and go to, or send messages to nodes.
Args:
- goto: name of the node to navigate to next.
- If not specified, the graph will halt after executing the current superstep.
graph: graph to send the command to. Supported values are:
- None: the current graph (default)
- GraphCommand.PARENT: closest parent graph
update: state update to apply to the graph's state at the current superstep.
send: list of `Send` objects to send to other nodes.
resume: value to resume execution with. Will be used when `interrupt()` is called.
+ goto: name of the node to navigate to next.
+ Can be any node that belongs to the specified `graph` (current or parent).
+ If `goto` not specified, the graph will halt after executing the current superstep.
"""
goto: Union[str, Sequence[str]] = ()