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(proxy): enforce tool allowlist on OpenAI-format tools sent to /v1/messages
mateo-berriba7c268a6ab4
diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.pyindex 721a6653597..2afea1444f9 100644--- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py+++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py@@ -677,12 +677,9 @@ class AnthropicMessagesHandler(BaseTranslation):)def extract_request_tool_names(self, data: dict) -> list[str]:- """Extract tool names from Anthropic messages request (tools[].name)."""- names: Final[list[str]] = []- for tool in data.get("tools") or []:- if isinstance(tool, dict) and tool.get("name"):- names.append(str(tool["name"]))- return names+ """Extract tool names from Anthropic messages request (tools[].name, or+ tools[].function.name for OpenAI-format tools the bridge forwards verbatim)."""+ return [name for tool in data.get("tools") or [] if (name := anthropic_tool_name(tool))]@classmethoddef _extract_input_text_and_images(diff --git a/litellm/llms/base_llm/guardrail_translation/utils.py b/litellm/llms/base_llm/guardrail_translation/utils.pyindex 1546adbb0bd..4494f8451f4 100644--- a/litellm/llms/base_llm/guardrail_translation/utils.py+++ b/litellm/llms/base_llm/guardrail_translation/utils.py@@ -210,8 +210,16 @@ def openai_tool_name(tool: object) -> str | None:def anthropic_tool_name(tool: object) -> str | None:- name: Final = tool.get("name") if isinstance(tool, dict) else None- return name if isinstance(name, str) else None+ """Anthropic tools carry a flat ``name``; an OpenAI-format function tool, which the+ non-Anthropic bridge forwards verbatim, carries it under ``function.name`` instead."""+ if not isinstance(tool, dict):+ return None+ flat_name: Final = tool.get("name")+ if isinstance(flat_name, str):+ return flat_name+ function: Final = tool.get("function") if tool.get("type") == "function" else None+ function_name: Final = function.get("name") if isinstance(function, dict) else None+ return function_name if isinstance(function_name, str) else Nonedef merge_returned_tools_into_request_tools(diff --git a/tests/test_litellm/proxy/test_tools_allowlist_enforcement.py b/tests/test_litellm/proxy/test_tools_allowlist_enforcement.pyindex 31f5fbf606b..c881effada5 100644--- a/tests/test_litellm/proxy/test_tools_allowlist_enforcement.py+++ b/tests/test_litellm/proxy/test_tools_allowlist_enforcement.py@@ -93,6 +93,19 @@ class TestExtractRequestToolNames:"run_sql",]+ def test_anthropic_openai_format_tools_forwarded_by_bridge(self):+ data = {+ "tools": [+ {"type": "function", "function": {"name": "get_weather"}},+ {"name": "run_sql"},+ {"googleSearch": {}},+ ]+ }+ assert extract_request_tool_names("/v1/messages", data) == [+ "get_weather",+ "run_sql",+ ]+def test_generate_content_tools(self):data = {"tools": [@@ -159,6 +172,20 @@ class TestCheckToolsAllowlist:assert exc_info.value.type == ProxyErrorTypes.tool_access_deniedassert "get_weather" in str(exc_info.value.message)+ @pytest.mark.asyncio+ async def test_disallowed_openai_format_tool_raises_on_messages_route(self):+ token = _token(metadata={"allowed_tools": ["other_tool"]})+ body = {"tools": [{"type": "function", "function": {"name": "get_weather"}}]}+ with pytest.raises(ProxyException) as exc_info:+ await check_tools_allowlist(+ request_body=body,+ valid_token=token,+ team_object=None,+ route="/v1/messages",+ )+ assert exc_info.value.type == ProxyErrorTypes.tool_access_denied+ assert "get_weather" in str(exc_info.value.message)+@pytest.mark.asyncioasync def test_disallowed_custom_tool_raises_on_responses_route(self):token = _token(metadata={"allowed_tools": ["other_tool"]})