diff --git a/docs/_scripts/copy_notebooks.py b/docs/_scripts/copy_notebooks.py index 5954e7ef5..703fd5dcd 100644 --- a/docs/_scripts/copy_notebooks.py +++ b/docs/_scripts/copy_notebooks.py @@ -56,6 +56,7 @@ _MANUAL = { "human_in_the_loop/time-travel.ipynb", "human_in_the_loop/edit-graph-state.ipynb", "human_in_the_loop/wait-user-input.ipynb", + "node-retries.ipynb", ], "tutorials": [ "introduction.ipynb", diff --git a/docs/docs/how-tos/index.md b/docs/docs/how-tos/index.md index d98b9e255..b1783e492 100644 --- a/docs/docs/how-tos/index.md +++ b/docs/docs/how-tos/index.md @@ -68,6 +68,7 @@ These guides show how to use different streaming modes. - [How to add runtime configuration to your graph](configuration.ipynb) - [How to use a Pydantic model as your state](state-model.ipynb) - [How to use a context object in state](state-context-key.ipynb) +- [How to add node retries](node-retries.ipynb) ## Prebuilt ReAct Agent diff --git a/docs/docs/reference/graphs.md b/docs/docs/reference/graphs.md index 6a2dfd613..497e16d14 100644 --- a/docs/docs/reference/graphs.md +++ b/docs/docs/reference/graphs.md @@ -65,4 +65,8 @@ builder.add_conditional_edges("my_node", my_condition) ## Send -::: langgraph.constants.Send \ No newline at end of file +::: langgraph.constants.Send + +## RetryPolicy + +::: langgraph.pregel.types.RetryPolicy \ No newline at end of file diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index daf74c894..e7e1f71e5 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -163,6 +163,7 @@ nav: - Add runtime configuration: how-tos/configuration.ipynb - Use Pydantic model as state: how-tos/state-model.ipynb - Use a context object in state: how-tos/state-context-key.ipynb + - Add node retries: how-tos/node-retries.ipynb - Prebuilt ReAct Agent: - Create a ReAct agent: how-tos/create-react-agent.ipynb - Add memory to a ReAct agent: how-tos/create-react-agent-memory.ipynb diff --git a/examples/node-retries.ipynb b/examples/node-retries.ipynb new file mode 100644 index 000000000..c50230ded --- /dev/null +++ b/examples/node-retries.ipynb @@ -0,0 +1,112 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# How to add node retry policies\n", + "\n", + "There are many use cases where you may wish for your node to have a custom retry policy, for example if you are calling an API, querying a database, or calling an LLM, etc. \n", + "\n", + "In order to configure the retry policty, you have to pass the `retry` parameter to the `add_node` function. The `retry` parameter takes in a `RetryPolicy` named tuple object. Below we instantiate a `RetryPolicy` object with the default parameters:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "RetryPolicy(initial_interval=0.5, backoff_factor=2.0, max_interval=128.0, max_attempts=3, jitter=True, retry_on=)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langgraph.pregel import RetryPolicy\n", + "\n", + "RetryPolicy()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you want more information on what each of the parameters does, be sure to read the [reference](https://langchain-ai.github.io/langgraph/reference/graphs/#retrypolicy).\n", + "\n", + "## Passing a retry policy to a node\n", + "\n", + "Lastly, we can pass `RetryPolicy` objects when we call the `add_node` function. In the example below we pass two different retry policies to each of our nodes:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "import operator\n", + "import sqlite3\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", + "from langchain_anthropic import ChatAnthropic\n", + "from langchain_core.messages import BaseMessage\n", + "\n", + "from langgraph.graph import END, StateGraph, START\n", + "from langchain_community.utilities import SQLDatabase\n", + "from langchain_core.messages import AIMessage\n", + "\n", + "db = SQLDatabase.from_uri(\"sqlite:///:memory:\")\n", + "\n", + "model = ChatAnthropic(model_name=\"claude-2.1\")\n", + "\n", + "class AgentState(TypedDict):\n", + " messages: Annotated[Sequence[BaseMessage], operator.add]\n", + "\n", + "def query_database(state):\n", + " query_result = db.run(\"SELECT * FROM Artist LIMIT 10;\")\n", + " return {\"messages\": [AIMessage(content=query_result)]}\n", + "\n", + "def call_model(state):\n", + " response = model.invoke(state[\"messages\"])\n", + " return {\"messages\": [response]}\n", + "\n", + "# Define a new graph\n", + "workflow = StateGraph(AgentState)\n", + "workflow.add_node(\"query_database\",query_database, retry=RetryPolicy(retry_on=sqlite3.OperationalError))\n", + "workflow.add_node(\"model\", call_model, retry=RetryPolicy(max_attempts=5))\n", + "workflow.add_edge(START, \"model\")\n", + "workflow.add_edge(\"model\",\"query_database\")\n", + "workflow.add_edge(\"query_database\", END)\n", + "\n", + "app = workflow.compile()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "env", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py index abde9d0e9..96ad8e513 100644 --- a/libs/langgraph/langgraph/graph/state.py +++ b/libs/langgraph/langgraph/graph/state.py @@ -254,6 +254,7 @@ class StateGraph(Graph): action (Optional[RunnableLike]): The action associated with the node. (default: None) metadata (Optional[dict[str, Any]]): The metadata associated with the node. (default: None) input (Optional[Type[Any]]): The input schema for the node. (default: the graph's input schema) + retry (Optional[RetryPolicy]): The policy for retrying the node. (default: None) Raises: ValueError: If the key is already being used as a state key.