-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: guard against empty/filtered LLM responses in get_first_message_content #524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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") | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the Azure client, the check here should also validate that
Suggested change
|
||||||||||
| return completion.choices[0].message.content | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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), themessageobject might exist but itscontentfield can beNone. Since this function is type-hinted to return astr, returningNonewould 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.