Skip to content

fix(examples): guard against empty choices in LLM completion response#232

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

fix(examples): guard against empty choices in LLM completion response#232
qizwiz wants to merge 1 commit into
operand:mainfrom
qizwiz:fix/guard-empty-choices-llm-response

Conversation

@qizwiz
Copy link
Copy Markdown

@qizwiz qizwiz commented May 18, 2026

What

Guards completion.choices[0] in examples/demo/agents/openai_completion_agent.py before accessing .text:

# Before
action = util.extract_json(completion.choices[0].text, ["/END"])

# After
if not completion.choices:
    raise ValueError("LLM returned empty choices list")
action = util.extract_json(completion.choices[0].text, ["/END"])

Why

The OpenAI Completions API can return choices=[] at HTTP 200 when content is filtered. Without the guard, this raises an unhandled IndexError that crashes the agent mid-action, losing any state built up during the conversation.

Evidence

Scope

1 file, 2 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