Skip to content

Conversation

MuhammadHamidRaza
Copy link
Contributor

This Pull Request introduces a new set of runnable examples to the examples/exceptions/ directory within the OpenAI Agents SDK repository. These examples are designed to clearly demonstrate how different types of AgentsException are triggered and how they can be handled by users of the SDK.

Each example focuses on a specific exception, providing a minimal, self-contained, and executable code snippet along with detailed docstrings explaining the scenario and expected behavior. This aims to improve the developer experience by offering practical illustrations of error handling.

The following exceptions are covered with dedicated examples:

  1. UserError:

    • File: examples/exceptions/user_error_example.py

    • Description: Demonstrates UserError being raised due to incorrect SDK usage or configuration, such as providing an invalid tool_use_behavior or a tool returning a type that mismatches its declared return type.

  2. MaxTurnsExceeded:

    • File: examples/exceptions/max_turns_exceeded_example.py

    • Description: Illustrates how MaxTurnsExceeded is triggered when an agent's execution requires more turns (e.g., for tool calls and responses) than the max_turns limit set in Runner.run.

  3. ModelBehaviorError:

    • File: examples/exceptions/model_behavior_error_example.py

    • Description: Shows ModelBehaviorError in action, specifically when the underlying language model (LLM) attempts to call a tool that does not exist in the agent's defined tools. This highlights issues arising from unexpected model output or behavior.

  4. InputGuardrailTripwireTriggered:

    • File: examples/guardrails/input_guardrail_example.py (As this is a guardrail-specific exception, it fits well within examples/guardrails as it was already placed there.)

    • Description: Demonstrates how InputGuardrailTripwireTriggered is raised when an input guardrail's conditions are met (e.g., detecting a math homework question), preventing the main agent from processing the input.

  5. OutputGuardrailTripwireTriggered:

    • File: examples/guardrails/output_guardrail_example.py (Similar to InputGuardrail, placed within examples/guardrails.)

    • Description: Illustrates OutputGuardrailTripwireTriggered being raised when an output guardrail's conditions are met (e.g., detecting sensitive data or forbidden content in the agent's final response).

Each example includes:

  • Clear docstrings explaining the purpose and how the exception is triggered.

  • Comprehensive try-except blocks to catch and explain the specific exception.

This addition will be highly beneficial for developers learning to use and debug the OpenAI Agents SDK.

This Pull Request introduces a new set of runnable examples to the `examples/exceptions/` directory within the OpenAI Agents SDK repository. These examples are designed to clearly demonstrate how different types of `AgentsException` are triggered and how they can be handled by users of the SDK.

Each example focuses on a specific exception, providing a minimal, self-contained, and executable code snippet along with detailed docstrings explaining the scenario and expected behavior. This aims to improve the developer experience by offering practical illustrations of error handling.

The following exceptions are covered with dedicated examples:

1.  **`UserError`**:

    * **File:** `examples/exceptions/user_error_example.py`

    * **Description:** Demonstrates `UserError` being raised due to incorrect SDK usage or configuration, such as providing an invalid `tool_use_behavior` or a tool returning a type that mismatches its declared return type.

2.  **`MaxTurnsExceeded`**:

    * **File:** `examples/exceptions/max_turns_exceeded_example.py`

    * **Description:** Illustrates how `MaxTurnsExceeded` is triggered when an agent's execution requires more turns (e.g., for tool calls and responses) than the `max_turns` limit set in `Runner.run`.

3.  **`ModelBehaviorError`**:

    * **File:** `examples/exceptions/model_behavior_error_example.py`

    * **Description:** Shows `ModelBehaviorError` in action, specifically when the underlying language model (LLM) attempts to call a tool that does not exist in the agent's defined tools. This highlights issues arising from unexpected model output or behavior.

4.  **`InputGuardrailTripwireTriggered`**:

    * **File:** `examples/guardrails/input_guardrail_example.py` (As this is a guardrail-specific exception, it fits well within `examples/guardrails` as it was already placed there.)

    * **Description:** Demonstrates how `InputGuardrailTripwireTriggered` is raised when an input guardrail's conditions are met (e.g., detecting a math homework question), preventing the main agent from processing the input.

5.  **`OutputGuardrailTripwireTriggered`**:

    * **File:** `examples/guardrails/output_guardrail_example.py` (Similar to InputGuardrail, placed within `examples/guardrails`.)

    * **Description:** Illustrates `OutputGuardrailTripwireTriggered` being raised when an output guardrail's conditions are met (e.g., detecting sensitive data or forbidden content in the agent's final response).

Each example includes:

* Clear docstrings explaining the purpose and how the exception is triggered.

* Comprehensive `try-except` blocks to catch and explain the specific exception.

This addition will be highly beneficial for developers learning to use and debug the OpenAI Agents SDK.
@seratch
Copy link
Member

seratch commented Jul 30, 2025

Thanks for taking the time to send this pull request. The changes look auto generated using AI, and try/except patterns do not sound relevant because those exceptions actually do not arise with the code snippets. Even if they occur, these do not bring value to this repo. Let me close this PR.

@seratch seratch closed this Jul 30, 2025
@seratch seratch added documentation Improvements or additions to documentation invalid This doesn't seem right labels Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation invalid This doesn't seem right

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants