From 224d507ab584a61516405757c6954825eb74e28c Mon Sep 17 00:00:00 2001
From: Parham Saidi <parham@parha.me>
Date: Fri, 26 Jul 2024 18:55:22 +0200
Subject: [PATCH] fix: prevent anthropic tool calling getting mixed with
 conversation (#1078)

---
 .changeset/hot-cats-nail.md                               | 5 +++++
 packages/community/src/llm/bedrock/providers/anthropic.ts | 2 +-
 packages/community/src/llm/bedrock/utils.ts               | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 .changeset/hot-cats-nail.md

diff --git a/.changeset/hot-cats-nail.md b/.changeset/hot-cats-nail.md
new file mode 100644
index 000000000..52b035e33
--- /dev/null
+++ b/.changeset/hot-cats-nail.md
@@ -0,0 +1,5 @@
+---
+"@llamaindex/community": patch
+---
+
+fix: prevent tool calling getting mixed with conversation
diff --git a/packages/community/src/llm/bedrock/providers/anthropic.ts b/packages/community/src/llm/bedrock/providers/anthropic.ts
index 9dadf782f..b59f48d2a 100644
--- a/packages/community/src/llm/bedrock/providers/anthropic.ts
+++ b/packages/community/src/llm/bedrock/providers/anthropic.ts
@@ -118,7 +118,7 @@ export class AnthropicProvider extends Provider<AnthropicStreamEvent> {
       if (!delta && !options) continue;
 
       yield {
-        delta,
+        delta: options ? "" : delta,
         options,
         raw: response,
       };
diff --git a/packages/community/src/llm/bedrock/utils.ts b/packages/community/src/llm/bedrock/utils.ts
index 217c5a4d5..4c79f994c 100644
--- a/packages/community/src/llm/bedrock/utils.ts
+++ b/packages/community/src/llm/bedrock/utils.ts
@@ -144,6 +144,8 @@ export const mapChatMessagesToAnthropicMessages = <
       const content = message.content[0];
       if (content.type === "text" && !content.text) return false;
       if (content.type === "image" && !content.source.data) return false;
+      if (content.type === "image" && message.role === "assistant")
+        return false;
       return true;
     });
 
-- 
GitLab