Skip to content

fix: guard against empty/filtered LLM responses in openai adapter#159

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

fix: guard against empty/filtered LLM responses in openai adapter#159
qizwiz wants to merge 1 commit into
OpenBMB:mainfrom
qizwiz:fix/llm-response-unguarded

Conversation

@qizwiz
Copy link
Copy Markdown

@qizwiz qizwiz commented May 18, 2026

What

Adds null checks before accessing choices[0].message in three files:

  • agentverse/llms/openai.pygenerate_response and agenerate_response (4 locations)
  • scripts/evaluate_responsegen.py — eval loop
  • agentverse/memory/chat_history.pysummarize_memory

Why

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

  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)

Both crash the agent mid-task with an unhelpful traceback rather than a clear error.

Fix

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

This converts both failure modes into a single, descriptive ValueError that callers can catch and handle.

Detected by pact static analysis.

An empty choices list raises IndexError; a None message (returned by
some providers on filtered content with HTTP 200) raises AttributeError.
Add null checks in generate_response, agenerate_response, evaluate_responsegen,
and chat_history.summarize_memory before accessing choices[0].message.
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