Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(anthropic): make WebSearch tolerate pause_turn and return every search's results (#7421)

Wolf Mermelstein3d14d2c1b4a1
diff --git a/crates/anthropic/src/openai/stream_response.rs b/crates/anthropic/src/openai/stream_response.rs
index 681cc33da5..2053c45d48 100644
--- a/crates/anthropic/src/openai/stream_response.rs
+++ b/crates/anthropic/src/openai/stream_response.rs
@@ -105,8 +105,10 @@ fn map_stop_reason(stop_reason: StopReason) -> FinishReason {
StopReason::MaxTokens => FinishReason::Length,
StopReason::StopSequence => FinishReason::Stop,
StopReason::ToolUse => FinishReason::ToolCalls,
- StopReason::PausTurn => FinishReason::Stop,
+ StopReason::PauseTurn => FinishReason::Stop,
StopReason::Refusal => FinishReason::ContentFilter,
+ StopReason::ModelContextWindowExceeded => FinishReason::Length,
+ StopReason::Unknown => FinishReason::Stop,
}
}
diff --git a/crates/anthropic/src/toolset/test.rs b/crates/anthropic/src/toolset/test.rs
index be4653f3c5..bffb38a0d7 100644
--- a/crates/anthropic/src/toolset/test.rs
+++ b/crates/anthropic/src/toolset/test.rs
@@ -1,4 +1,5 @@
use super::*;
+use crate::types::response::web_search::{SearchResult, WebSearchContent, WebSearchToolError};
use crate::types::response::{Content, MessageResponse, ResponseContentKind};
use ai_billing::domain::{
DenyReason,
@@ -462,6 +463,150 @@ fn deserialize_web_search_response() {
}
}
+fn search_result(url: &str) -> serde_json::Value {
+ serde_json::json!({
+ "type": "web_search_result",
+ "url": url,
+ "title": format!("Title of {url}"),
+ "encrypted_content": "EqgfCioIARgB...",
+ "page_age": "April 30, 2025"
+ })
+}
+
+fn search_result_block(tool_use_id: &str, content: serde_json::Value) -> serde_json::Value {
+ serde_json::json!({
+ "type": "web_search_tool_result",
+ "tool_use_id": tool_use_id,
+ "content": content
+ })
+}
+
+/// A paused server-tool loop is a real stop reason for web search, so the
+/// tool must hand back the results found so far rather than fail on the
+/// envelope; stop reasons this crate has never seen must not fail it either.
+#[tokio::test]
+async fn web_search_survives_paused_and_unknown_stop_reasons() {
+ use ai_toolset::{AsyncTool, ServiceContext};
+ for stop_reason in [
+ "pause_turn",
+ "model_context_window_exceeded",
+ "some_future_reason",
+ ] {
+ let body = serde_json::json!({
+ "id": "msg-1",
+ "type": "message",
+ "role": "assistant",
+ "content": [
+ {"type": "server_tool_use", "id": "srv-1", "name": "web_search", "input": {"query": "a"}},
+ search_result_block("srv-1", serde_json::json!([search_result("https://a.example")])),
+ {"type": "server_tool_use", "id": "srv-2", "name": "web_search", "input": {"query": "b"}},
+ ],
+ "stop_reason": stop_reason,
+ "stop_sequence": null,
+ "usage": {"input_tokens": 10, "output_tokens": 5}
+ });
+ let context = context_with_response(&body.to_string(), Arc::default()).await;
+ let request = RequestContext::new(
+ .to_owned()
+ .try_into()
+ .unwrap(),
+ );
+ let response = WebSearch {
+ input: "a and b".into(),
+ }
+ .call(ServiceContext(context), request)
+ .await
+ .unwrap_or_else(|error| panic!("{stop_reason}: {}", error.description));
+ assert_eq!(response.tool_use_id, "srv-1");
+ match response.content {
+ WebSearchContent::Results(results) => assert_eq!(results.len(), 1),
+ WebSearchContent::Error(error) => panic!("{stop_reason}: {error:?}"),
+ }
+ }
+}
+
+#[test]
+fn web_search_output_folds_every_search_without_encrypted_content() {
+ let blocks: Vec<ResponseContentKind> = serde_json::from_value(serde_json::json!([
+ {"type": "text", "text": "Searching."},
+ {"type": "server_tool_use", "id": "srv-1", "name": "web_search", "input": {"query": "a"}},
+ search_result_block("srv-1", serde_json::json!([
+ search_result("https://a.example"),
+ search_result("https://shared.example"),
+ ])),
+ search_result_block("srv-2", serde_json::json!({
+ "type": "web_search_tool_result_error",
+ "error_code": "too_many_requests"
+ })),
+ search_result_block("srv-3", serde_json::json!([
+ search_result("https://shared.example"),
+ search_result("https://c.example"),
+ ])),
+ ]))
+ .unwrap();
+
+ let response = web_search::collect_results(blocks).expect("a search ran");
+ assert_eq!(response.tool_use_id, "srv-1");
+ let WebSearchContent::Results(results) = response.content else {
+ panic!("one failed search must not hide the others' results");
+ };
+ let urls: Vec<_> = results
+ .iter()
+ .map(|result| match result {
+ SearchResult::WebSearchResult {
+ url,
+ encrypted_content,
+ ..
+ } => {
+ assert_eq!(encrypted_content, &None);
+ url.as_str()
+ }
+ })
+ .collect();
+ assert_eq!(
+ urls,
+ [
+ "https://a.example",
+ "https://shared.example",
+ "https://c.example"
+ ]
+ );
+}
+
+#[test]
+fn web_search_output_reports_the_error_only_when_nothing_was_found() {
+ let blocks: Vec<ResponseContentKind> = serde_json::from_value(serde_json::json!([
+ search_result_block(
+ "srv-1",
+ serde_json::json!({
+ "type": "web_search_tool_result_error",
+ "error_code": "max_uses_exceeded"
+ })
+ ),
+ search_result_block(
+ "srv-2",
+ serde_json::json!({
+ "type": "web_search_tool_result_error",
+ "error_code": "unavailable"
+ })
+ ),
+ ]))
+ .unwrap();
+ let response = web_search::collect_results(blocks).expect("a search ran");
+ assert_eq!(response.tool_use_id, "srv-1");
+ assert!(matches!(
+ response.content,
+ WebSearchContent::Error(WebSearchToolError { ref error_code, .. }) if error_code == "max_uses_exceeded"
+ ));
+
+ let no_search: Vec<ResponseContentKind> = serde_json::from_value(serde_json::json!([
+ {"type": "text", "text": "I do not need to search for that."}
+ ]))
+ .unwrap();
+ assert!(web_search::collect_results(no_search).is_none());
+}
+
#[test]
fn deserialize_web_search_error_response() {
// Error case from docs — content is a single object, not an array
diff --git a/crates/anthropic/src/toolset/web_search.rs b/crates/anthropic/src/toolset/web_search.rs
index bc0cb46895..45cb45f856 100644
--- a/crates/anthropic/src/toolset/web_search.rs
+++ b/crates/anthropic/src/toolset/web_search.rs
@@ -1,12 +1,13 @@
use super::AnthropicToolContext;
use crate::types::request::WEB_SEARCH_TOOL;
use crate::types::response::ResponseContentKind;
-use crate::types::response::web_search::WebSearchResponse;
+use crate::types::response::web_search::{SearchResult, WebSearchContent, WebSearchResponse};
use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};
use ai_toolset::{ToolAnnotated, ToolAnnotations};
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::Deserialize;
+use std::collections::HashSet;
/// Search the web using Claude's built-in web search.
#[derive(Deserialize, JsonSchema, Clone)]
@@ -40,15 +41,69 @@ impl AsyncTool<AnthropicToolContext> for WebSearch {
super::invoke_server_tool(ctx, &request_context, WEB_SEARCH_TOOL.clone(), &self.input)
.await?;
- blocks
- .into_iter()
- .find_map(|b| match b {
- ResponseContentKind::WebSearchToolResult(r) => Some(r),
- _ => None,
- })
- .ok_or_else(|| ToolCallError {
- internal_error: anyhow::anyhow!("no web search result in response"),
- description: "No web search result returned".into(),
- })
+ collect_results(blocks).ok_or_else(|| ToolCallError {
+ internal_error: anyhow::anyhow!("no web search result in response"),
+ description: "No web search result returned".into(),
+ })
+ }
+}
+
+/// Fold every `web_search_tool_result` block of one response into the tool's
+/// output.
+///
+/// The server-side model runs as many searches as the instruction needs, one
+/// result block each, so the output carries all of them (deduplicated by URL)
+/// under the first search's id rather than whichever block came first. A
+/// search error is only the outcome when no search returned results, and
+/// `None` means the model never searched.
+///
+/// `encrypted_content` is dropped: it is an opaque blob only the Anthropic
+/// API can read, for citing within the same conversation, which never happens
+/// here, and it is by far the largest part of every result.
+pub(crate) fn collect_results(blocks: Vec<ResponseContentKind>) -> Option<WebSearchResponse> {
+ let mut searched = None;
+ let mut results = Vec::new();
+ let mut seen_urls = HashSet::new();
+ let mut failed = None;
+ for block in blocks {
+ let ResponseContentKind::WebSearchToolResult(result) = block else {
+ continue;
+ };
+ match result.content {
+ WebSearchContent::Results(found) => {
+ searched.get_or_insert(result.tool_use_id);
+ for found in found {
+ let SearchResult::WebSearchResult {
+ title,
+ url,
+ page_age,
+ ..
+ } = found;
+ if !seen_urls.insert(url.clone()) {
+ continue;
+ }
+ results.push(SearchResult::WebSearchResult {
+ title,
+ url,
+ encrypted_content: None,
+ page_age,
+ });
+ }
+ }
+ WebSearchContent::Error(error) => {
+ failed.get_or_insert((result.tool_use_id, error));
+ }
+ }
+ }
+ match (searched, failed) {
+ (Some(tool_use_id), _) => Some(WebSearchResponse {
+ content: WebSearchContent::Results(results),
+ tool_use_id,
+ }),
+ (None, Some((tool_use_id, error))) => Some(WebSearchResponse {
+ content: WebSearchContent::Error(error),
+ tool_use_id,
+ }),
+ (None, None) => None,
}
}
diff --git a/crates/anthropic/src/types/response/types.rs b/crates/anthropic/src/types/response/types.rs
index 306b1b3de2..5d995b8eb6 100644
--- a/crates/anthropic/src/types/response/types.rs
+++ b/crates/anthropic/src/types/response/types.rs
@@ -60,6 +60,12 @@ pub struct TextResponse {
text: String,
}
+/// Why the model stopped generating.
+///
+/// Every variant the API documents is named so callers can match on it, and
+/// anything newer lands in [`StopReason::Unknown`]: a stop reason this crate
+/// has not heard of must not make the whole response unparseable, since the
+/// content it carries is still good.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum StopReason {
@@ -67,8 +73,16 @@ pub enum StopReason {
MaxTokens,
StopSequence,
ToolUse,
- PausTurn,
+ /// A server-tool loop (web search, web fetch, code execution) hit its
+ /// iteration limit. The content so far is complete up to that point;
+ /// sending it back continues the turn.
+ PauseTurn,
Refusal,
+ /// The response filled the model's context window; treat it as truncated.
+ ModelContextWindowExceeded,
+ /// A stop reason added to the API after this enum.
+ #[serde(other)]
+ Unknown,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]