Skip to content

fix: guard against empty choices and message=None in LLM responses (3 sites) - #444

Open
qizwiz wants to merge 1 commit into
anarchy-ai:mainfrom
qizwiz:fix/guard-empty-choices-llm-response
Open

fix: guard against empty choices and message=None in LLM responses (3 sites)#444
qizwiz wants to merge 1 commit into
anarchy-ai:mainfrom
qizwiz:fix/guard-empty-choices-llm-response

Conversation

@qizwiz

@qizwiz qizwiz commented May 18, 2026

Copy link
Copy Markdown

What

Guards response.choices[0] in the FLAT and REBEL agent helpers against two crash vectors — empty choices list and choices[0].message = None:

src/llm_vm/agents/FLAT/agent_helper/requests/call_open_ai.py (2 sites):

# OPENAI_CHAT path
+ if not response.choices or response.choices[0].message is None:
+     raise ValueError("LLM returned empty or filtered response")
  response_text = response.choices[0].message.content

# OPENAI_COMPLETION path
+ if not response.choices:
+     raise ValueError("LLM returned empty choices list")
  response_text = response.choices[0].text

src/llm_vm/agents/REBEL/utils.py (1 site):

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

Why

The OpenAI API can return choices=[] (HTTP 200, empty list) or choices[0].message=None when content is filtered. Without the guard, the agent crashes with an unhandled IndexError or AttributeError — there's no outer exception handler in these paths that would catch these specific errors gracefully.

Evidence

  • Live production crash — confirmed in the wild from the same pattern
  • Gemini 2.5 Flash returns choices[0].message = None on PROHIBITED_CONTENT with HTTP 200
  • Found by pact across 13.5k violations in 759 real repositories

Scope

2 files, 6 lines added.

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