From be74207945491c72d104cfd3e771fd75eebb09e7 Mon Sep 17 00:00:00 2001
From: Thuc Pham <51660321+thucpn@users.noreply.github.com>
Date: Thu, 20 Feb 2025 12:47:43 +0700
Subject: [PATCH] fix: dont add empty text block to tool call (#1670)

---
 .changeset/four-crabs-design.md         |  5 ++++
 packages/providers/anthropic/src/llm.ts | 40 ++++++++++++++-----------
 2 files changed, 28 insertions(+), 17 deletions(-)
 create mode 100644 .changeset/four-crabs-design.md

diff --git a/.changeset/four-crabs-design.md b/.changeset/four-crabs-design.md
new file mode 100644
index 000000000..bbfdd6af4
--- /dev/null
+++ b/.changeset/four-crabs-design.md
@@ -0,0 +1,5 @@
+---
+"@llamaindex/anthropic": patch
+---
+
+fix: dont add empty text block to tool call
diff --git a/packages/providers/anthropic/src/llm.ts b/packages/providers/anthropic/src/llm.ts
index ca9747d9d..dd2f6414d 100644
--- a/packages/providers/anthropic/src/llm.ts
+++ b/packages/providers/anthropic/src/llm.ts
@@ -207,23 +207,26 @@ export class Anthropic extends ToolCallLLM<
       }
 
       if ("toolCall" in options) {
-        const formattedMessage: MessageParam = {
-          role: "assistant",
-          content: [
-            {
-              type: "text" as const,
-              text: extractText(message.content),
-            },
-            ...options.toolCall.map((tool) => ({
-              type: "tool_use" as const,
-              id: tool.id,
-              name: tool.name,
-              input: this.parseToolInput(tool.input),
-            })),
-          ],
-        };
+        const text = extractText(message.content);
 
-        return formattedMessage;
+        const content: MessageParam["content"] = [];
+        if (text && text.trim().length > 0) {
+          // don't add empty text blocks
+          content.push({
+            type: "text" as const,
+            text: text,
+          });
+        }
+        content.push(
+          ...options.toolCall.map((tool) => ({
+            type: "tool_use" as const,
+            id: tool.id,
+            name: tool.name,
+            input: this.parseToolInput(tool.input),
+          })),
+        );
+
+        return { role: "assistant", content } satisfies MessageParam;
       }
 
       // Handle tool results
@@ -442,7 +445,10 @@ export class Anthropic extends ToolCallLLM<
       raw: response,
       message: {
         content: response.content
-          .filter((content): content is TextBlock => content.type === "text")
+          .filter(
+            (content): content is TextBlock =>
+              content.type === "text" && content.text?.trim().length > 0,
+          )
           .map((content) => ({
             type: "text" as const,
             text: content.text,
-- 
GitLab