Handle asyncio.CancelledError in ToolNode#6764
Open
veeceey wants to merge 1 commit intolangchain-ai:mainfrom
Open
Handle asyncio.CancelledError in ToolNode#6764veeceey wants to merge 1 commit intolangchain-ai:mainfrom
veeceey wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
Fixes langchain-ai#6726 When a tool execution raises asyncio.CancelledError and handle_tool_errors=True, the ToolNode now creates an error ToolMessage instead of letting the error propagate. This prevents INVALID_CHAT_HISTORY errors when tools are cancelled. Root cause: asyncio.CancelledError inherits from BaseException, not Exception, so it wasn't caught by the existing exception handler. Changes: - Add explicit asyncio.CancelledError handler in _execute_tool_async before the general Exception handler - When handle_tool_errors=True, convert CancelledError to error ToolMessage with appropriate error content - When handle_tool_errors=False, re-raise CancelledError - Add comprehensive tests for both error handling scenarios
Author
PR Review SummaryStatus: ✅ READY TO MERGEWell-handled edge case! This PR correctly catches which inherits from rather than , allowing proper error handling in ToolNode. Key Strengths:
Technical Details:Problem: inherits from , not , so it bypassed the existing exception handler and propagated unhandled Solution:
Test Coverage:
This is a clean fix for a subtle Python exception hierarchy issue. Proper error handling for async tool cancellation. Ready for merge! |
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #6726 - ToolNode now properly handles asyncio.CancelledError when handle_tool_errors=True.
Problem
When a tool execution is cancelled via
asyncio.CancelledError, theToolNodedoes not create an errorToolMessageeven whenhandle_tool_errors=True. This leaves the message history in an invalid state where anAIMessagehastool_callswithout correspondingToolMessages, causing INVALID_CHAT_HISTORY errors on subsequent LLM calls.Root cause:
asyncio.CancelledErrorinherits fromBaseException, notException, so it bypasses the existing exception handler which only catchesExceptionand its subclasses.Solution
Added an explicit handler for
asyncio.CancelledErrorin_execute_tool_async()that:CancelledErrorbefore the generalExceptionhandlerhandle_tool_errors=True, converts it to an errorToolMessagewith "Tool execution was cancelled" messagehandle_tool_errors=False, re-raises theCancelledError(preserving existing behavior)Test Plan
Added two comprehensive tests:
test_tool_node_handles_cancelled_error: Verifies that when a tool raises CancelledError and handle_tool_errors=True, it returns an error ToolMessage instead of propagating the errortest_tool_node_raises_cancelled_error_when_not_handled: Verifies that when handle_tool_errors=False, CancelledError is still raisedChanges
libs/prebuilt/langgraph/prebuilt/tool_node.py: Added CancelledError handlerlibs/prebuilt/tests/test_tool_node.py: Added regression tests