跳过内容

异常

RunErrorDetails dataclass

在发生异常时从 agent 运行中收集的数据。

源代码位于 src/agents/exceptions.py
@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)

AgentsException

基类: Exception

Agents SDK 中所有异常的基类。

源代码位于 src/agents/exceptions.py
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

MaxTurnsExceeded

基类: AgentsException

当超过最大回合数时引发的异常。

源代码位于 src/agents/exceptions.py
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)

ModelBehaviorError

基类: AgentsException

当模型执行意外操作时引发的异常,例如调用不存在的工具,或提供格式错误的 JSON。

源代码位于 src/agents/exceptions.py
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)

UserError

基类: AgentsException

当用户在使用 SDK 时出错时引发的异常。

源代码位于 src/agents/exceptions.py
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)

InputGuardrailTripwireTriggered

基类: AgentsException

当触发 guardrail 触发器时引发的异常。

源代码位于 src/agents/exceptions.py
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"
        )

guardrail_result instance-attribute

guardrail_result: InputGuardrailResult = guardrail_result

触发的 guardrail 的结果数据。

OutputGuardrailTripwireTriggered

基类: AgentsException

当触发 guardrail 触发器时引发的异常。

源代码位于 src/agents/exceptions.py
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"
        )

guardrail_result instance-attribute

guardrail_result: OutputGuardrailResult = guardrail_result

触发的 guardrail 的结果数据。

ToolInputGuardrailTripwireTriggered

基类: AgentsException

当触发工具输入 guardrail 触发器时引发的异常。

源代码位于 src/agents/exceptions.py
class ToolInputGuardrailTripwireTriggered(AgentsException):
    """Exception raised when a tool input guardrail tripwire is triggered."""

    guardrail: ToolInputGuardrail[Any]
    """The guardrail that was triggered."""

    output: ToolGuardrailFunctionOutput
    """The output from the guardrail function."""

    def __init__(self, guardrail: ToolInputGuardrail[Any], output: ToolGuardrailFunctionOutput):
        self.guardrail = guardrail
        self.output = output
        super().__init__(f"Tool input guardrail {guardrail.__class__.__name__} triggered tripwire")

guardrail instance-attribute

guardrail: ToolInputGuardrail[Any] = guardrail

触发的 guardrail。

输出 实例属性

output: ToolGuardrailFunctionOutput = output

guardrail 函数的输出。

ToolOutputGuardrailTripwireTriggered

基类: AgentsException

当触发工具输出 guardrail 触发器时引发的异常。

源代码位于 src/agents/exceptions.py
class ToolOutputGuardrailTripwireTriggered(AgentsException):
    """Exception raised when a tool output guardrail tripwire is triggered."""

    guardrail: ToolOutputGuardrail[Any]
    """The guardrail that was triggered."""

    output: ToolGuardrailFunctionOutput
    """The output from the guardrail function."""

    def __init__(self, guardrail: ToolOutputGuardrail[Any], output: ToolGuardrailFunctionOutput):
        self.guardrail = guardrail
        self.output = output
        super().__init__(f"Tool output guardrail {guardrail.__class__.__name__} triggered tripwire")

guardrail instance-attribute

guardrail: ToolOutputGuardrail[Any] = guardrail

触发的 guardrail。

输出 实例属性

output: ToolGuardrailFunctionOutput = output

guardrail 函数的输出。