From 763ef9262ceedaa0e80bc1fc2947ea12386688de Mon Sep 17 00:00:00 2001 From: Brace Sproul Date: Thu, 10 Oct 2024 18:23:43 -0700 Subject: [PATCH] fix(openai): Ensure usage_metadata is serialized when invoking (#6966) --- libs/langchain-openai/src/chat_models.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libs/langchain-openai/src/chat_models.ts b/libs/langchain-openai/src/chat_models.ts index 93a3dbd30e45..728d08eabed0 100644 --- a/libs/langchain-openai/src/chat_models.ts +++ b/libs/langchain-openai/src/chat_models.ts @@ -1506,6 +1506,11 @@ export class ChatOpenAI< if (isAIMessage(generation.message)) { generation.message.usage_metadata = usageMetadata; } + // 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, + }); generations.push(generation); } return {