mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-13 22:59:05 +00:00
openai[patch]: Improve error message when response type is malformed (#31619)
This commit is contained in:
parent
9de4f22205
commit
4ee6112161
@ -1111,15 +1111,27 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
response_dict = (
|
response_dict = (
|
||||||
response if isinstance(response, dict) else response.model_dump()
|
response if isinstance(response, dict) else response.model_dump()
|
||||||
)
|
)
|
||||||
# Sometimes the AI Model calling will get error, we should raise it.
|
# Sometimes the AI Model calling will get error, we should raise it (this is
|
||||||
# Otherwise, the next code 'choices.extend(response["choices"])'
|
# typically followed by a null value for `choices`, which we raise for
|
||||||
# will throw a "TypeError: 'NoneType' object is not iterable" error
|
# separately below).
|
||||||
# to mask the true error. Because 'response["choices"]' is None.
|
|
||||||
if response_dict.get("error"):
|
if response_dict.get("error"):
|
||||||
raise ValueError(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")
|
token_usage = response_dict.get("usage")
|
||||||
for res in response_dict["choices"]:
|
|
||||||
|
for res in choices:
|
||||||
message = _convert_dict_to_message(res["message"])
|
message = _convert_dict_to_message(res["message"])
|
||||||
if token_usage and isinstance(message, AIMessage):
|
if token_usage and isinstance(message, AIMessage):
|
||||||
message.usage_metadata = _create_usage_metadata(token_usage)
|
message.usage_metadata = _create_usage_metadata(token_usage)
|
||||||
|
Loading…
Reference in New Issue
Block a user