[Docs] Add explanation for error handling when branching (#591)

This commit is contained in:
William FH
2024-06-04 13:22:28 -07:00
committed by GitHub
parent 5fd52a486e
commit 90958ab973
+33 -23
View File
@@ -50,6 +50,7 @@
" # The operator.add reducer fn makes this append-only\n",
" aggregate: Annotated[list, operator.add]\n",
"\n",
"\n",
"from typing import Any\n",
"\n",
"\n",
@@ -61,6 +62,7 @@
" print(f\"Adding {self._value} to {state['aggregate']}\")\n",
" return {\"aggregate\": [self._value]}\n",
"\n",
"\n",
"builder = StateGraph(State)\n",
"builder.add_node(\"a\", ReturnNodeValue(\"I'm A\"))\n",
"builder.set_entry_point(\"a\")\n",
@@ -126,7 +128,23 @@
}
],
"source": [
"graph.invoke({\"aggregate\": []})"
"graph.invoke({\"aggregate\": []}, {\"configurable\": {\"thread_id\": \"foo\"}})"
]
},
{
"cell_type": "markdown",
"id": "c392b3d2",
"metadata": {},
"source": [
"<details class=\"note\"> <summary>Exception handling?</summary>\n",
" <p>LangGraph executes nodes within <a href=\"https://langchain-ai.github.io/langgraph/concepts/#core-design\">\"supersteps\"</a>, meaning that while parallel branches are executed in parallel, the entire superstep is <b>transactional</b>. If any of these branches raises an exception, <b>none</b> of the updates are applied to the state (the entire superstep errors).<br><br>\n",
" If you have error-prone (perhaps want to handle flakey API calls), LangGraph provides two ways to address this:<br>\n",
" <ol>\n",
" <li>You can write regular python code within your node to catch and handle exceptions.</li>\n",
" <li>You can set a <b><a href=\"https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.graph.CompiledGraph.retry_policy\">retry_policy</a></b> to direct the graph to retry nodes that raise certain types of exceptions. Only failing branches are retried, so you needn't worry about performing redundant work.</li>\n",
"</ol></p>\n",
"Together, these let you perform parallel execution and fully control exception handling.\n",
"</details>"
]
},
{
@@ -241,7 +259,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "95f5e026",
"metadata": {},
"outputs": [],
@@ -290,7 +308,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "1d0e6c56",
"metadata": {},
"outputs": [
@@ -313,7 +331,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "7134f652",
"metadata": {},
"outputs": [
@@ -322,8 +340,8 @@
"output_type": "stream",
"text": [
"Adding I'm A to []\n",
"Adding I'm C to [\"I'm A\"]\n",
"Adding I'm B to [\"I'm A\"]\n",
"Adding I'm C to [\"I'm A\"]\n",
"Adding I'm E to [\"I'm A\", \"I'm B\", \"I'm C\"]\n"
]
},
@@ -333,7 +351,7 @@
"{'aggregate': [\"I'm A\", \"I'm B\", \"I'm C\", \"I'm E\"], 'which': 'bc'}"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -344,7 +362,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "b130e694",
"metadata": {},
"outputs": [
@@ -364,7 +382,7 @@
"{'aggregate': [\"I'm A\", \"I'm C\", \"I'm D\", \"I'm E\"], 'which': 'cd'}"
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -389,7 +407,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "836bc12d",
"metadata": {},
"outputs": [],
@@ -481,7 +499,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"id": "932c497e",
"metadata": {},
"outputs": [
@@ -504,7 +522,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"id": "933b3afd",
"metadata": {},
"outputs": [
@@ -525,7 +543,7 @@
" 'which': 'bc'}"
]
},
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -536,7 +554,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"id": "e30531bf",
"metadata": {},
"outputs": [
@@ -557,7 +575,7 @@
" 'which': 'cd'}"
]
},
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -565,14 +583,6 @@
"source": [
"graph.invoke({\"aggregate\": [], \"which\": \"cd\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c8a7b99",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -591,7 +601,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
"version": "3.12.2"
}
},
"nbformat": 4,