mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-13 14:50:00 +00:00
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 <yangkx@asiainfo-int.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
This commit is contained in:
parent
833d61adb3
commit
a8104ea8e9
@ -522,6 +522,14 @@ class ChatOpenAI(BaseChatModel):
|
|||||||
generations = []
|
generations = []
|
||||||
if not isinstance(response, dict):
|
if not isinstance(response, dict):
|
||||||
response = response.model_dump()
|
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"]:
|
for res in response["choices"]:
|
||||||
message = _convert_dict_to_message(res["message"])
|
message = _convert_dict_to_message(res["message"])
|
||||||
generation_info = dict(finish_reason=res.get("finish_reason"))
|
generation_info = dict(finish_reason=res.get("finish_reason"))
|
||||||
|
@ -371,6 +371,13 @@ class BaseOpenAI(BaseLLM):
|
|||||||
# dict. For the transition period, we deep convert it to dict.
|
# dict. For the transition period, we deep convert it to dict.
|
||||||
response = response.model_dump()
|
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"])
|
choices.extend(response["choices"])
|
||||||
_update_token_usage(_keys, response, token_usage)
|
_update_token_usage(_keys, response, token_usage)
|
||||||
if not system_fingerprint:
|
if not system_fingerprint:
|
||||||
|
Loading…
Reference in New Issue
Block a user