Skip to content

fix: guard against empty choices and message=None in make_api_call#43

Open
qizwiz wants to merge 1 commit into
build-with-groq:mainfrom
qizwiz:fix/guard-empty-llm-response
Open

fix: guard against empty choices and message=None in make_api_call#43
qizwiz wants to merge 1 commit into
build-with-groq:mainfrom
qizwiz:fix/guard-empty-llm-response

Conversation

@qizwiz
Copy link
Copy Markdown

@qizwiz qizwiz commented May 18, 2026

What

Add explicit guards in make_api_call() before accessing response.choices[0].message.content in both the is_final_answer=True and is_final_answer=False branches.

Why

client.chat.completions.create() (Groq's OpenAI-compatible API) can return two empty-response shapes:

  1. choices = [] — on content-policy rejections, rate-limit errors, or provider failures
  2. choices[0].message = None — e.g. when content is filtered, some OpenAI-compatible providers return HTTP 200 with the message field absent

Both crash with IndexError or AttributeError inside the retry loop. The surrounding except Exception as e: catches these crashes but produces a confusing "Failed to generate step after 3 attempts. Error: list index out of range" message, masking the actual cause.

With the explicit guards, the ValueError is raised before the attribute access, caught by the existing retry logic, and reported accurately.

Change

# Before
return response.choices[0].message.content

# After
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

Applied to both the final-answer branch and the step-reasoning branch.

Corpus context

Detected by pact (llm_response_unguarded mode), a Z3-verified static analyzer for LLM crash vectors. This pattern was found across 13.7k violations in 786 repos.

client.chat.completions.create() (Groq's OpenAI-compatible API) can
return choices=[] on content-policy rejections or provider errors, and
choices[0].message=None on filtered responses. Both crash with
IndexError/AttributeError inside the retry loop, producing a confusing
'Failed after 3 attempts' message. Explicit guards raise ValueError so
the except branch catches a meaningful error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant