Skip to content

Commit d7e3bfb

Browse files
committed
fix: address review comments
1 parent 599cdd0 commit d7e3bfb

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

sentry_sdk/integrations/openai_agents/patches/agent_run.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from sentry_sdk.consts import SPANDATA
55
from sentry_sdk.integrations import DidNotEnable
6-
from sentry_sdk.utils import reraise
6+
from sentry_sdk.utils import capture_internal_exceptions, reraise
77
from ..spans import (
88
invoke_agent_span,
99
end_invoke_agent_span,
@@ -218,11 +218,13 @@ async def patched_run_single_turn_streamed(
218218
try:
219219
result = await original_run_single_turn_streamed(*args, **kwargs)
220220
except Exception as exc:
221-
if span is not None and span.timestamp is None:
222-
_record_exception_on_span(span, exc)
223-
end_invoke_agent_span(context_wrapper, agent)
224-
_close_streaming_workflow_span(agent)
225-
reraise(*sys.exc_info())
221+
exc_info = sys.exc_info()
222+
with capture_internal_exceptions():
223+
if span is not None and span.timestamp is None:
224+
_record_exception_on_span(span, exc)
225+
end_invoke_agent_span(context_wrapper, agent)
226+
_close_streaming_workflow_span(agent)
227+
reraise(*exc_info)
226228

227229
return result
228230

sentry_sdk/integrations/openai_agents/patches/models.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,11 @@ async def wrapped_stream_response(*args: "Any", **kwargs: "Any") -> "Any":
145145
if len(args) > 1:
146146
span_kwargs["input"] = args[1]
147147

148-
span = ai_client_span(agent, span_kwargs)
149-
span.__enter__()
150-
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
148+
with ai_client_span(agent, span_kwargs) as span:
149+
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
150+
151+
streaming_response = None
151152

152-
streaming_response = None
153-
try:
154153
async for event in original_stream_response(*args, **kwargs):
155154
# Capture the full response from ResponseCompletedEvent
156155
if hasattr(event, "response"):
@@ -167,8 +166,6 @@ async def wrapped_stream_response(*args: "Any", **kwargs: "Any") -> "Any":
167166
)
168167
_set_response_model_on_agent_span(agent, response_model)
169168
update_ai_client_span(span, streaming_response)
170-
finally:
171-
span.__exit__(*sys.exc_info())
172169

173170
model.stream_response = wrapped_stream_response
174171

0 commit comments

Comments
 (0)