The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
openai[patch]: Improve error message when response type is malformed (#31619)
Li-Kuang Chen4ee6112161e6
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.pyindex f9fa43ec9f..edc5dc5661 100644--- a/libs/partners/openai/langchain_openai/chat_models/base.py+++ b/libs/partners/openai/langchain_openai/chat_models/base.py@@ -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)