Skip to content

Commit d94f953

Browse files
.
1 parent 5cee69e commit d94f953

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,15 @@ 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_completions_api_output_data(
710-
span, response, kwargs, integration, start_time, finish_span=True
711-
)
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+
)
712718

713719
return response
714720

@@ -731,6 +737,24 @@ def _set_completions_api_output_data(
731737
)
732738

733739

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+
_common_set_output_data(
749+
span,
750+
response,
751+
kwargs,
752+
integration,
753+
start_time,
754+
finish_span,
755+
)
756+
757+
734758
def _set_responses_api_output_data(
735759
span: "Span",
736760
response: "Any",
@@ -749,6 +773,24 @@ def _set_responses_api_output_data(
749773
)
750774

751775

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+
_common_set_output_data(
785+
span,
786+
response,
787+
kwargs,
788+
integration,
789+
start_time,
790+
finish_span,
791+
)
792+
793+
752794
def _set_embeddings_output_data(
753795
span: "Span",
754796
response: "Any",
@@ -943,9 +985,15 @@ def _new_responses_create_common(f: "Any", *args: "Any", **kwargs: "Any") -> "An
943985
start_time = time.perf_counter()
944986
response = yield f, args, kwargs
945987

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

950998
return response
951999

0 commit comments

Comments
 (0)