Skip to content

OpenAI integration fails when response has no messages in choices. #5071

@dchaplinsky

Description

@dchaplinsky

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.43.0

Steps to Reproduce

I'm using openai lib with gemini. For couple of my tasks gemini's response was like this:

{
    "id": "vOALaYOpG4CJ7M8PlpOM0QY",
    "choices": [
        {
            "finish_reason": "content_filter: PROHIBITED_CONTENT",
            "index": 0,
            "logprobs": null,
            "message": null
        }
    ],
    "created": 1762386108,
    "model": "gemini-2.5-flash-lite",
    "object": "chat.completion",
    "service_tier": null,
    "system_fingerprint": null,
    "usage": {
        "completion_tokens": 0,
        "prompt_tokens": 5431,
        "total_tokens": 5431,
        "completion_tokens_details": null,
        "prompt_tokens_details": null
    }
}

This made openai integration choke like following:

File /usr/local/lib/python3.12/site-packages/sentry_sdk/integrations/openai.py:246, in _set_output_data(span, response, kwargs, integration, finish_span)
    244 if hasattr(response, "choices"):
    245     if should_send_default_pii() and integration.include_prompts:
--> 246         response_text = [choice.message.model_dump() for choice in response.choices]
    247         if len(response_text) > 0:
    248             set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_text)

AttributeError: 'NoneType' object has no attribute 'model_dump'

Which is understandable, as integration clearly expects message to be present.

I'm using latest sdk and openai client (openai==2.7.1)

Expected Result

It should ignore such messages or report them separately

Actual Result

It dies with a traceback even before I can read the message from openai lib.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions