fix(prebuilt): catch asyncio.CancelledError in ToolNode to prevent INVALID_CHAT_HISTORY#6945
Open
Subham Singhania (Subhamsinghania18) wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
…on paths asyncio.CancelledError was changed to inherit from BaseException (not Exception) in Python 3.8 (bpo-32528). ToolNode's async error handlers only caught Exception, so a task cancellation propagated unhandled, leaving the message history in an invalid state: AIMessage(tool_calls=[...]) <- no matching ToolMessage --> next LLM call raises INVALID_CHAT_HISTORY Two sites are fixed: 1. _execute_tool_async — the primary execution try/except block. A CancelledError handler is inserted between GraphBubbleUp (must always propagate) and the general Exception handler. When handle_tool_errors is truthy an error ToolMessage is returned to keep history valid; when it is falsy the error is re-raised unchanged. 2. _arun_one wrapper block — the outer try/except that wraps calls through _awrap_tool_call / _wrap_tool_call. Same logic applies. asyncio is already imported at the top of the module; no new imports are required. Closes: langchain-ai#6726
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.
asyncio.CancelledErrorwas changed to inherit fromBaseException(notException) in Python 3.8 (bpo-32528).ToolNode's async error handlersonly caught
Exception, so any task cancellation propagated unhandled,leaving the message history in an invalid state — an
AIMessagewithtool_callsbut no correspondingToolMessage. The next LLM call thenraised
INVALID_CHAT_HISTORY.Two sites are fixed:
_execute_tool_async— primary execution try/except block. ACancelledErrorhandler is inserted betweenGraphBubbleUp(must alwayspropagate) and the general
Exceptionhandler._arun_onewrapper block — the outer try/except wrapping_awrap_tool_call/_wrap_tool_call.When
handle_tool_errorsis truthy a proper errorToolMessageisreturned to keep chat history valid. When falsy the error is re-raised
unchanged — zero behaviour change for the default case.
asynciois already imported at the top of the module.Issue: #6726
Dependencies: None