Skip to content

fix(responses): round-trip an Anthropic server tool search through the Responses bridge - #50107

Closed
homenshum-cheiron wants to merge 3 commits into
BerriAI:mainfrom
homenshum-cheiron:fix/responses-anthropic-tool-search-replay
Closed

homenshum-cheiron wants to merge 3 commits into
BerriAI:mainfrom
homenshum-cheiron:fix/responses-anthropic-tool-search-replay

Conversation

@homenshum-cheiron

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

<make-me-human-readable>
When Claude searches for tools on the server during an agent run, the Responses endpoint now returns the search result and sends the full search record back to Claude on the next turn. Before, the record was dropped, so on later turns Claude saw a call to a tool with no record of the search that loaded it.
</make-me-human-readable>

Details

TLDR

Problem this solves:

  • Claude's server tool search result is missing from Responses replies
  • Replayed history drops the search, keeping only the found tool's call
  • Later turns send Claude a deferred tool_use with no search record

How it solves it:

  • Each server search gets a tool_search_output item naming the found tools
  • A replayed answered search rebuilds server_tool_use, tool_search_tool_result, tool_use in order
  • Replay happens only beside an outbound Anthropic tool search tool
  • previous_response_id history follows the same rule

User Flow

Before: on turn 2 of an Agents SDK chat on Claude, the search record is gone

  1. The app sends POST https://litellm-domain/v1/responses with deferred function tools and {"type": "tool_search"}, model claude-opus-5 (Bedrock Invoke)
  2. The reply holds a tool_search_call and a function_call, but no item with the search result
  3. On turn 2 the app replays its history; the request that reaches Bedrock carries the tool_use alone, with no server_tool_use or tool_search_tool_result

After: turn 2 reaches Claude with the whole search

  1. The app sends the same POST
  2. The reply holds tool_search_call, tool_search_output (the found tool's definition) and function_call
  3. On turn 2 the Bedrock request carries server_tool_use, tool_search_tool_result and tool_use, in that order, in one assistant turn

Relevant issues

Part of #50104 (item 2). Related: #46444 fixed the other direction (GPT tool search through the chat bridge); #49132 checks tool_search_output items against allowed_tools, which now applies to the items this PR returns.

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

Shared setup: Agents SDK 0.24.0, OpenAIResponsesModel pointed at the proxy's /v1/responses, 6 deferred function tools plus ToolSearchTool(), two user turns. Proxy config as in #50104, with a callback that records each request body sent to Bedrock.

Before (fd4cef2 plus fixes for items 1 and 3 of #50104, without this PR)

  1. On fd4cef2 alone the run stops at call 1 (item 1 of [Bug]: Claude on Bedrock behind the OpenAI Agents SDK: deferred tool not found after tool search, search record lost on later turns, compaction param dropped on /v1/messages #50104), so this step used a build with the other two fixes
  2. Run the agent. The recorded later requests carry the tool_use alone: the assistant turns are [tool_use] and [tool_use, text], with no server_tool_use or tool_search_tool_result. This step ran against a local Bedrock stub

After (tested on a build of this PR plus the other two PRs for #50104)

  1. Run the same agent, live: 4 Bedrock calls, both user turns answered
  2. In the recorded bodies, calls 2 to 4 carry server_tool_use, tool_search_tool_result and tool_use in that order in the assistant turn
  3. The kept blocks add about 170 to 240 input tokens per later call

Focused checks on this PR's tip:

  • New in test_litellm_completion_responses.py: test_server_tool_search_result_becomes_a_tool_search_output_item, test_tool_search_output_holds_only_the_tools_the_search_named, test_tool_search_call_without_a_search_result_gets_no_output_item, test_replayed_server_tool_search_reaches_claude_before_the_tool_use, test_server_tool_search_is_not_replayed_to_a_provider_that_cannot_run_it, test_server_tool_search_call_without_its_output_is_not_replayed, test_server_tool_search_is_not_replayed_without_a_tool_search_tool[request_without_tool_search|model_without_tool_search].
  • New in test_streaming_iterator_transformation.py: test_anthropic_server_tool_search_result_streams_as_tool_search_output_item (sync and async).
  • New in test_anthropic_responses_bridge.py: test_previous_response_id_replays_a_stored_server_search_only_beside_a_tool_search_tool[request_without_tool_search|request_with_tool_search].
  • Replaced: test_tool_search_items_are_not_replayed_to_chat_messages, which asserted the drop.
  • Each new test fails on the commit before the change it covers.
  • uv run pytest tests/unit/responses/ tests/unit/llms/bedrock/messages/: 2158 passed. The only failures are test_reasoning_effort_grid_wire_shapes[*-vertex-ai] (needs the Google Cloud SDK) and test_every_openai_sdk_response_error_code_has_explicit_status_mapping; both fail the same way on main.

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • History stored as a chat.completion response (a Message object) skips the new rule
    • Its search result is rebuilt by the prompt factory, as on main

Low

  • Item order differs: streaming emits tool_search_output after the function_call
    • Replay pairs by call_id, so both orders replay the same way
  • Guardrails and token counting now see the replayed search as a tool call named tool_search_tool_regex, like a hosted web search replay
  • Compaction-replace history keeps orphan search results, which emit nothing, as on main
  • Not tested: several searches in one turn, a search error result, BM25 search
  • Whether keeping the record stops Claude from searching again is not measured: without the fix, one live run searched again on 3 of 4 later calls and another did not
  • The deferred tool namespace PR for [Bug]: Claude on Bedrock behind the OpenAI Agents SDK: deferred tool not found after tool search, search record lost on later turns, compaction param dropped on /v1/messages #50104 edits the same file; whichever merges second may need a rebase
  • The live After ran on a build with this PR and its two sibling PRs; a live run on this tip alone will follow in a comment

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

🤖 Generated with Claude Code

homenshum-cheiron and others added 3 commits November 4, 2026 13:01
…e Responses bridge

Bug: Claude runs a tool search on the server. Its reply holds a server_tool_use
block, a tool_search_tool_result block with tool_references, and the tool_use of
the tool it found. The bridge returned only a tool_search_call item and dropped
the result. On the next turn the client replays its history, the bridge dropped
the tool_search_call too, and Claude received only the tool_use of a deferred
tool with no record of the search that loaded it. What the model did next
varied: in one live run it searched again on 3 of 4 later calls, in another it
did not.

Fix:
- Reply side: next to each server tool_search_call, return a tool_search_output
  item (execution "server") whose tools are the request's tool definitions named
  in the tool_references. Namespaced tools keep their namespace and list only the
  named members. Streaming emits the same item as output_item.added and
  output_item.done events at the next dense output index.
- Request side: a replayed server tool_search_call that a tool_search_output
  answers becomes an assistant tool call with the search result in
  provider_specific_fields.tool_results. The Anthropic prompt factory already
  rebuilds server_tool_use, tool_search_tool_result and tool_use from that shape,
  in order, in one assistant turn.
- Unanswered search calls, client-executed searches and non-Anthropic routes keep
  today's behaviour: the call is dropped and nothing reaches the provider.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ool search tool

The replay rebuilt server_tool_use and tool_search_tool_result for every
Anthropic route. A turn can hold a server search in its history while the
request sends no tool search tool: the caller left it out (for example a
handoff to an agent without tool search), or the bridge dropped it because
the model cannot run tool search. That tool is what adds the tool-search
beta, so those turns sent the search blocks with no tool and no beta, where
main sent the tool_use alone.

The replay now needs an outbound Anthropic tool search tool as well as an
Anthropic route. Otherwise the search is dropped as before. The route check
still runs only when a replayed search is present.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…story too

The previous commit kept a replayed server search only beside an outbound
tool search tool, but only for the request's own input. History rebuilt from
previous_response_id is turned into messages later, in the session handler,
and kept every replayed server search on an Anthropic route. A later turn
without the tool (a handoff to an agent without tool search) still sent
server_tool_use and tool_search_tool_result with no tool and no beta.

Stored history now follows the same rule: without an outbound Anthropic tool
search tool, without_replayed_server_searches drops the stored search and
keeps the tool_use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mateo-berri

Copy link
Copy Markdown
Contributor

Closing this as superseded by #50821, which merged the Claude tool search replay for Responses follow-up turns. Thanks for the report and fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants