From a8104ea8e9fd58e29f44957ebd8f208bde097395 Mon Sep 17 00:00:00 2001 From: Kaixin Yang <106639000+yangkx111@users.noreply.github.com> Date: Fri, 29 Mar 2024 08:17:32 +0800 Subject: [PATCH] openai[patch]: add checking codes for calling AI model get error (#17909) **Description:**: adding checking codes for calling AI model get error in chat_models/base.py and llms/base.py **Issue**: 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. **Dependencies**: None --------- Co-authored-by: yangkx Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> --- libs/partners/openai/langchain_openai/chat_models/base.py | 8 ++++++++ libs/partners/openai/langchain_openai/llms/base.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 90b7c9c8fd5..fcabe5c99a7 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -522,6 +522,14 @@ class ChatOpenAI(BaseChatModel): generations = [] if not isinstance(response, dict): response = 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. + if response.get("error"): + raise ValueError(response.get("error")) + for res in response["choices"]: message = _convert_dict_to_message(res["message"]) generation_info = dict(finish_reason=res.get("finish_reason")) diff --git a/libs/partners/openai/langchain_openai/llms/base.py b/libs/partners/openai/langchain_openai/llms/base.py index 59e5faf69c6..f5602a816ba 100644 --- a/libs/partners/openai/langchain_openai/llms/base.py +++ b/libs/partners/openai/langchain_openai/llms/base.py @@ -371,6 +371,13 @@ class BaseOpenAI(BaseLLM): # dict. For the transition period, we deep convert it to dict. response = 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. + if response.get("error"): + raise ValueError(response.get("error")) + choices.extend(response["choices"]) _update_token_usage(_keys, response, token_usage) if not system_fingerprint: