Skip to content

fix: guard against empty choices and message=None in LLM response#601

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

fix: guard against empty choices and message=None in LLM response#601
qizwiz wants to merge 1 commit into
datawhalechina:mainfrom
qizwiz:fix/guard-unguarded-llm-choices

Conversation

@qizwiz
Copy link
Copy Markdown

@qizwiz qizwiz commented May 18, 2026

Problem

In Co-creation-projects/JJason-DeepCastAgent/backend/src/services/script_generator.py, the LLM response is accessed without checking:

content = response.choices[0].message.content

This raises IndexError (empty choices) or AttributeError (message=None on filtered content).

Fix

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

2 lines added, 0 deleted.

Detected by pact static analysis.

response.choices[0].message.content raises IndexError when choices is
empty or AttributeError when message is None (filtered/refused content).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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