Skip to content

Commit 2da1531

Browse files
update
1 parent 21cacb2 commit 2da1531

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

examples/exceptions/input_guardrail_tripwire_triggered.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
GuardrailFunctionOutput,
1010
InputGuardrailTripwireTriggered,
1111
Runner,
12+
RunContextWrapper,
1213
input_guardrail,
1314
)
1415

@@ -18,6 +19,7 @@
1819
The 'CustomerSupportAgent' processes user queries provided as direct string inputs in an interactive loop. A guardrail, implemented via 'GuardrailAgent' and a Pydantic model (`MathHomeworkOutput`), checks if the input is a math homework question. If detected, the guardrail raises `InputGuardrailTripwireTriggered`, triggering a refusal message ("Sorry, I can't help with math homework."). Otherwise, the agent responds to the query. The loop continues to prompt for new inputs, handling each independently.
1920
"""
2021

22+
2123
class MathHomeworkOutput(BaseModel):
2224
is_math_homework: bool
2325

@@ -30,7 +32,11 @@ class MathHomeworkOutput(BaseModel):
3032

3133

3234
@input_guardrail
33-
async def math_guardrail(context, agent: Agent, input: str) -> GuardrailFunctionOutput:
35+
def my_input_guardrail(
36+
context: RunContextWrapper[Any],
37+
agent: Agent[Any],
38+
inputs: str | list[Any],
39+
) -> GuardrailFunctionOutput:
3440
result = await Runner.run(guardrail_agent, input)
3541
output = result.final_output_as(MathHomeworkOutput)
3642
return GuardrailFunctionOutput(

examples/exceptions/user_error.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
from __future__ import annotations
22

33
import asyncio
4-
54
from agents import Agent, Runner, function_tool
65
from agents.exceptions import UserError
76

7+
88
"""
9-
This example demonstrates an OpenAI Agents SDK agent that triggers a UserError due to incorrect SDK usage.
9+
This example demonstrates raising a `UserError` at runtime by violating a tool-specific logic rule
10+
(e.g., returning the wrong type despite declaring a valid return type).
1011
11-
The 'Assistant' agent is configured with an invalid `tool_use_behavior` (empty string) and an invalid tool (`invalid_tool`) that declares a `None` return type but returns a string. Either issue raises a `UserError` when the agent is executed, indicating improper SDK configuration by the user. The interactive loop processes user queries as direct string inputs, catching and displaying the `UserError` message.
12+
This passes `mypy` but fails at runtime due to logical misuse of the SDK.
1213
"""
1314

1415

1516
@function_tool
1617
def invalid_tool() -> None:
17-
return "I return a string" # Type mismatch triggers UserError
18+
# This tool *claims* to return None, but it returns a string instead.
19+
# This misuse is caught by the SDK during runtime and raises UserError.
20+
return "This return violates the declared return type."
1821

1922

2023
async def main():
2124
agent = Agent(
2225
name="Assistant",
23-
instructions="Use the invalid_tool to process queries.",
26+
instructions="Use the tool to demonstrate misuse.",
2427
tools=[invalid_tool],
25-
tool_use_behavior="invalid_tool",
28+
tool_use_behavior="run_llm_again", # ✅ valid value — no mypy error
2629
)
27-
user_input = "Do Something."
30+
31+
user_input = "Please do something invalid"
2832
try:
2933
result = await Runner.run(agent, user_input)
3034
print(result.final_output)
3135
except UserError as e:
32-
print(f"UserError: {e}")
36+
print(f"UserError caught as expected: {e}")
3337

3438

3539
if __name__ == "__main__":

0 commit comments

Comments
 (0)