From 0207dc1431c29379b724f51c09fa49e6b0333639 Mon Sep 17 00:00:00 2001 From: Tom Daniel Grande Date: Tue, 3 Sep 2024 22:30:03 +0200 Subject: [PATCH] community: delta in openai choice can be None, creates handler for that (#25954) Thank you for contributing to LangChain! - [X ] **PR title** - [X ] **PR message**: **Description:** adds a handler for when delta choice is None **Issue:** Fixes #25951 **Dependencies:** Not applicable - [ X] **Add tests and docs**: Not applicable - [X ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. Co-authored-by: Grande Co-authored-by: Erick Friis --- libs/community/langchain_community/chat_models/openai.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/community/langchain_community/chat_models/openai.py b/libs/community/langchain_community/chat_models/openai.py index a8d197d3be..54326d0acc 100644 --- a/libs/community/langchain_community/chat_models/openai.py +++ b/libs/community/langchain_community/chat_models/openai.py @@ -403,6 +403,8 @@ class ChatOpenAI(BaseChatModel): if len(chunk["choices"]) == 0: continue choice = chunk["choices"][0] + if choice["delta"] is None: + continue chunk = _convert_delta_to_message_chunk( choice["delta"], default_chunk_class ) @@ -495,6 +497,8 @@ class ChatOpenAI(BaseChatModel): if len(chunk["choices"]) == 0: continue choice = chunk["choices"][0] + if choice["delta"] is None: + continue chunk = _convert_delta_to_message_chunk( choice["delta"], default_chunk_class )