From 257e44ccb40b6efe943f007d93221e22aa75cb53 Mon Sep 17 00:00:00 2001 From: vbarda Date: Wed, 4 Dec 2024 18:46:29 -0500 Subject: [PATCH] update --- docs/docs/concepts/low_level.md | 32 ++++++++++++------------- docs/docs/how-tos/graph-command.ipynb | 34 +++++++++++++-------------- libs/langgraph/langgraph/types.py | 6 ++++- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/docs/docs/concepts/low_level.md b/docs/docs/concepts/low_level.md index 75ff6d4a3..c775eae81 100644 --- a/docs/docs/concepts/low_level.md +++ b/docs/docs/concepts/low_level.md @@ -322,13 +322,13 @@ def continue_to_jokes(state: OverallState): graph.add_conditional_edges("node_a", continue_to_jokes) ``` -## `GraphCommand` +## `Command` -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: +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 [`Command`][langgraph.graph.state.GraphCommand] object from node functions: ```python -def my_node(state: State) -> GraphCommand[Literal["my_other_node"]]: - return GraphCommand( +def my_node(state: State) -> Command[Literal["my_other_node"]]: + return Command( # state update update={"foo": "bar"}, # control flow @@ -336,25 +336,25 @@ def my_node(state: State) -> GraphCommand[Literal["my_other_node"]]: ) ``` -`GraphCommand` has the following properties: +`Command` has the following properties: | 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. | +| `graph` | Graph to send the command to. Supported values:
- `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`](#send) objects to send to other nodes | | `resume` | Value to resume execution with. Will be used when `interrupt()` is called | +| `goto` | Can be one of the following:
- name of the node to navigate to next (any node that belongs to the specified `graph`)
- list of node names to navigate to next
- `Send` object
- sequence of `Send` objects
If `goto` is not specified and there are no other tasks left in the graph, the graph will halt after executing the current superstep. | ```python -from langgraph.graph import GraphCommand, StateGraph, START +from langgraph.graph import StateGraph, START +from langgraph.types import Command from typing_extensions import Literal, TypedDict class State(TypedDict): foo: str -def my_node(state: State) -> GraphCommand[Literal["my_other_node"]]: - return GraphCommand(update={"foo": "bar"}, goto="my_other_node") +def my_node(state: State) -> Command[Literal["my_other_node"]]: + return Command(update={"foo": "bar"}, goto="my_other_node") def my_other_node(state: State): return {"foo": state["foo"] + "baz"} @@ -367,17 +367,17 @@ builder.add_node("my_other_node", my_other_node) graph = builder.compile() ``` -With `GraphCommand` you can also achieve dynamic control flow behavior (identical to [conditional edges](#conditional-edges)): +With `Command` you can also achieve dynamic control flow behavior (identical to [conditional edges](#conditional-edges)): ```python -def my_node(state: State) -> GraphCommand[Literal["my_other_node", "__end__"]]: +def my_node(state: State) -> Command[Literal["my_other_node", "__end__"]]: if state["foo"] == "bar": - return GraphCommand(update={"foo": "baz"}, goto="my_other_node") + return Command(update={"foo": "baz"}, goto="my_other_node") else: - return GraphCommand(goto="__end__") + return Command(goto="__end__") ``` -Check out this [how-to guide](../how-tos/graph-command.ipynb) for an end-to-end example of how to use `GraphCommand`. +Check out this [how-to guide](../how-tos/graph-command.ipynb) for an end-to-end example of how to use `Command`. ## Persistence diff --git a/docs/docs/how-tos/graph-command.ipynb b/docs/docs/how-tos/graph-command.ipynb index b768e75a8..ad33e3a62 100644 --- a/docs/docs/how-tos/graph-command.ipynb +++ b/docs/docs/how-tos/graph-command.ipynb @@ -5,7 +5,7 @@ "id": "d33ecddc-6818-41a3-9d0d-b1b1cbcd286d", "metadata": {}, "source": [ - "# How to combine control flow and state updates with GraphCommand" + "# How to combine control flow and state updates with Command" ] }, { @@ -19,12 +19,12 @@ " - [State](../../concepts/low_level/#state)\n", " - [Nodes](../../concepts/low_level/#nodes)\n", " - [Edges](../../concepts/low_level/#edges)\n", - " - [GraphCommand](../../concepts/low_level/#graphcommand)\n", + " - [Command](../../concepts/low_level/#command)\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", + "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 `Command` object from node functions:\n", "\n", "```python\n", - "def my_node(state: State) -> GraphCommand[Literal[\"my_other_node\"]]:\n", + "def my_node(state: State) -> Command[Literal[\"my_other_node\"]]:\n", " return GraphCommand(\n", " # state update\n", " update={\"foo\": \"bar\"},\n", @@ -33,7 +33,7 @@ " )\n", "```\n", "\n", - "This guide shows how you can do use `GraphCommand` to add dynamic control flow in your LangGraph app." + "This guide shows how you can do use `Command` to add dynamic control flow in your LangGraph app." ] }, { @@ -83,7 +83,7 @@ "id": "6a08d957-b3d2-4538-bf4a-68ef90a51b98", "metadata": {}, "source": [ - "## Control flow with GraphCommand" + "## Control flow with Command" ] }, { @@ -96,7 +96,8 @@ "import random\n", "from typing_extensions import TypedDict, Literal\n", "\n", - "from langgraph.graph import GraphCommand, StateGraph, START\n", + "from langgraph.graph import StateGraph, START\n", + "from langgraph.types import Command\n", "\n", "\n", "# Define graph state\n", @@ -107,7 +108,7 @@ "# Define the nodes\n", "\n", "\n", - "def node_a(state: State) -> GraphCommand[Literal[\"node_b\", \"node_c\"]]:\n", + "def node_a(state: State) -> Command[Literal[\"node_b\", \"node_c\"]]:\n", " print(\"Called A\")\n", " value = random.choice([\"a\", \"b\"])\n", " # this is a replacement for a conditional edge function\n", @@ -116,8 +117,8 @@ " else:\n", " goto = \"node_c\"\n", "\n", - " # note how GraphCommand allows you to BOTH update the graph state AND route to the next node\n", - " return GraphCommand(\n", + " # note how Command allows you to BOTH update the graph state AND route to the next node\n", + " return Command(\n", " # this is the state update\n", " update={\"foo\": value},\n", " # this is a replacement for an edge\n", @@ -130,7 +131,6 @@ "\n", "def node_b(state: State):\n", " print(\"Called B\")\n", - " # graph command can also be used\n", " return {\"foo\": state[\"foo\"] + \"b\"}\n", "\n", "\n", @@ -171,7 +171,7 @@ "source": [ "!!! important\n", "\n", - " You might have noticed that we used `GraphCommand` as a return type annotation, e.g. `GraphCommand[Literal[\"node_b\", \"node_c\"]]`. This is necessary for the graph compilation and rendering, and tells LangGraph that `node_a` can navigate to `node_b` and `node_c`." + " You might have noticed that we used `Command` as a return type annotation, e.g. `Command[Literal[\"node_b\", \"node_c\"]]`. This is necessary for the graph compilation and rendering, and tells LangGraph that `node_a` can navigate to `node_b` and `node_c`." ] }, { @@ -216,13 +216,13 @@ "output_type": "stream", "text": [ "Called A\n", - "Called B\n" + "Called C\n" ] }, { "data": { "text/plain": [ - "{'foo': 'ab'}" + "{'foo': 'bc'}" ] }, "execution_count": 5, @@ -237,9 +237,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "langgraph", "language": "python", - "name": "python3" + "name": "langgraph" }, "language_info": { "codemirror_mode": { @@ -251,7 +251,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.11.9" } }, "nbformat": 4, diff --git a/libs/langgraph/langgraph/types.py b/libs/langgraph/langgraph/types.py index 98f9e350d..28e5a5940 100644 --- a/libs/langgraph/langgraph/types.py +++ b/libs/langgraph/langgraph/types.py @@ -252,8 +252,12 @@ class Command(Generic[N]): - 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: can be one of the following: + - name of the node to navigate to next (any node that belongs to the specified `graph`) + - list of node names to navigate to next + - `Send` object + - sequence of `Send` objects """ graph: Optional[str] = None