openai[patch]: Improve error message when response type is malformed (#31619)

This commit is contained in:
Li-Kuang Chen 2025-06-22 02:15:21 +08:00 committed by GitHub
parent 9de4f22205
commit 4ee6112161
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1111,15 +1111,27 @@ class BaseChatOpenAI(BaseChatModel):
response_dict = (
response if isinstance(response, dict) else response.model_dump()
)
# Sometimes the AI Model calling will get error, we should raise it.
# Otherwise, the next code 'choices.extend(response["choices"])'
# will throw a "TypeError: 'NoneType' object is not iterable" error
# to mask the true error. Because 'response["choices"]' is None.
# Sometimes the AI Model calling will get error, we should raise it (this is
# typically followed by a null value for `choices`, which we raise for
# separately below).
if response_dict.get("error"):
raise ValueError(response_dict.get("error"))
# Raise informative error messages for non-OpenAI chat completions APIs
# that return malformed responses.
try:
choices = response_dict["choices"]
except KeyError as e:
raise KeyError(
f"Response missing `choices` key: {response_dict.keys()}"
) from e
if choices is None:
raise TypeError("Received response with null value for `choices`.")
token_usage = response_dict.get("usage")
for res in response_dict["choices"]:
for res in choices:
message = _convert_dict_to_message(res["message"])
if token_usage and isinstance(message, AIMessage):
message.usage_metadata = _create_usage_metadata(token_usage)