diff --git a/libs/langgraph/langgraph/pregel/algo.py b/libs/langgraph/langgraph/pregel/algo.py index 1410e432f..3c3008ce4 100644 --- a/libs/langgraph/langgraph/pregel/algo.py +++ b/libs/langgraph/langgraph/pregel/algo.py @@ -1,3 +1,4 @@ +import sys from collections import defaultdict, deque from functools import partial from hashlib import sha1 @@ -66,6 +67,7 @@ from langgraph.types import All, LoopProtocol, PregelExecutableTask, PregelTask from langgraph.utils.config import merge_configs, patch_config GetNextVersion = Callable[[Optional[V], BaseChannel], V] +SUPPORTS_EXC_NOTES = sys.version_info >= (3, 11) class WritesProtocol(Protocol): @@ -634,6 +636,12 @@ def prepare_single_task( ) except StopIteration: return + except Exception as exc: + if SUPPORTS_EXC_NOTES: + exc.add_note( + f"Before task with name '{name}' and path '{task_path[:3]}'" + ) + raise # create task id checkpoint_ns = f"{parent_ns}{NS_SEP}{name}" if parent_ns else name diff --git a/libs/langgraph/langgraph/pregel/retry.py b/libs/langgraph/langgraph/pregel/retry.py index 6e52a7c41..2d0f2b6da 100644 --- a/libs/langgraph/langgraph/pregel/retry.py +++ b/libs/langgraph/langgraph/pregel/retry.py @@ -1,6 +1,7 @@ import asyncio import logging import random +import sys import time from dataclasses import replace from functools import partial @@ -18,6 +19,7 @@ from langgraph.types import Command, PregelExecutableTask, RetryPolicy from langgraph.utils.config import patch_configurable logger = logging.getLogger(__name__) +SUPPORTS_EXC_NOTES = sys.version_info >= (3, 11) def run_with_retry( @@ -60,6 +62,8 @@ def run_with_retry( # if interrupted, end raise except Exception as exc: + if SUPPORTS_EXC_NOTES: + exc.add_note(f"During task with name '{task.name}' and id '{task.id}'") if retry_policy is None: raise # increment attempts @@ -152,6 +156,8 @@ async def arun_with_retry( # if interrupted, end raise except Exception as exc: + if SUPPORTS_EXC_NOTES: + exc.add_note(f"During task with name '{task.name}' and id '{task.id}'") if retry_policy is None: raise # increment attempts