diff --git a/.changeset/four-crabs-design.md b/.changeset/four-crabs-design.md new file mode 100644 index 0000000000000000000000000000000000000000..bbfdd6af485dbc889a4e8eee656d77e06fbe3a25 --- /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 ca9747d9d1cb0d649d501ec72df169125e6ccb28..dd2f6414dddca5d75a4813a66872532e2c79ce90 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,