Skip to content

Commit

Permalink
openai[patch]: add checking codes for calling AI model get error (#17909
Browse files Browse the repository at this point in the history
)

**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 <[email protected]>
Co-authored-by: Bagatur <[email protected]>
  • Loading branch information
3 people authored and hinthornw committed Apr 26, 2024
1 parent 0d428e4 commit 9d22597
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,14 @@ def _create_chat_result(
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"))
Expand Down
7 changes: 7 additions & 0 deletions libs/partners/openai/langchain_openai/llms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ def _generate(
# 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:
Expand Down

0 comments on commit 9d22597

Please sign in to comment.