fix(bedrock): stop naming typed tools on Bedrock invoke /v1/messages so toolset entries reach Claude

A computer_toolset or browser_toolset entry carries no name, and Bedrock
refuses any name on one, so the placeholder name LiteLLM added for nameless
tools turned every toolset request into a 400. Only custom tools get the
placeholder now. Typed tools pass through as the caller sent them.
This commit is contained in:
mateo-berri 2026-10-30 03:04:07 -07:00
parent 4c8b90945f
commit a5bee2645d
2 changed files with 34 additions and 4 deletions

View file

@ -683,17 +683,19 @@ def normalize_tool_input_schema_types_for_bedrock_invoke(request_body: dict) ->
def ensure_bedrock_anthropic_messages_tool_names(request_body: dict) -> None:
"""
Bedrock Invoke (Anthropic Messages) requires each tool to include ``name``.
Bedrock Invoke (Anthropic Messages) requires each custom tool to include ``name``.
Some clients send only ``input_schema``; Bedrock then errors with
``tools.0.custom.name: Field required``.
In-place: set ``name`` to ``litellm_unnamed_tool_{index}`` when missing or blank.
In-place: set ``name`` to ``litellm_unnamed_tool_{index}`` on a custom tool whose name
is missing or blank. Typed tools pass through as sent, since a toolset entry refuses
any name and a dated tool accepts only its fixed one.
"""
tools: Final = request_body.get("tools")
if not tools or not isinstance(tools, list):
return
for i, tool in enumerate(tools):
if not isinstance(tool, dict):
for i, tool in enumerate(_TOOL_ITEMS_ADAPTER.validate_python(tools)):
if not _is_str_keyed_dict(tool) or tool.get("type") not in (None, "custom"):
continue
name = tool.get("name")
if name is None or (isinstance(name, str) and not name.strip()):

View file

@ -706,6 +706,34 @@ def test_bedrock_invoke_messages_transform_adds_name_when_tool_missing_name():
assert result["tools"][0]["name"] == "litellm_unnamed_tool_0"
def test_bedrock_invoke_messages_transform_names_only_custom_tools():
"""Bedrock refuses any name on a toolset entry, so only the unnamed custom tool gets a placeholder."""
from litellm.types.router import GenericLiteLLMParams
cfg = AmazonAnthropicClaudeMessagesConfig()
result = cfg.transform_anthropic_messages_request(
model="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
messages=[{"role": "user", "content": "Take a screenshot"}],
anthropic_messages_optional_request_params={
"max_tokens": 128,
"tools": [
{"type": "computer_toolset_20260801"},
{"type": "browser_toolset_20260801"},
{"type": "custom", "input_schema": {"type": "object", "properties": {}}},
{"input_schema": {"type": "object", "properties": {}}},
],
},
litellm_params=GenericLiteLLMParams(),
headers={},
)
assert result["tools"] == [
{"type": "computer_toolset_20260801"},
{"type": "browser_toolset_20260801"},
{"type": "custom", "input_schema": {"type": "object", "properties": {}}, "name": "litellm_unnamed_tool_2"},
{"input_schema": {"type": "object", "properties": {}}, "name": "litellm_unnamed_tool_3"},
]
def test_bedrock_invoke_messages_skips_thinking_injection_when_already_enabled(
local_model_cost_map,
):