from __future__ import annotations from dataclasses import dataclass from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from .agent import Agent from .guardrail import InputGuardrailResult, OutputGuardrailResult from .items import ModelResponse, RunItem, TResponseInputItem from .run_context import RunContextWrapper from .util._pretty_print import pretty_print_run_error_details @dataclass class RunErrorDetails: """Data collected from an agent run when an exception occurs.""" input: str | list[TResponseInputItem] new_items: list[RunItem] raw_responses: list[ModelResponse] last_agent: Agent[Any] context_wrapper: RunContextWrapper[Any] input_guardrail_results: list[InputGuardrailResult] output_guardrail_results: list[OutputGuardrailResult] def __str__(self) -> str: return pretty_print_run_error_details(self) class AgentsException(Exception): """Base class for all exceptions in the Agents SDK.""" run_data: RunErrorDetails | None def __init__(self, *args: object) -> None: super().__init__(*args) self.run_data = None class MaxTurnsExceeded(AgentsException): """Exception raised when the maximum number of turns is exceeded.""" message: str def __init__(self, message: str): self.message = message super().__init__(message) class ModelBehaviorError(AgentsException): """Exception raised when the model does something unexpected, e.g. calling a tool that doesn't exist, or providing malformed JSON. """ message: str def __init__(self, message: str): self.message = message super().__init__(message) class UserError(AgentsException): """Exception raised when the user makes an error using the SDK.""" message: str def __init__(self, message: str): self.message = message super().__init__(message) class InputGuardrailTripwireTriggered(AgentsException): """Exception raised when a guardrail tripwire is triggered.""" guardrail_result: InputGuardrailResult """The result data of the guardrail that was triggered.""" def __init__(self, guardrail_result: InputGuardrailResult): self.guardrail_result = guardrail_result super().__init__( f"Guardrail {guardrail_result.guardrail.__class__.__name__} triggered tripwire" ) class OutputGuardrailTripwireTriggered(AgentsException): """Exception raised when a guardrail tripwire is triggered.""" guardrail_result: OutputGuardrailResult """The result data of the guardrail that was triggered.""" def __init__(self, guardrail_result: OutputGuardrailResult): self.guardrail_result = guardrail_result super().__init__( f"Guardrail {guardrail_result.guardrail.__class__.__name__} triggered tripwire" )