Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/azureai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
def get_first_message_content(completion: ChatCompletion) -> str:
r"""When we only need the content of the first message.
It is the default parser for chat completion."""
if not completion.choices or completion.choices[0].message is None:
raise ValueError("LLM returned empty or filtered response")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check should also include a validation for completion.choices[0].message.content. In some scenarios (such as content filtering or when the model returns a tool call instead of text), the message object might exist but its content field can be None. Since this function is type-hinted to return a str, returning None would violate the contract and likely cause errors in downstream components that expect a string. Adding this check ensures that we only proceed when actual text content is available.

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

return completion.choices[0].message.content


Expand Down
2 changes: 2 additions & 0 deletions api/openai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def get_first_message_content(completion: ChatCompletion) -> str:
r"""When we only need the content of the first message.
It is the default parser for chat completion."""
log.debug(f"raw completion: {completion}")
if not completion.choices or completion.choices[0].message is None:
raise ValueError("LLM returned empty or filtered response")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the Azure client, the check here should also validate that completion.choices[0].message.content is not None. This prevents the function from returning None when a str is expected, which can happen if the response is filtered or contains non-textual data. This is especially important given the PR's goal of guarding against "filtered" responses.

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

return completion.choices[0].message.content


Expand Down