fix(anthropic): map tool search only beside a deferred tool, guard non-string tool types, and honor additional_drop_params tools

This commit is contained in:
mateo-berri 2026-10-30 03:53:49 -07:00
parent b2b965f8d0
commit 6ae64adfd5
8 changed files with 84 additions and 34 deletions

View file

@ -24,6 +24,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (
prompt_cache_breakpoint_for_wire,
with_prompt_cache_breakpoint,
)
from litellm.litellm_core_utils.tool_fields import drop_policy
from litellm.responses.utils import auto_reasoning_summary
from litellm.types.integrations.custom_logger import CONVERTED_STREAM_KEYS
from litellm.types.llms.anthropic import (
@ -316,7 +317,7 @@ def _build_responses_kwargs(
request_data["thinking"] = thinking
if tool_choice:
request_data["tool_choice"] = tool_choice
if tools:
if tools and "tools" not in drop_policy(forwarded_kwargs).additional_drop_params:
request_data["tools"] = tools
if top_p is not None:
request_data["top_p"] = top_p

View file

@ -232,7 +232,21 @@ def _custom_function(name: str, tool: Mapping[str, object]) -> ResponsesTool:
def is_tool_search(tool: Mapping[str, object]) -> bool:
return tool.get("type") in ANTHROPIC_TOOL_SEARCH_TOOL_TYPES
tool_type: Final = tool.get("type")
return isinstance(tool_type, str) and tool_type in ANTHROPIC_TOOL_SEARCH_TOOL_TYPES
def _defers_a_function(tool: Mapping[str, object]) -> bool:
translated: Final = _translated_tool(tool, False) if tool.get("defer_loading") is True else None
return isinstance(translated, ResponsesTool) and translated.payload.get("type") == "function"
def keeps_tool_search(tools: Sequence[Mapping[str, object]], *, maps_tool_search: bool) -> bool:
return (
maps_tool_search
and any(is_tool_search(tool) for tool in tools)
and any(_defers_a_function(tool) for tool in tools)
)
def _with_deferral(translated: TranslatedTool, defer_loading: object) -> TranslatedTool:

View file

@ -30,7 +30,7 @@ from litellm.llms.anthropic.messages.responses_adapters.tool_translation import
ResponsesTool,
TranslatedTool,
UntranslatableTool,
is_tool_search,
keeps_tool_search,
log_dropped_tools,
responses_tool,
unforceable_tool_choice_error,
@ -472,7 +472,7 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
drop_params: bool = True,
maps_tool_search: bool = False,
) -> tuple[TranslatedTool, ...]:
runs_tool_search: Final = maps_tool_search and any(is_tool_search(tool) for tool in tools)
runs_tool_search: Final = keeps_tool_search(tools, maps_tool_search=maps_tool_search)
translated: Final = tuple(responses_tool(tool, runs_tool_search=runs_tool_search) for tool in tools)
untranslatable: Final = tuple(tool for tool in translated if isinstance(tool, UntranslatableTool))
if untranslatable and not drop_params:

View file

@ -148,7 +148,6 @@ class OpenAITokenCounter(BaseTokenCounter):
messages_format: TokenCountMessagesFormat,
output_format: JsonValue,
api_base: str | None,
drop_params: bool,
) -> ResponsesCountRequestParts:
match messages_format:
case "anthropic":
@ -159,7 +158,6 @@ class OpenAITokenCounter(BaseTokenCounter):
tools=tools,
output_format=output_format,
maps_tool_search=openai_runs_tool_search(model_to_use, api_base),
drop_params=drop_params,
)
case "openai":
input_items, instructions = OpenAICountTokensConfig.messages_to_responses_input(
@ -239,7 +237,6 @@ class OpenAITokenCounter(BaseTokenCounter):
messages_format=messages_format,
output_format=output_format,
api_base=resolved.api_base,
drop_params=deployment_drops_params(deployment),
)
# If no input items were produced (e.g., system-only messages), fall back to local counting

View file

