feat(core): Add AgentCancel exception for control-flow in callbacks#894
Merged
feat(core): Add AgentCancel exception for control-flow in callbacks#894
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 44 files with indirect coverage changes 🚀 New features to boost your workflow:
|
Callbacks can now raise exceptions inheriting from AgentCancel to stop agent execution. These exceptions propagate directly to the caller without being wrapped in AgentRunError, allowing users to catch them by their specific type. - Add AgentCancel ABC with trace property for accessing spans - Modify run_async to preserve AgentCancel exceptions - Update AgentRunError docstring for clarity
ecdb446 to
e4ee3fb
Compare
- Test AgentCancel ABC cannot be instantiated directly - Test subclasses can be instantiated and caught - Test trace property and message preservation - Test run_async preserves AgentCancel without wrapping - Test regular exceptions are wrapped in AgentRunError
Verify AgentCancel subclasses propagate without being wrapped in AgentRunError across all agent frameworks.
- Add "Stopping Execution" section to callbacks documentation - Document AgentCancel vs regular exception patterns - Add examples for both exception types - Add AgentCancel to API reference
The isinstance check using a runtime-constructed tuple doesn't allow mypy to narrow the type, causing a no-any-return error instead of the original return-value error.
e4ee3fb to
6def831
Compare
daavoo
approved these changes
Jan 16, 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
AgentCancelabstract base class for control-flow exceptions in callbacksrun_asyncto preserveAgentCancelsubclasses without wrapping inAgentRunErrorRationale
Currently,
AnyAgent.run_asyncunconditionally wraps all exceptions inAgentRunError, breaking user control-flow semantics. When users raise custom exceptions in callbacks to stop agent execution, they cannot catch them by their specific type:This is particularly problematic for implementing safety guardrails, rate limits, and validation logic where stopping execution is expected behaviour rather than an error.
Solution
Introduce
AgentCancel, an abstract base class for control-flow exceptions. Subclasses propagate directly to the caller with the execution trace attached:This follows Python conventions like
asyncio.CancelledErrorandStopIterationwhere control-flow exceptions are distinct from error exceptions.NOTE: This is a non-breaking, opt-in change. Existing code continues to work unchanged, only users who explicitly subclass
AgentCancelwill see the new behaviour.Additional Fixes
This PR also includes two unrelated type fixes discovered during CI:
type: ignorecomment inwrappers.py- the isinstance check using a runtime-constructed tuple doesn't allow mypy to narrow the type, requiring[no-any-return]instead of[return-value]type: ignoreforreasoning_effortnow thatany-llm-sdk1.7.0 supportsxhigh