From 90958ab97359f8543cf67e1fb64a9ccf34be5ff1 Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Tue, 4 Jun 2024 13:22:28 -0700 Subject: [PATCH] [Docs] Add explanation for error handling when branching (#591) --- examples/branching.ipynb | 56 +++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/examples/branching.ipynb b/examples/branching.ipynb index b4f75214d..da29a87e9 100644 --- a/examples/branching.ipynb +++ b/examples/branching.ipynb @@ -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": [ + "
Exception handling?\n", + "

LangGraph executes nodes within \"supersteps\", meaning that while parallel branches are executed in parallel, the entire superstep is transactional. If any of these branches raises an exception, none of the updates are applied to the state (the entire superstep errors).

\n", + " If you have error-prone (perhaps want to handle flakey API calls), LangGraph provides two ways to address this:
\n", + "

    \n", + "
  1. You can write regular python code within your node to catch and handle exceptions.
  2. \n", + "
  3. You can set a retry_policy 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.
  4. \n", + "

\n", + "Together, these let you perform parallel execution and fully control exception handling.\n", + "
" ] }, { @@ -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,