The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(bedrock): filter out `custom` field from tools to prevent 400 errors (#22861)
weiguang li3d027c0f7afd
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.pyindex dfab81123fd..e5698843e8f 100644--- a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py+++ b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py@@ -6,7 +6,10 @@ from litellm.llms.anthropic.chat.transformation import AnthropicConfigfrom litellm.llms.bedrock.chat.invoke_transformations.base_invoke_transformation import (AmazonInvokeConfig,)-from litellm.llms.bedrock.common_utils import get_anthropic_beta_from_headers+from litellm.llms.bedrock.common_utils import (+ get_anthropic_beta_from_headers,+ remove_custom_field_from_tools,+)from litellm.types.llms.anthropic import ANTHROPIC_TOOL_SEARCH_BETA_HEADERfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.utils import ModelResponse@@ -108,6 +111,12 @@ class AmazonAnthropicClaudeConfig(AmazonInvokeConfig, AnthropicConfig):if "anthropic_version" not in _anthropic_request:_anthropic_request["anthropic_version"] = self.anthropic_version+ # Remove `custom` field from tools (Bedrock doesn't support it)+ # Claude Code sends `custom: {defer_loading: true}` on tool definitions,+ # which causes Bedrock to reject the request with "Extra inputs are not permitted"+ # Ref: https://github.com/BerriAI/litellm/issues/22847+ remove_custom_field_from_tools(_anthropic_request)+tools = optional_params.get("tools")tool_search_used = self.is_tool_search_used(tools)programmatic_tool_calling_used = self.is_programmatic_tool_calling_used(tools)diff --git a/litellm/llms/bedrock/common_utils.py b/litellm/llms/bedrock/common_utils.pyindex b779c892c67..8e944988a95 100644--- a/litellm/llms/bedrock/common_utils.py+++ b/litellm/llms/bedrock/common_utils.py@@ -49,6 +49,27 @@ def get_cached_model_info():return _get_model_info+def remove_custom_field_from_tools(request_body: dict) -> None:+ """+ Remove ``custom`` field from each tool in the request body.++ Claude Code (v2.1.69+) sends ``custom: {defer_loading: true}`` on tool+ definitions, which Anthropic's API accepts but Bedrock rejects with+ ``"Extra inputs are not permitted"``.++ Args:+ request_body: The request dictionary to modify in-place.++ Ref: https://github.com/BerriAI/litellm/issues/22847+ """+ tools = request_body.get("tools")+ if not tools or not isinstance(tools, list):+ return+ for tool in tools:+ if isinstance(tool, dict):+ tool.pop("custom", None)++class AmazonBedrockGlobalConfig:def __init__(self):passdiff --git a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.pyindex f0aa643b345..9fae5fd2a17 100644--- a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py+++ b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py@@ -26,6 +26,7 @@ from litellm.llms.bedrock.chat.invoke_transformations.base_invoke_transformationfrom litellm.llms.bedrock.common_utils import (get_anthropic_beta_from_headers,is_claude_4_5_on_bedrock,+ remove_custom_field_from_tools,)from litellm.types.llms.anthropic import ANTHROPIC_TOOL_SEARCH_BETA_HEADERfrom litellm.types.llms.openai import AllMessageValues@@ -418,6 +419,12 @@ class AmazonAnthropicClaudeMessagesConfig(anthropic_messages_request=anthropic_messages_request,)+ # 5a. Remove `custom` field from tools (Bedrock doesn't support it)+ # Claude Code sends `custom: {defer_loading: true}` on tool definitions,+ # which causes Bedrock to reject the request with "Extra inputs are not permitted"+ # Ref: https://github.com/BerriAI/litellm/issues/22847+ remove_custom_field_from_tools(anthropic_messages_request)+# 6. AUTO-INJECT beta headers based on features usedanthropic_model_info = AnthropicModelInfo()tools = anthropic_messages_optional_request_params.get("tools")diff --git a/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py b/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.pyindex ee4c7828c33..e0b06ced172 100644--- a/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py+++ b/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py@@ -11,6 +11,7 @@ import pytestsys.path.insert(0, os.path.abspath("../../../../../.."))from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.llms.bedrock.common_utils import remove_custom_field_from_toolsfrom litellm.llms.bedrock.messages.invoke_transformations.anthropic_claude3_transformation import (AmazonAnthropicClaudeMessagesConfig,AmazonAnthropicClaudeMessagesStreamDecoder,@@ -180,6 +181,57 @@ def test_remove_ttl_from_cache_control():assert request5 == {}+def test_remove_custom_field_from_tools():+ """+ Ensure the `custom` field is stripped from every tool definition.++ Claude Code v2.1.69+ sends `custom: {defer_loading: true}` on tool+ objects. Bedrock does not accept this extra field and returns+ "Extra inputs are not permitted".++ Ref: https://github.com/BerriAI/litellm/issues/22847+ """++ # Case 1: tool with `custom` field should have it removed+ request = {+ "tools": [+ {+ "name": "Read",+ "description": "Read a file",+ "input_schema": {"type": "object", "properties": {}},+ "custom": {"defer_loading": True},+ },+ {+ "name": "Write",+ "description": "Write a file",+ "input_schema": {"type": "object", "properties": {}},+ },+ ]+ }++ remove_custom_field_from_tools(request)++ for tool in request["tools"]:+ assert "custom" not in tool, f"Tool {tool['name']} still has 'custom' field"+ # Other fields should be preserved+ assert request["tools"][0]["name"] == "Read"+ assert request["tools"][1]["name"] == "Write"++ # Case 2: request without tools key (should not raise error)+ request2 = {"messages": [{"role": "user", "content": "hi"}]}+ remove_custom_field_from_tools(request2)+ assert "tools" not in request2++ # Case 3: empty tools list (should not raise error)+ request3 = {"tools": []}+ remove_custom_field_from_tools(request3)+ assert request3["tools"] == []++ # Case 4: tools with None value (should not raise error)+ request4 = {"tools": None}+ remove_custom_field_from_tools(request4)+ assert request4["tools"] is None+def test_remove_scope_from_cache_control():"""Ensure scope field is removed from cache_control for Bedrock (not supported)."""