{ "cells": [ { "cell_type": "markdown", "id": "d33ecddc-6818-41a3-9d0d-b1b1cbcd286d", "metadata": {}, "source": [ "# How to combine control flow and state updates with Command" ] }, { "cell_type": "markdown", "id": "7c0a8d03-80b4-47fd-9b17-e26aa9b081f3", "metadata": {}, "source": [ "!!! 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", " - [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 `Command` object from node functions:\n", "\n", "```python\n", "def my_node(state: State) -> Command[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 `Command` to add dynamic control flow in your LangGraph app." ] }, { "cell_type": "markdown", "id": "d1c3f866-8c20-40c7-a201-35f6c9f4b680", "metadata": {}, "source": [ "## Setup\n", "\n", "First, let's install the required packages" ] }, { "cell_type": "code", "execution_count": 1, "id": "6999c7fe-31bb-4c19-946a-85c2edc57da7", "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install -U langgraph" ] }, { "cell_type": "markdown", "id": "0f131c92-4744-431c-a89c-7c382a15b79f", "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", "