diff --git a/api/azureai_client.py b/api/azureai_client.py index 948e86c30..5d70e5cee 100644 --- a/api/azureai_client.py +++ b/api/azureai_client.py @@ -75,6 +75,12 @@ 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 + or completion.choices[0].message.content is None + ): + raise ValueError("LLM returned empty or filtered response") return completion.choices[0].message.content diff --git a/api/openai_client.py b/api/openai_client.py index bc75ed586..afa4059ad 100644 --- a/api/openai_client.py +++ b/api/openai_client.py @@ -59,6 +59,12 @@ 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 + or completion.choices[0].message.content is None + ): + raise ValueError("LLM returned empty or filtered response") return completion.choices[0].message.content