Skip to content

Commit 387b5eb

Browse files
Changes requested by rm-openai
1 parent c1e43a6 commit 387b5eb

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/agents/exceptions.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import TYPE_CHECKING, Optional
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
24

35
if TYPE_CHECKING:
46
from .guardrail import InputGuardrailResult, OutputGuardrailResult
@@ -13,9 +15,9 @@ class MaxTurnsExceeded(AgentsException):
1315
"""Exception raised when the maximum number of turns is exceeded."""
1416

1517
message: str
16-
run_error_details: Optional["RunErrorDetails"]
18+
run_error_details: RunErrorDetails | None
1719

18-
def __init__(self, message: str, run_error_details: Optional["RunErrorDetails"] = None):
20+
def __init__(self, message: str, run_error_details: RunErrorDetails | None = None):
1921
self.message = message
2022
self.run_error_details = run_error_details
2123

@@ -43,10 +45,10 @@ def __init__(self, message: str):
4345
class InputGuardrailTripwireTriggered(AgentsException):
4446
"""Exception raised when a guardrail tripwire is triggered."""
4547

46-
guardrail_result: "InputGuardrailResult"
48+
guardrail_result: InputGuardrailResult
4749
"""The result data of the guardrail that was triggered."""
4850

49-
def __init__(self, guardrail_result: "InputGuardrailResult"):
51+
def __init__(self, guardrail_result: InputGuardrailResult):
5052
self.guardrail_result = guardrail_result
5153
super().__init__(
5254
f"Guardrail {guardrail_result.guardrail.__class__.__name__} triggered tripwire"
@@ -56,10 +58,10 @@ def __init__(self, guardrail_result: "InputGuardrailResult"):
5658
class OutputGuardrailTripwireTriggered(AgentsException):
5759
"""Exception raised when a guardrail tripwire is triggered."""
5860

59-
guardrail_result: "OutputGuardrailResult"
61+
guardrail_result: OutputGuardrailResult
6062
"""The result data of the guardrail that was triggered."""
6163

62-
def __init__(self, guardrail_result: "OutputGuardrailResult"):
64+
def __init__(self, guardrail_result: OutputGuardrailResult):
6365
self.guardrail_result = guardrail_result
6466
super().__init__(
6567
f"Guardrail {guardrail_result.guardrail.__class__.__name__} triggered tripwire"

src/agents/result.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,28 @@ def _cleanup_tasks(self):
249249
def __str__(self) -> str:
250250
return pretty_print_run_result_streaming(self)
251251

252+
252253
@dataclass
253-
class RunErrorDetails(RunResultBase):
254+
class RunErrorDetails:
255+
input: str | list[TResponseInputItem]
256+
"""The original input items i.e. the items before run() was called. This may be a mutated
257+
version of the input, if there are handoff input filters that mutate the input.
258+
"""
259+
260+
new_items: list[RunItem]
261+
"""The new items generated during the agent run. These include things like new messages, tool
262+
calls and their outputs, etc.
263+
"""
264+
265+
raw_responses: list[ModelResponse]
266+
"""The raw LLM responses generated by the model during the agent run."""
267+
268+
input_guardrail_results: list[InputGuardrailResult]
269+
"""Guardrail results for the input messages."""
270+
271+
context_wrapper: RunContextWrapper[Any]
272+
"""The context wrapper for the agent run."""
273+
254274
_last_agent: Agent[Any]
255275

256276
@property

src/agents/run.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
from __future__ import annotations
23

34
import asyncio
@@ -208,19 +209,8 @@ async def run(
208209
data={"max_turns": max_turns},
209210
),
210211
)
211-
run_error_details = RunErrorDetails(
212-
input=original_input,
213-
new_items=generated_items,
214-
raw_responses=model_responses,
215-
final_output=None,
216-
input_guardrail_results=input_guardrail_results,
217-
output_guardrail_results=[],
218-
context_wrapper=context_wrapper,
219-
_last_agent=current_agent
220-
)
221212
raise MaxTurnsExceeded(
222-
f"Max turns ({max_turns}) exceeded",
223-
run_error_details
213+
f"Max turns ({max_turns}) exceeded"
224214
)
225215

226216
logger.debug(
@@ -296,6 +286,23 @@ async def run(
296286
raise AgentsException(
297287
f"Unknown next step type: {type(turn_result.next_step)}"
298288
)
289+
except Exception as e:
290+
run_error_details = RunErrorDetails(
291+
input=original_input,
292+
new_items=generated_items,
293+
raw_responses=model_responses,
294+
input_guardrail_results=input_guardrail_results,
295+
context_wrapper=context_wrapper,
296+
_last_agent=current_agent
297+
)
298+
# Re-raise with the error details
299+
if isinstance(e, MaxTurnsExceeded):
300+
raise MaxTurnsExceeded(
301+
f"Max turns ({max_turns}) exceeded",
302+
run_error_details
303+
) from e
304+
else:
305+
raise
299306
finally:
300307
if current_span:
301308
current_span.finish(reset_current=True)

0 commit comments

Comments
 (0)