diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index 99a50fcd0c..1fbc22021e 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -301,7 +301,7 @@ def _calculate_responses_token_usage( if streaming_message_responses is not None: for message in streaming_message_responses: output_tokens += count_tokens(message) - elif hasattr(response, "output"): + elif hasattr(response, "output") and isinstance(response.output, list): for output_item in response.output: if hasattr(output_item, "content"): for content_item in output_item.content: @@ -711,7 +711,9 @@ def _set_common_output_data( "tool": [], } - if has_data_collection_enabled(client.options): + if has_data_collection_enabled(client.options) and isinstance( + response.output, list + ): record_outputs = client.options["data_collection"]["gen_ai"]["outputs"] if record_outputs: @@ -741,7 +743,11 @@ def _set_common_output_data( span, SPANDATA.GEN_AI_RESPONSE_TEXT, output_messages["response"] ) - elif should_send_default_pii() and integration.include_prompts: + elif ( + should_send_default_pii() + and integration.include_prompts + and isinstance(response.output, list) + ): for output in response.output: if output.type == "function_call": output_messages["tool"].append(output.dict())