From 621f78babd03fb2c362c417c27eff9e84a8a1230 Mon Sep 17 00:00:00 2001 From: Wang Date: Fri, 1 Nov 2024 02:07:17 +0800 Subject: [PATCH] community: [fix] add missing tool_calls kwargs of delta message in openai adapter (#27492) - **Description:** add missing tool_calls kwargs of delta message in openai adapter, then tool call will work correctly via adapter's stream chat completion - **Issue:** Fixes https://github.com/langchain-ai/langchain/issues/25436 - **Dependencies:** None --- libs/community/langchain_community/adapters/openai.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/community/langchain_community/adapters/openai.py b/libs/community/langchain_community/adapters/openai.py index 8db939d5c97..673e5fa567b 100644 --- a/libs/community/langchain_community/adapters/openai.py +++ b/libs/community/langchain_community/adapters/openai.py @@ -180,6 +180,12 @@ def _convert_message_chunk(chunk: BaseMessageChunk, i: int) -> dict: # not missing, but None. if i == 0: _dict["content"] = None + if "tool_calls" in chunk.additional_kwargs: + _dict["tool_calls"] = chunk.additional_kwargs["tool_calls"] + # If the first chunk is tool calls, the content is not empty string, + # not missing, but None. + if i == 0: + _dict["content"] = None else: _dict["content"] = chunk.content else: