Skip to content

Commit e9e9e3a

Browse files
capture all exceptions again
1 parent 59732ed commit e9e9e3a

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

sentry_sdk/integrations/openai_agents/patches/agent_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
end_invoke_agent_span,
99
handoff_span,
1010
)
11-
from ..utils import _capture_exception, _record_exception_on_span
11+
from ..utils import _capture_exception, _record_exception_on_span, _SingleTurnException
1212

1313
from typing import TYPE_CHECKING
1414

@@ -103,7 +103,7 @@ async def patched_run_single_turn(cls, *args, **kwargs):
103103
_record_exception_on_span(span, exc)
104104
end_invoke_agent_span(context_wrapper, agent)
105105

106-
raise exc from None
106+
raise _SingleTurnException(exc)
107107

108108
return result
109109

sentry_sdk/integrations/openai_agents/patches/runner.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from sentry_sdk.integrations import DidNotEnable
55

66
from ..spans import agent_workflow_span, end_invoke_agent_span
7-
from ..utils import _capture_exception, _record_exception_on_span
7+
from ..utils import _capture_exception, _record_exception_on_span, _SingleTurnException
88

99
try:
1010
from agents.exceptions import AgentsException
@@ -53,6 +53,15 @@ async def wrapper(*args, **kwargs):
5353
end_invoke_agent_span(context_wrapper, agent)
5454

5555
raise exc from None
56+
except _SingleTurnException as exc:
57+
# Handled in _run_single_turn() patch.
58+
raise exc.original from None
59+
except Exception as exc:
60+
# Invoke agent span is not finished in this case.
61+
# This is much less likely to occur than other cases because
62+
# AgentRunner.run() is "just" a while loop around _run_single_turn.
63+
_capture_exception(exc)
64+
raise exc from None
5665

5766
end_invoke_agent_span(run_result.context_wrapper, agent)
5867
return run_result

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
raise DidNotEnable("OpenAI Agents not installed")
2727

2828

29+
class _SingleTurnException(Exception):
30+
def __init__(self, original):
31+
self.original = original
32+
33+
2934
def _capture_exception(exc):
3035
# type: (Any) -> None
3136
set_span_errored()

0 commit comments

Comments
 (0)