Skip to content

fix: guard against empty/filtered LLM responses in OpenAI extension#1891

Open
qizwiz wants to merge 1 commit into
sinaptik-ai:mainfrom
qizwiz:fix/llm-response-unguarded
Open

fix: guard against empty/filtered LLM responses in OpenAI extension#1891
qizwiz wants to merge 1 commit into
sinaptik-ai:mainfrom
qizwiz:fix/llm-response-unguarded

Conversation

@qizwiz
Copy link
Copy Markdown

@qizwiz qizwiz commented May 18, 2026

What

Adds a null check in extensions/llms/openai/pandasai_openai/base.py before returning response.choices[0].message.content.

Why

The OpenAI SDK can return two silent failure modes that cause unhandled crashes at this line:

  1. IndexErrorchoices is an empty list ([]) when the provider returns no completions
  2. AttributeErrorchoices[0].message is None when content is filtered (e.g. Gemini returns HTTP 200 with PROHIBITED_CONTENT finish reason)

Fix

if not response.choices or response.choices[0].message is None:
    raise ValueError("LLM returned empty or filtered response")
return response.choices[0].message.content

Detected by pact static analysis.

Accessing choices[0].message when choices is empty raises IndexError;
accessing .content when message is None raises AttributeError. Add a
null check before the return in OpenAI._chat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant