Skip to content

Commit c747043

Browse files
fix(openai): Check response text is present to avoid AttributeError (#5081)
Verify that messages are not null before calling `model_dump()` on message objects in chat completion choices. Gemini's API can return `null` values in these fields. Closes #5071
1 parent 98fa10a commit c747043

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ def _set_output_data(span, response, kwargs, integration, finish_span=True):
243243

244244
if hasattr(response, "choices"):
245245
if should_send_default_pii() and integration.include_prompts:
246-
response_text = [choice.message.model_dump() for choice in response.choices]
246+
response_text = [
247+
choice.message.model_dump()
248+
for choice in response.choices
249+
if choice.message is not None
250+
]
247251
if len(response_text) > 0:
248252
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_text)
249253

0 commit comments

Comments
 (0)