fix(core): Fix AgentCancel propagation for LangChain and smolagents#897
Merged
fix(core): Fix AgentCancel propagation for LangChain and smolagents#897
Conversation
Some frameworks (e.g., smolagents) catch callback exceptions and re-raise them wrapped in their own error types using `raise ... from e`. This caused AgentCancel to be wrapped in AgentRunError instead of propagating directly. Add _unwrap_agent_cancel() helper that traverses Python's exception chain (__cause__ and __context__) to find any wrapped AgentCancel, allowing it to propagate correctly to the caller.
LangChain's BaseCallbackHandler defaults to raise_error=False, which catches exceptions in callbacks, logs a warning, and continues execution. This silently broke the circuit-breaker pattern for callback exceptions. Set raise_error=True on our callback handler to ensure exceptions (including AgentCancel subclasses) propagate correctly to run_async.
Codecov Report❌ Patch coverage is
... and 42 files with indirect coverage changes 🚀 New features to boost your workflow:
|
daavoo
approved these changes
Jan 19, 2026
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
_unwrap_agent_cancel()helper to traverse exception chains and find wrappedAgentCancelinstances. This fixes smolagents (which usesraise ... from e) and acts as a defensive catch-all for any future frameworks that follow the same pattern.raise_error=Trueon LangChain callback handler to propagate exceptions instead of swallowing them. LangChain defaults to logging warnings and continuing execution, which silently broke the circuit-breaker pattern for callback exceptions.Fixes integration test failures on main for
test_agent_cancel_not_wrapped[LANGCHAIN]andtest_agent_cancel_not_wrapped[SMOLAGENTS].Tests can be checked using:
uv run pytest tests/integration/frameworks/test_error_handling.py::test_agent_cancel_not_wrapped -k "LANGCHAIN or SMOLAGENTS" -v