1
- from typing import TYPE_CHECKING
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from typing import TYPE_CHECKING , Any
2
5
3
6
if TYPE_CHECKING :
7
+ from .agent import Agent
4
8
from .guardrail import InputGuardrailResult , OutputGuardrailResult
9
+ from .items import ModelResponse , RunItem , TResponseInputItem
10
+ from .run_context import RunContextWrapper
11
+
12
+ from .util ._pretty_print import pretty_print_run_error_details
13
+
14
+
15
+ @dataclass
16
+ class RunErrorDetails :
17
+ """Data collected from an agent run when an exception occurs."""
18
+ input : str | list [TResponseInputItem ]
19
+ new_items : list [RunItem ]
20
+ raw_responses : list [ModelResponse ]
21
+ last_agent : Agent [Any ]
22
+ context_wrapper : RunContextWrapper [Any ]
23
+ input_guardrail_results : list [InputGuardrailResult ]
24
+ output_guardrail_results : list [OutputGuardrailResult ]
25
+
26
+ def __str__ (self ) -> str :
27
+ return pretty_print_run_error_details (self )
5
28
6
29
7
30
class AgentsException (Exception ):
8
31
"""Base class for all exceptions in the Agents SDK."""
32
+ run_data : RunErrorDetails | None
33
+
34
+ def __init__ (self , * args : object ) -> None :
35
+ super ().__init__ (* args )
36
+ self .run_data = None
9
37
10
38
11
39
class MaxTurnsExceeded (AgentsException ):
@@ -15,6 +43,7 @@ class MaxTurnsExceeded(AgentsException):
15
43
16
44
def __init__ (self , message : str ):
17
45
self .message = message
46
+ super ().__init__ (message )
18
47
19
48
20
49
class ModelBehaviorError (AgentsException ):
@@ -26,6 +55,7 @@ class ModelBehaviorError(AgentsException):
26
55
27
56
def __init__ (self , message : str ):
28
57
self .message = message
58
+ super ().__init__ (message )
29
59
30
60
31
61
class UserError (AgentsException ):
@@ -35,15 +65,16 @@ class UserError(AgentsException):
35
65
36
66
def __init__ (self , message : str ):
37
67
self .message = message
68
+ super ().__init__ (message )
38
69
39
70
40
71
class InputGuardrailTripwireTriggered (AgentsException ):
41
72
"""Exception raised when a guardrail tripwire is triggered."""
42
73
43
- guardrail_result : " InputGuardrailResult"
74
+ guardrail_result : InputGuardrailResult
44
75
"""The result data of the guardrail that was triggered."""
45
76
46
- def __init__ (self , guardrail_result : " InputGuardrailResult" ):
77
+ def __init__ (self , guardrail_result : InputGuardrailResult ):
47
78
self .guardrail_result = guardrail_result
48
79
super ().__init__ (
49
80
f"Guardrail { guardrail_result .guardrail .__class__ .__name__ } triggered tripwire"
@@ -53,10 +84,10 @@ def __init__(self, guardrail_result: "InputGuardrailResult"):
53
84
class OutputGuardrailTripwireTriggered (AgentsException ):
54
85
"""Exception raised when a guardrail tripwire is triggered."""
55
86
56
- guardrail_result : " OutputGuardrailResult"
87
+ guardrail_result : OutputGuardrailResult
57
88
"""The result data of the guardrail that was triggered."""
58
89
59
- def __init__ (self , guardrail_result : " OutputGuardrailResult" ):
90
+ def __init__ (self , guardrail_result : OutputGuardrailResult ):
60
91
self .guardrail_result = guardrail_result
61
92
super ().__init__ (
62
93
f"Guardrail { guardrail_result .guardrail .__class__ .__name__ } triggered tripwire"
0 commit comments