Skip to content

fix(core): Fix AgentCancel propagation for LangChain and smolagents#897

Merged
daavoo merged 2 commits intomainfrom
peteski22/fix-agent-cancel
Jan 19, 2026
Merged

fix(core): Fix AgentCancel propagation for LangChain and smolagents#897
daavoo merged 2 commits intomainfrom
peteski22/fix-agent-cancel

Conversation

@peteski22
Copy link
Contributor

@peteski22 peteski22 commented Jan 16, 2026

Summary

  • Add _unwrap_agent_cancel() helper to traverse exception chains and find wrapped AgentCancel instances. This fixes smolagents (which uses raise ... from e) and acts as a defensive catch-all for any future frameworks that follow the same pattern.
  • Set raise_error=True on 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] and test_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

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.
@peteski22 peteski22 requested a review from daavoo January 16, 2026 12:03
@peteski22 peteski22 added callbacks Related to `any_agent/callbacks` frameworks Related to existing or new agent frameworks labels Jan 16, 2026
@codecov
Copy link

codecov bot commented Jan 16, 2026

Codecov Report

❌ Patch coverage is 96.51163% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/any_agent/frameworks/any_agent.py 70.00% 2 Missing and 1 partial ⚠️
Files with missing lines Coverage Δ
src/any_agent/callbacks/wrappers/langchain.py 89.23% <100.00%> (+0.16%) ⬆️
.../callbacks/wrappers/test_get_wrapper_and_unwrap.py 100.00% <100.00%> (ø)
tests/unit/frameworks/test_agent_cancel.py 100.00% <100.00%> (ø)
src/any_agent/frameworks/any_agent.py 84.94% <70.00%> (-7.10%) ⬇️

... and 42 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@daavoo daavoo merged commit 480fc0c into main Jan 19, 2026
10 checks passed
@daavoo daavoo deleted the peteski22/fix-agent-cancel branch January 19, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

callbacks Related to `any_agent/callbacks` frameworks Related to existing or new agent frameworks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants