From 5f398071b2837857c95126d93474c36705602df5 Mon Sep 17 00:00:00 2001 From: Agus Zubiaga Date: Wed, 19 Mar 2025 12:54:57 -0300 Subject: [PATCH] assistant2: Skip tool uses without a matching tool result (#27082) Anthropic API doesn't allow `tool_use` messages without a corresponding `tool_result`, so we'll skip those when building a request. I'll separately investigate why we are sending request before the tool result as that might lead to separate issues, but that might take a while and this is currently very frustrating. Release Notes: - N/A --- crates/assistant2/src/tool_use.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/assistant2/src/tool_use.rs b/crates/assistant2/src/tool_use.rs index 2045aa9b7361f96743537be8f3c90179a548a042..e0d03edd91af0ff2d51279b7d5e5a2f6e8247232 100644 --- a/crates/assistant2/src/tool_use.rs +++ b/crates/assistant2/src/tool_use.rs @@ -286,9 +286,17 @@ impl ToolUseState { ) { if let Some(tool_uses) = self.tool_uses_by_assistant_message.get(&message_id) { for tool_use in tool_uses { - request_message - .content - .push(MessageContent::ToolUse(tool_use.clone())); + if self.tool_results.contains_key(&tool_use.id) { + // Do not send tool uses until they are completed + request_message + .content + .push(MessageContent::ToolUse(tool_use.clone())); + } else { + log::debug!( + "skipped tool use {:?} because it is still pending", + tool_use + ); + } } } }