From a76d83e6cdefeb0627f8e7654bb7d4aae5972931 Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Thu, 19 Dec 2024 17:58:43 -0800 Subject: [PATCH] fix(openai): Remove extra serialized params for OpenAI messages (#7414) --- libs/langchain-openai/src/chat_models.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libs/langchain-openai/src/chat_models.ts b/libs/langchain-openai/src/chat_models.ts index 0906d2848655..3bd3a4e5ee57 100644 --- a/libs/langchain-openai/src/chat_models.ts +++ b/libs/langchain-openai/src/chat_models.ts @@ -15,6 +15,7 @@ import { OpenAIToolCall, isAIMessage, UsageMetadata, + BaseMessageFields, } from "@langchain/core/messages"; import { type ChatGeneration, @@ -1677,9 +1678,13 @@ export class ChatOpenAI< } // Fields are not serialized unless passed to the constructor // Doing this ensures all fields on the message are serialized - generation.message = new AIMessage({ - ...generation.message, - }); + generation.message = new AIMessage( + Object.fromEntries( + Object.entries(generation.message).filter( + ([key]) => !key.startsWith("lc_") + ) + ) as BaseMessageFields + ); generations.push(generation); } return {