Skip to content

Commit 6837662

Browse files
ref(openai): Separate output handling (#5543)
Create separate functions for output-handling of `openai` Responses, Completions, and Embeddings API functions. Create streaming variants where applicable.
1 parent c11d1f2 commit 6837662

File tree

1 file changed

+112
-4
lines changed

1 file changed

+112
-4
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def _set_embeddings_input_data(
462462
_commmon_set_input_data(span, kwargs)
463463

464464

465-
def _set_output_data(
465+
def _set_common_output_data(
466466
span: "Span",
467467
response: "Any",
468468
kwargs: "dict[str, Any]",
@@ -706,11 +706,109 @@ def _new_chat_completion_common(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
706706
start_time = time.perf_counter()
707707
response = yield f, args, kwargs
708708

709-
_set_output_data(span, response, kwargs, integration, start_time, finish_span=True)
709+
is_streaming_response = kwargs.get("stream", False)
710+
if is_streaming_response:
711+
_set_streaming_completions_api_output_data(
712+
span, response, kwargs, integration, start_time, finish_span=True
713+
)
714+
else:
715+
_set_completions_api_output_data(
716+
span, response, kwargs, integration, start_time, finish_span=True
717+
)
710718

711719
return response
712720

713721

722+
def _set_completions_api_output_data(
723+
span: "Span",
724+
response: "Any",
725+
kwargs: "dict[str, Any]",
726+
integration: "OpenAIIntegration",
727+
start_time: "Optional[float]" = None,
728+
finish_span: bool = True,
729+
) -> None:
730+
_set_common_output_data(
731+
span,
732+
response,
733+
kwargs,
734+
integration,
735+
start_time,
736+
finish_span,
737+
)
738+
739+
740+
def _set_streaming_completions_api_output_data(
741+
span: "Span",
742+
response: "Any",
743+
kwargs: "dict[str, Any]",
744+
integration: "OpenAIIntegration",
745+
start_time: "Optional[float]" = None,
746+
finish_span: bool = True,
747+
) -> None:
748+
_set_common_output_data(
749+
span,
750+
response,
751+
kwargs,
752+
integration,
753+
start_time,
754+
finish_span,
755+
)
756+
757+
758+
def _set_responses_api_output_data(
759+
span: "Span",
760+
response: "Any",
761+
kwargs: "dict[str, Any]",
762+
integration: "OpenAIIntegration",
763+
start_time: "Optional[float]" = None,
764+
finish_span: bool = True,
765+
) -> None:
766+
_set_common_output_data(
767+
span,
768+
response,
769+
kwargs,
770+
integration,
771+
start_time,
772+
finish_span,
773+
)
774+
775+
776+
def _set_streaming_responses_api_output_data(
777+
span: "Span",
778+
response: "Any",
779+
kwargs: "dict[str, Any]",
780+
integration: "OpenAIIntegration",
781+
start_time: "Optional[float]" = None,
782+
finish_span: bool = True,
783+
) -> None:
784+
_set_common_output_data(
785+
span,
786+
response,
787+
kwargs,
788+
integration,
789+
start_time,
790+
finish_span,
791+
)
792+
793+
794+
def _set_embeddings_output_data(
795+
span: "Span",
796+
response: "Any",
797+
kwargs: "dict[str, Any]",
798+
integration: "OpenAIIntegration",
799+
start_time: "Optional[float]" = None,
800+
finish_span: bool = True,
801+
) -> None:
802+
_set_common_output_data(
803+
span,
804+
response,
805+
kwargs,
806+
integration,
807+
start_time,
808+
finish_span,
809+
)
810+
811+
714812
def _wrap_chat_completion_create(f: "Callable[..., Any]") -> "Callable[..., Any]":
715813
def _execute_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any":
716814
gen = _new_chat_completion_common(f, *args, **kwargs)
@@ -795,7 +893,9 @@ def _new_embeddings_create_common(f: "Any", *args: "Any", **kwargs: "Any") -> "A
795893

796894
response = yield f, args, kwargs
797895

798-
_set_output_data(span, response, kwargs, integration, finish_span=False)
896+
_set_embeddings_output_data(
897+
span, response, kwargs, integration, finish_span=False
898+
)
799899

800900
return response
801901

@@ -885,7 +985,15 @@ def _new_responses_create_common(f: "Any", *args: "Any", **kwargs: "Any") -> "An
885985
start_time = time.perf_counter()
886986
response = yield f, args, kwargs
887987

888-
_set_output_data(span, response, kwargs, integration, start_time, finish_span=True)
988+
is_streaming_response = kwargs.get("stream", False)
989+
if is_streaming_response:
990+
_set_streaming_responses_api_output_data(
991+
span, response, kwargs, integration, start_time, finish_span=True
992+
)
993+
else:
994+
_set_responses_api_output_data(
995+
span, response, kwargs, integration, start_time, finish_span=True
996+
)
889997

890998
return response
891999

0 commit comments

Comments
 (0)