Skip to content

Commit 961eee2

Browse files
fix(openai): Only finish relevant spans in Chat Completions patches (#6190)
OpenAI Completions spans are only created in `_new_sync_chat_completion()` and `_new_async_chat_completion()`. Exit spans in these functions when the openai library function raises an exception. Resolves an edge case where `_new_sync_chat_completion()` or `_new_async_chat_completion()` exits early without creating a span, causing `get_current_span()` to return a non-openai span.
1 parent ae2cf7c commit 961eee2

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

sentry_sdk/integrations/openai.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ def _new_sync_chat_completion(f: "Any", *args: "Any", **kwargs: "Any") -> "Any":
709709
except Exception as exc:
710710
exc_info = sys.exc_info()
711711
with capture_internal_exceptions():
712-
_capture_exception(exc)
712+
_capture_exception(exc, manual_span_cleanup=False)
713+
span.__exit__(None, None, None)
713714
reraise(*exc_info)
714715

715716
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
@@ -776,7 +777,8 @@ async def _new_async_chat_completion(f: "Any", *args: "Any", **kwargs: "Any") ->
776777
except Exception as exc:
777778
exc_info = sys.exc_info()
778779
with capture_internal_exceptions():
779-
_capture_exception(exc)
780+
_capture_exception(exc, manual_span_cleanup=False)
781+
span.__exit__(None, None, None)
780782
reraise(*exc_info)
781783

782784
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.

0 commit comments

Comments
 (0)