-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Emit native web_search_tool_result blocks for Anthropic clients (Claude Desktop / Cowork citations) #27886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit native web_search_tool_result blocks for Anthropic clients (Claude Desktop / Cowork citations) #27886
Changes from all commits
132de86
42101c5
1b7b6ed
a932111
de73c54
eb771ed
5df3d34
2e9bf34
136646e
11ff17f
20b0992
a3975bd
e07209e
da81476
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,11 +100,14 @@ def _detect_from_non_streaming_response( | |
| block_id = getattr(block, "id", None) | ||
| block_input = getattr(block, "input", {}) | ||
|
|
||
| # Check for LiteLLM standard or legacy web search tools | ||
| # Handles: litellm_web_search, WebSearch, web_search | ||
| # Detect tool_use blocks that came from interception. After | ||
| # pre-request conversion the model always sees | ||
| # ``litellm_web_search``; the bare ``web_search`` entry handles | ||
| # callers that bypass our pre-request hooks (e.g. direct | ||
| # litellm.acompletion). "WebSearch" is intentionally omitted — | ||
| # see is_web_search_tool for the Cowork rationale. | ||
| if block_type == "tool_use" and block_name in ( | ||
| LITELLM_WEB_SEARCH_TOOL_NAME, | ||
| "WebSearch", | ||
| "web_search", | ||
| ): | ||
| # Convert to dict for easier handling | ||
|
|
@@ -190,10 +193,12 @@ def _detect_from_openai_response( | |
| getattr(function, "arguments", None) if function else None | ||
| ) | ||
|
|
||
| # Check for LiteLLM standard or legacy web search tools | ||
| # Detect function-style web search tool_calls. ``WebSearch`` is | ||
| # intentionally omitted — see is_web_search_tool for the Cowork | ||
| # rationale (clients ship their own client-side ``WebSearch`` and | ||
| # we must not hijack it). | ||
| if tool_type == "function" and function_name in ( | ||
| LITELLM_WEB_SEARCH_TOOL_NAME, | ||
| "WebSearch", | ||
| "web_search", | ||
| ): | ||
| # Parse arguments (might be JSON string) | ||
|
|
@@ -350,6 +355,57 @@ def _transform_response_openai( | |
|
|
||
| return assistant_message, tool_messages | ||
|
|
||
| @staticmethod | ||
| def build_web_search_tool_result_block( | ||
| tool_use_id: str, | ||
| search_response: Optional[SearchResponse], | ||
| ) -> Dict[str, Any]: | ||
| """ | ||
| Build an Anthropic-native ``web_search_tool_result`` content block. | ||
|
|
||
| Native Anthropic clients (Claude Desktop, the Anthropic SDK, the | ||
| Anthropic Console) expect search-tool results to be returned as | ||
| structured ``web_search_tool_result`` blocks so that citations and | ||
| source links can be rendered. The agentic loop currently feeds the | ||
| model a flat text blob in the follow-up call (which is correct — the | ||
| model needs readable evidence). This helper produces the *additional* | ||
| block that should accompany the model's text reply when the original | ||
| request used a native ``web_search_*`` tool. | ||
|
|
||
| Spec reference: | ||
| https://docs.anthropic.com/en/api/web-search-tool | ||
|
|
||
| Args: | ||
| tool_use_id: The ``tool_use_id`` the model emitted on the first | ||
| turn. Must match exactly so the client can pair the result | ||
| with its tool_use block. | ||
| search_response: Structured ``SearchResponse`` from | ||
| ``litellm.asearch()``. If None or empty, the block is still | ||
| emitted with an empty result list (signals "search ran, no | ||
| results" rather than "search did not run"). | ||
| """ | ||
| items: List[Dict[str, Any]] = [] | ||
| if search_response is not None: | ||
| results = getattr(search_response, "results", None) or [] | ||
| for r in results: | ||
| url = getattr(r, "url", "") or "" | ||
| title = getattr(r, "title", "") or "" | ||
| page_age = getattr(r, "date", None) or getattr(r, "last_updated", None) | ||
| items.append( | ||
| { | ||
| "type": "web_search_result", | ||
| "url": url, | ||
| "title": title, | ||
| "page_age": page_age, | ||
| "encrypted_content": "", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Null
|
||
| } | ||
| ) | ||
|
Comment on lines
+393
to
+402
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a search result carries no date, |
||
| return { | ||
| "type": "web_search_tool_result", | ||
| "tool_use_id": tool_use_id, | ||
| "content": items, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def format_search_response(result: SearchResponse) -> str: | ||
| """ | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.