Skip to content

Commit 4e16e04

Browse files
committed
Make sure the span an error occures in is captured
1 parent bef3e36 commit 4e16e04

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def count_tokens(self, s):
7474

7575
def _capture_exception(exc):
7676
# type: (Any) -> None
77+
# Close an eventually open span
78+
# We need to do this by hand because we are not using the start_span context manager
79+
current_span = sentry_sdk.get_current_span()
80+
if current_span is not None:
81+
current_span.__exit__(None, None, None)
82+
7783
event, hint = event_from_exception(
7884
exc,
7985
client_options=sentry_sdk.get_client().options,

tests/integrations/openai/test_openai.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,9 +1062,14 @@ def test_error_in_responses_api(sentry_init, capture_events):
10621062
)
10631063

10641064
(error_event, transaction_event) = events
1065+
1066+
assert transaction_event["type"] == "transaction"
1067+
# make sure the span where the error occurred is captured
1068+
assert transaction_event["spans"][0]["op"] == "gen_ai.responses"
1069+
10651070
assert error_event["level"] == "error"
10661071
assert error_event["exception"]["values"][0]["type"] == "OpenAIError"
1067-
assert transaction_event["type"] == "transaction"
1072+
10681073
assert (
10691074
error_event["contexts"]["trace"]["trace_id"]
10701075
== transaction_event["contexts"]["trace"]["trace_id"]

0 commit comments

Comments
 (0)