You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(core): Add AgentCancel exception for control-flow in callbacks (#894)
* feat(core): Add AgentCancel exception for control-flow in callbacks
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
* test(unit): Add tests for AgentCancel exception
- 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
* test(integration): Add AgentCancel integration test
Verify AgentCancel subclasses propagate without being wrapped in
AgentRunError across all agent frameworks.
* docs: Document AgentCancel for stopping agent execution
- Add "Stopping Execution" section to callbacks documentation
- Document AgentCancel vs regular exception patterns
- Add examples for both exception types
- Add AgentCancel to API reference
* fix(tools): Update type: ignore for isinstance tuple type narrowing
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.
* fix(openai): Remove type: ignore now that any-llm-sdk 1.7.0 supports xhigh
Copy file name to clipboardExpand all lines: docs/agents/callbacks.md
+92-11Lines changed: 92 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,20 +48,90 @@ class CountSearchWeb(Callback):
48
48
return context
49
49
```
50
50
51
-
Callbacks can raise exceptions to stop agent execution. This is useful for implementing safety guardrails or validation logic:
51
+
## Stopping Execution
52
+
53
+
Callbacks can raise exceptions to stop agent execution. This is useful for implementing safety guardrails or validation logic.
54
+
55
+
!!! warning "Exceptions act as a circuit breaker"
56
+
57
+
Raising any exception from a callback immediately halts the agent loop. Use this intentionally to enforce limits or abort on invalid states.
58
+
59
+
### Using `AgentCancel` (Recommended)
60
+
61
+
For intentional cancellation (rate limits, guardrails, validation), subclass [`AgentCancel`][any_agent.AgentCancel]. These exceptions propagate directly to your code, allowing you to catch them by their specific type:
52
62
53
63
```python
64
+
from any_agent import AgentCancel, AgentConfig, AnyAgent
trace = agent.run("Find information about Python")
90
+
except SearchLimitReached as e:
91
+
print(f"Search limit reached: {e}")
92
+
print(f"Trace: {e.trace}") # Access spans collected before cancellation
93
+
```
94
+
95
+
### Using Regular Exceptions
96
+
97
+
Regular exceptions (like `RuntimeError`) are automatically wrapped in [`AgentRunError`][any_agent.AgentRunError] by the framework, which provides access to the execution trace but requires you to inspect the wrapped exception:
98
+
99
+
```python
100
+
from any_agent import AgentConfig, AgentRunError, AnyAgent
0 commit comments