@ -20,11 +20,7 @@ from litellm.llms.anthropic.messages.native.utils import normalize_native_messag
from litellm.llms.anthropic.messages.responses_adapters.transformation import (
LiteLLMAnthropicToResponsesAPIAdapter,
)
from litellm.llms.openai.responses.transformation import keeps_anthropic_tool_search
from litellm.responses.litellm_completion_transformation.tool_search import (
tools_with_anthropic_tool_search_mapped,
tools_without_anthropic_tool_search,
)
from litellm.responses.litellm_completion_transformation.tool_search import tools_with_anthropic_tool_search_mapped
from litellm.responses.utils import ResponsesAPIRequestUtils
from litellm.types.llms.anthropic import AnthropicMessagesRequest, AnthropicOutputSchema
from litellm.types.llms.openai import ResponsesAPIRequestParams, replayable_responses_input
@ -165,14 +161,6 @@ def chat_content_blocks_to_responses_content(
return "\n".join(part["text"] for part in parts if part["type"] == "input_text")
def _tools_inference_sends(
model: str, tools: Sequence[Mapping[str, object]], *, drop_params: bool
) -> Sequence[Mapping[str, object]] | None:
if keeps_anthropic_tool_search(model, tools, drop_params=drop_params):
return tools_with_anthropic_tool_search_mapped(tools)
return tools_without_anthropic_tool_search(tools)
class OpenAICountTokensConfig:
"""
Configuration and transformation logic for OpenAI Responses API token counting.
@ -357,7 +345,6 @@ class OpenAICountTokensConfig:
tools: Sequence[Mapping[str, object]] | None,
output_format: JsonValue = None,
maps_tool_search: bool = False,
drop_params: bool = False,
) -> ResponsesCountRequestParts:
"""Convert with the adapter /v1/messages inference uses, so the count sees the same input it bills."""
anthropic_request: Final = AnthropicMessagesRequest(
@ -379,9 +366,7 @@ class OpenAICountTokensConfig:
translated.get("text"),
)
counted_tools: Final = (
_tools_inference_sends(model, translated_tools, drop_params=drop_params)
if maps_tool_search and translated_tools is not None
else translated_tools
tools_with_anthropic_tool_search_mapped(translated_tools) if maps_tool_search else translated_tools
)
return input_items, instructions, None if counted_tools is None else tuple(counted_tools), text

View file

@ -158,17 +158,12 @@ def _tool_deferral(tool: object) -> _ToolDeferral:
return _ToolDeferral()
def _runs_tool_search(model: str, tools: Sequence[object]) -> bool:
def _runs_tool_search(model: str, tools: Sequence[ALL_RESPONSES_API_TOOL_PARAMS]) -> bool:
return any(_tool_deferral(tool).defers for tool in tools) and litellm.supports_tool_search(
model=model, custom_llm_provider=LlmProviders.OPENAI.value
)
def keeps_anthropic_tool_search(model: str, tools: Sequence[object], *, drop_params: bool) -> bool:
callers_own_search: Final = any(is_openai_tool_search_tool(tool) for tool in tools)
return callers_own_search or not drop_params or _runs_tool_search(model, tools)
def _without_defer_loading(tool: ALL_RESPONSES_API_TOOL_PARAMS) -> ALL_RESPONSES_API_TOOL_PARAMS:
deferral: Final = _tool_deferral(tool)
if not deferral.defers:
@ -733,7 +728,8 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig):
) -> Sequence[ALL_RESPONSES_API_TOOL_PARAMS] | None:
if tools is None or not any(is_anthropic_tool_search_chat_tool(tool) for tool in tools):
return tools
if keeps_anthropic_tool_search(model, tools, drop_params=drops_params(litellm_params)):
callers_own_search: Final = any(is_openai_tool_search_tool(tool) for tool in tools)
if callers_own_search or not drops_params(litellm_params) or _runs_tool_search(model, tools):
return tools_with_anthropic_tool_search_mapped(tools)
verbose_logger.warning(
"Dropping tool search under drop_params: OpenAI model '%s' cannot run it or no tool is deferred, so every "

View file

@ -889,6 +889,56 @@ async def test_drop_params_drops_tool_search_and_its_deferral_on_an_openai_model
]
UNDEFERRED_WEATHER: Final = {key: value for key, value in DEFERRED_WEATHER.items() if key != "defer_loading"}
@pytest.mark.asyncio
@pytest.mark.parametrize(
"weather",
[
pytest.param(UNDEFERRED_WEATHER, id="no-defer-loading"),
pytest.param({**DEFERRED_WEATHER, "defer_loading": "true"}, id="defer-loading-string"),
],
)
async def test_tool_search_with_no_deferred_tool_answers_400_naming_drop_params_before_any_openai_call(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, weather: Mapping[str, object]
):
with pytest.raises(litellm.UnsupportedParamsError) as error:
await _openai_request_for(respx_mock, monkeypatch, [TOOL_SEARCH, weather], drop_params=False)
assert error.value.status_code == 400
assert "tool_search_tool_bm25_20251119" in error.value.message
assert "drop_params" in error.value.message
assert not respx_mock.calls
@pytest.mark.asyncio
@pytest.mark.parametrize("tool_type", [pytest.param(["bash"], id="list"), pytest.param({"name": "bash"}, id="object")])
async def test_a_tool_whose_type_is_not_a_string_reaches_openai_as_a_function_beside_tool_search(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, tool_type: object
):
upstream: Final = await _openai_request_for(
respx_mock, monkeypatch, [TOOL_SEARCH, DEFERRED_WEATHER, {"type": tool_type, "name": "odd_tool"}]
)
assert [(tool["type"], tool.get("name")) for tool in _tools_sent(upstream)] == [
("tool_search", None),
("function", "get_weather"),
("function", "odd_tool"),
]
@pytest.mark.asyncio
async def test_additional_drop_params_tools_sends_no_tools_even_beside_one_openai_cannot_run(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
):
upstream: Final = await _openai_request_for(
respx_mock, monkeypatch, [WEB_FETCH, TYPED_BASH], drop_params=False, additional_drop_params=["tools"]
)
assert "tools" not in json.loads(upstream.calls.last.request.content)
@pytest.mark.asyncio
async def test_a_tool_openai_cannot_run_answers_400_before_any_openai_call_without_drop_params(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch

View file

@ -255,10 +255,17 @@ async def test_anthropic_tool_search_is_counted_as_the_openai_tool_search_infere
pytest.param(UNDEFERRED_TOOL_SEARCH_TOOLS, [("function", "get_weather", None)], id="nothing-is-deferred"),
],
)
async def test_drop_params_counts_anthropic_tool_search_only_where_inference_keeps_it(
monkeypatch: pytest.MonkeyPatch, tools: list[dict[str, object]], expected: list[tuple[object, object, object]]
@pytest.mark.parametrize(
"litellm_params",
[pytest.param({}, id="without-drop-params"), pytest.param({"drop_params": True}, id="drop-params")],
)
async def test_anthropic_tool_search_is_counted_only_beside_a_deferred_tool(
monkeypatch: pytest.MonkeyPatch,
tools: list[dict[str, object]],
expected: list[tuple[object, object, object]],
litellm_params: Mapping[str, object],
) -> None:
assert await _counted_tools(monkeypatch, "gpt-5.6-luna", {"drop_params": True}, tools) == expected
assert await _counted_tools(monkeypatch, "gpt-5.6-luna", litellm_params, tools) == expected
@pytest.mark.asyncio