Skip to content

fix: guard choices[0] and message=None before content access#1550

Open
qizwiz wants to merge 1 commit into
open-compass:mainfrom
qizwiz:fix/guard-unguarded-llm-choices
Open

fix: guard choices[0] and message=None before content access#1550
qizwiz wants to merge 1 commit into
open-compass:mainfrom
qizwiz:fix/guard-unguarded-llm-choices

Conversation

@qizwiz
Copy link
Copy Markdown

@qizwiz qizwiz commented May 17, 2026

Summary

OpenAI-compatible APIs can return empty choices on error or rate-limiting, causing IndexError at choices[0]. Gemini additionally returns choices[0].message = None on PROHIBITED_CONTENT safety filtering (HTTP 200 — no exception raised), causing AttributeError at .content.

This PR adds the comprehensive guard:

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

before every bare choices[0].message.content access.

Files changed (3 files, 3 sites)

  • vlmeval/dataset/simplevqa.py — comparison answer extraction
  • vlmeval/api/glm_vision.py — GLM vision API adapter
  • vlmeval/api/doubao_vl_api.py — Doubao VL API adapter

Verification

The Gemini choices[0].message = None crash is a documented production behavior on PROHIBITED_CONTENT safety filtering. Static analysis via pact (Z3 Fixedpoint datalog, llm_response_unguarded rule) found all 3 sites; post-fix scan returns 0 violations.

OpenAI-compatible APIs can return empty choices on error or content
filtering; Gemini returns choices[0].message=None on PROHIBITED_CONTENT
(HTTP 200). Both cause unhandled exceptions at runtime.

Adds `if not response.choices or response.choices[0].message is None:`
before every bare `choices[0].message.content` access across 3 files.
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