Skip to content

Commit fbddcb4

Browse files
Update agents_exception.py
1 parent 9b1f53a commit fbddcb4

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

examples/exceptions/agents_exception.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
2-
from agents import Agent, RunContextWrapper, Runner, function_tool
3-
from agents.exceptions import AgentsException
2+
43
import asyncio
4+
from agents import Agent, Runner, function_tool
5+
from agents.exceptions import AgentsException
56

67

78
"""
@@ -12,47 +13,40 @@
1213
- Adding two numbers using the `sum_numbers` tool.
1314
1415
The agent is instructed to use only one tool per execution cycle and can switch to another tool in subsequent cycles.
15-
The example sets a `max_turns=1` limit to intentionally restrict the agent to a single turn, which may trigger a `MaxTurnsExceeded` error if the agent attempts multiple tool calls.
16+
The example sets a `max_turns=1` limit to intentionally restrict the agent to a single turn, which may trigger a `MaxTurnsExceeded` error.
1617
17-
Error handling is implemented with `AgentsException`, which is the base class for all SDK-related exceptions, including:
18-
- `MaxTurnsExceeded`: Raised when the run exceeds the `max_turns` specified in the run methods.
19-
- `ModelBehaviorError`: Raised when the model produces invalid outputs, e.g., malformed JSON or using non-existent tools.
20-
- `UserError`: Raised when the SDK user makes an error in code implementation.
21-
- `InputGuardrailTripwireTriggered`: Raised when an input guardrail is violated (e.g., invalid or off-topic input).
22-
- `OutputGuardrailTripwireTriggered`: Raised when an output guardrail is violated (e.g., invalid tool output).
23-
24-
Although this example does not include explicit guardrails, the structure supports adding input/output guardrails to validate user inputs or tool outputs. The `AgentsException` catch block ensures all SDK-related errors are handled gracefully.
18+
All exceptions are caught via `AgentsException`, the base class for SDK errors.
2519
"""
2620

27-
21+
# Define tools
2822

2923
@function_tool
30-
def get_weather(city: str) -> str:
24+
async def get_weather(city: str) -> str:
3125
"""Returns weather info for the specified city."""
32-
return f"The weather in {city} is sunny"
26+
return f"The weather in {city} is sunny."
3327

3428

3529
@function_tool
36-
def sum_numbers(a: int, b: int) -> int:
30+
async def sum_numbers(a: int, b: int) -> str:
3731
"""Adds two numbers."""
38-
return a + b
32+
result = a + b
33+
return f"The sum of {a} and {b} is {result}."
3934

4035

4136
agent = Agent(
4237
name="Triage Agent",
43-
instructions="Get weather or sum numbers. You can use one tool at a time, switching to another tool in subsequent turns.",
44-
tools=[sum_numbers, get_weather],
38+
instructions="Get weather or sum numbers. Use only one tool per turn.",
39+
tools=[get_weather, sum_numbers],
4540
)
4641

4742

4843
async def main():
4944
try:
5045
user_input = input("Enter a message: ")
51-
5246
result = await Runner.run(agent, user_input, max_turns=1)
53-
print(result.final_output)
47+
print("✅ Final Output:", result.final_output)
5448
except AgentsException as e:
55-
print(f"Caught AgentsException: {e}")
49+
print(f"Caught {e.__class__.__name__}: {e}")
5650

5751

5852
if __name__ == "__main__":

0 commit comments

Comments
 (0)