openai: Removed tool_calls from completion chunk after other chunks have already been sent. (#29649)

- **Description:** Before sending a completion chunk at the end of an
OpenAI stream, removing the tool_calls as those have already been sent
as chunks.
- **Issue:** -
- **Dependencies:** -
- **Twitter handle:** -

@ccurme as mentioned in another PR

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Marc Ammann
2025-02-07 08:15:52 -07:00
committed by GitHub
parent 0d45ad57c1
commit 5690575f13
2 changed files with 28 additions and 0 deletions

View File

@@ -1483,6 +1483,9 @@ class BaseChatOpenAI(BaseChatModel):
chat_message = chat_result.generations[0].message
if isinstance(chat_message, AIMessage):
usage_metadata = chat_message.usage_metadata
# Skip tool_calls, already sent as chunks
if "tool_calls" in chat_message.additional_kwargs:
chat_message.additional_kwargs.pop("tool_calls")
else:
usage_metadata = None
message = AIMessageChunk(