From e576adeadf520efecde764aae7df4ab96e6dbc51 Mon Sep 17 00:00:00 2001 From: Brace Sproul Date: Tue, 23 Jul 2024 12:17:50 -0700 Subject: [PATCH] anthropic[patch]: Handle non text llm tokens (#6179) * anthropic[patch]: Handle non text llm tokens * check for input field --- libs/langchain-anthropic/src/chat_models.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libs/langchain-anthropic/src/chat_models.ts b/libs/langchain-anthropic/src/chat_models.ts index 87013e8f8ad7..6c2ce2de1211 100644 --- a/libs/langchain-anthropic/src/chat_models.ts +++ b/libs/langchain-anthropic/src/chat_models.ts @@ -598,9 +598,14 @@ function extractToolCallChunk( } function extractToken(chunk: AIMessageChunk): string | undefined { - return typeof chunk.content === "string" && chunk.content !== "" - ? chunk.content - : undefined; + if (typeof chunk.content === "string") { + return chunk.content; + } else if (Array.isArray(chunk.content) && "input" in chunk.content[0]) { + return typeof chunk.content[0].input === "string" + ? chunk.content[0].input + : JSON.stringify(chunk.content[0].input); + } + return undefined; } function extractToolUseContent(