Repository navigation
fix(anthropic): keep tool_search_tool_result so the search call replays with its result - #46440
Conversation
…ys with its result Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
@litereview code-review |
|
bugbot run |
|
@litereview code-review |
Review already requestedcode-review is already reviewing |
✅ code-review5/5 · Ready for human reviewScope: full · Full PR review. Completed · Some checks not run. No correctness findings. Checks and execution details
View review · Reviewed |
Merging this PR will not alter performance
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…l_search_result_roundtrip Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> # Conflicts: # tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py
|
@litereview code-review |
|
bugbot run |
…with its server result
…s://github.com/BerriAI/litellm into litellm_anthropic_tool_search_result_roundtrip # Conflicts: # tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py
|
@litereview code-review |
|
@litereview code-review |
…client-answered marks, and replay it from run_tool_loop modify_params no longer adds a placeholder answer for a server tool call whose result is kept, unless the call ends the request. The cache control hook counts a mark on a server tool call the client answered, since that call goes out as a plain tool_use. run_tool_loop sends provider_specific_fields back and runs only the client's tool calls.
|
@litereview code-review |
1 similar comment
|
@litereview code-review |
…llows, scope run_tool_loop's replay, and keep code_interpreter_results off tool search turns
|
@litereview code-review |
|
bugbot run |
|
@litellm-stampbot |
litellm-stampbot: risk highNot approved at Rules (
Judge (gpt-6-luna): high, default behavior change, request hot path, complex diff
Pushes do not re-run this check. Comment |
…ay, cache marks, pass-through logging and chaos
|
Automatic reviews are disabled for this repository. |
|
@litereview code-review |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit cd4213d. Configure here.
…earch_items Resolves tool_loop.py against #46440 so provider_specific_fields replay for an Anthropic server tool call and for a turn that carries tool search items, and restores seven bridge unit tests that the previous main merge dropped
TLDR
Problem this solves:
tool_search_tool_resultblockHow it solves it:
provider_specific_fields["tool_results"], both modes, with nocode_interpreter_resultsfor itserver_tool_usemodify_paramsplaceholder) is droppedrun_tool_loopreplaysprovider_specific_fieldsonly for a turn with a server tool call, and runs only client tool callstool_useDecision: when the client's answer to a server tool call is the only message after that turn, it wins and the call goes back as
tool_use. Keeping the server result would end the request on an assistant turn, which Opus 5.5 rejects with a 400User Flow
Before: a developer running a tool-search agent loop on Claude through the SDK gets a 400 on the second call
litellm.completion()orRouter.completion()onanthropic/claude-opus-5-5withtool_search_tool_bm25_20251119plusdefer_loading: truefunction tools, stream or notget_weatherand calls it. The returned message has thesrvtoolu_call but no search resultget_weathertool resulttool_use ids were found without tool_result blocks immediately after: srvtoolu_...After: the same loop finishes
tool_search_tool_resultblock inprovider_specific_fields.tool_resultsget_weathertool resultLinear ticket
Resolves LIT-9576
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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 moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
The customer calls the SDK, so four cases use it and two go through a 2-worker proxy (no DB). Each is a two-turn loop on
anthropic/claude-opus-5-5against the real Anthropic API, with tool search and twodefer_loadingtools. Turn 2 sends back the assistant message as returned, plus atoolanswer forget_weather. Eight more cases cover the other rows of the tablesdk_loop.py (the four SDK cases)
proxy_nonstream.sh, proxy_stream.py, and the proxy config
regression_loops.py (the regression cases)
Commands each leg ran, against its own proxy on a random port
completion()andRouter.completion(), both modeslitellm.modify_params = Truemodify_params, no tool answers sentget_weatherread as interruptedweb_searchcallmodify_paramswithweb_searchcache_controlon the search calllitellm.run_tool_loopget_weatherBefore (457b5d2)
The six main cases get the User Flow's 400 on turn 2, and turn 1's
tool_resultsis[]. All fourweb_searchcases getunexpected tool_use_id found in tool_result blocks. Withmodify_paramsand no tool answers, Claude says both calls were interruptedRaw before output, all fourteen cases
After (072aa83)
Every case answers on turn 2,
tool_resultsholds the search result, andcode_interpreter_resultsis absent. Theweb_searchcases answer from the real results, and withmodify_paramsand no tool answers onlyget_weatherreads as interruptedRaw after output, all fourteen cases
Proxy QA, 2 workers with the worker pid per turn
A second run on a real proxy started from each worktree with
--num_workers 2,main(9271edc) on :4001 and this PR (072aa83) on :4000, against the real Anthropic API onanthropic/claude-opus-5-5. ACustomLoggerwritesos.getpid()on every upstream call, so each case prints which worker served turn 1 and turn 2, and a case reruns until turn 2 lands on the other worker (the retries are kept in the output). It covers the tool search loop and a plainget_weatherloop with no tool search, stream and non-stream. Turn 2 sends back the turn 1 assistant message as returned plus atoolanswer for everytoolu_callOn
mainboth tool search cases get the 400 and turn 1'stool_resultsis[]. On this PR both answer, and the plain loop answers on both, so ordinary proxy users see no change. Claude Code was not driven here because it calls/v1/messages, which goes throughlitellm/llms/anthropic/pass_through/messages/and never reaches the changed parser oranthropic_messages_ptconfig.yaml, pid_logger.py and qa.sh
Raw output, main (before) then this PR (after)
Type
🐛 Bug Fix
✅ Test
Caveats (if any)
Medium
provider_specific_fieldsfrom turn 1 still gets the 400contentandtool_callsalone, as before this PRLow
modify_params, a request ending on an assistant turn with only server tool calls still gets the placeholder, and the cache control hook misses a mark on those callsfind_anthropic_server_tool_result, so the second to merge fixes a smallfactory.pyconflictTests
test_anthropic_tool_search_result_wire.pyruns the loop through the proxy against a scripted upstream, stream and non-stream, and checks that the second request hasserver_tool_use, the exacttool_search_tool_result, thentool_use. Both cases fail on457b5d26c723 of 23 mutations fail the unit tests
Unit tests and mutations
The unit tests replace
test_tool_search_tool_result_not_in_tool_results, which pinned the bug. A round trip test covers tool search andweb_searchwith a client that answers only its own call, also answers the server call, or usesmodify_params, and the kept result must go back right after its call. A replay test covers a search that found nothing, where the client's answer stays unless the user writes again, and each case must answer everytool_useand end on a user turn. A parse test checks that a tool search turn getscode_interpreter_resultsonly when it also ran code. The cache control hook test counts a mark only on a server call that goes out astool_use, and therun_tool_looptests check that a search call is replayed and never executed, and that a turn with no server call goes back withoutprovider_specific_fieldsMutations: Restore either skip, drop
tool_resultsfromfind_anthropic_server_tool_result, put the result before its call, route the block intoweb_search_resultsin either mode, rebuild the server result even when the client answered, never rebuild it, never or always drop a client answer to a server call, count only tool rows as a later user turn, keep the dropped tool message, make the hook ignore dropped answers or client answers in either place, setcode_interpreter_resultson a search result or never set it in either mode, drop or always sendprovider_specific_fieldsinrun_tool_loop's replay, run server calls through the executor, or send a search-only turn back inarun_tool_loopFinal Attestation
Note
Medium Risk
Changes core Anthropic message transformation, streaming parsing, and tool-loop replay logic across many providers; incorrect edge-case handling could still produce invalid upstream message shapes or miscount cache breakpoints.
Overview
Fixes multi-turn Claude tool search loops that previously 400'd because
tool_search_tool_resultwas dropped and thesrvtoolu_call was replayed without its matching result.Response path: Streaming and non-streaming Anthropic parsing now retain
tool_search_tool_resultinprovider_specific_fields.tool_results(without treating it as code execution).code_interpreter_resultsis only built when non–tool-search tool results exist.Replay path: Message conversion tracks which tool IDs the client actually answered vs. cases where a kept server result should replace the client's tool message. Replays emit
server_tool_useplus the stored result immediately after the call; redundant client answers are skipped when a later user/tool turn follows. If the client's answer is the only follow-up, the call stays plaintool_useso the request does not end on an assistant turn.Tool loop & cache:
run_tool_loop/arun_tool_loopexecute only client tool calls, optionally preservingprovider_specific_fieldswhen server tools are replayed. The Anthropic cache-control hook counts breakpoints on server tool calls only when they are not client-answered and will be rebuilt as server tool use.Large integration/unit test coverage was added for proxy providers, chaos, SDK, passthrough, and cache marks.
Reviewed by Cursor Bugbot for commit cd4213d. Bugbot is set up for automated code reviews on this repo. Configure here.
Video walkthrough (/rush, live two-worker proxy curl plus SDK Router, real Anthropic) https://berriaillm.slack.com/files/U0ATSFX0VK2/F0C9E7AA1PT/pr46440_tool_search_tutorial.mp4