forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
174 lines (116 loc) · 5.35 KB
/
exceptions.py
File metadata and controls
174 lines (116 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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 .tool_guardrails import (
ToolGuardrailFunctionOutput,
ToolInputGuardrail,
ToolOutputGuardrail,
)
from .util._pretty_print import pretty_print_run_error_details
_DRAIN_STREAM_EVENTS_ATTR = "_agents_drain_queued_stream_events"
def _mark_error_to_drain_stream_events(error: Exception) -> None:
setattr(error, _DRAIN_STREAM_EVENTS_ATTR, True)
def _should_drain_stream_events_before_raising(error: Exception) -> bool:
return bool(getattr(error, _DRAIN_STREAM_EVENTS_ATTR, False))
@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 ModelRefusalError(AgentsException):
"""Exception raised when the model refuses to produce the requested output."""
refusal: str
"""The refusal text returned by the model."""
def __init__(self, refusal: str):
self.refusal = refusal
super().__init__(f"Model refused to produce output: {refusal}")
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 MCPToolCancellationError(AgentsException):
"""Exception raised when an MCP tool call is internally cancelled."""
message: str
def __init__(self, message: str):
self.message = message
super().__init__(message)
class ToolTimeoutError(AgentsException):
"""Exception raised when a function tool invocation exceeds its timeout."""
tool_name: str
timeout_seconds: float
def __init__(self, tool_name: str, timeout_seconds: float):
self.tool_name = tool_name
self.timeout_seconds = timeout_seconds
super().__init__(f"Tool '{tool_name}' timed out after {timeout_seconds:g} seconds.")
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"
)
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")
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")