fix(guardrails): judge legacy functions entries like tools and send Gemini a namespace's members

This commit is contained in:
mateo-berri 2026-10-30 21:29:23 -07:00
parent 600547764d
commit d308905b72
4 changed files with 112 additions and 19 deletions

View file

@ -8,6 +8,7 @@ import time
from collections.abc import Awaitable, Callable, Mapping, Sequence
from copy import deepcopy
from functools import lru_cache, partial
from itertools import chain
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Final, Literal, Optional, Protocol, Union, cast, get_args
@ -510,6 +511,19 @@ def mapped_gemini_speech_config(
}
def _namespace_members_or_tool(tool: JsonValue) -> tuple[JsonValue, ...]:
if not isinstance(tool, dict) or tool.get("type") != "namespace":
return (tool,)
members: Final = tool.get("tools")
return tuple(members) if isinstance(members, list) else ()
def _with_namespace_members_inlined(tools: JsonValue) -> JsonValue:
if not isinstance(tools, list):
return tools
return list(chain.from_iterable(map(_namespace_members_or_tool, tools)))
class VertexAIBaseConfig:
def get_mapped_special_auth_params(self) -> dict:
"""
@ -1063,7 +1077,8 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
# remove 'strict' from tools
remove_strict_from_schema(schema_value)
for tool in cast(list[dict], schema_value): # cast-ok: a deep copy keeps the caller's tool shape
inlined_tools: Final = _with_namespace_members_inlined(schema_value)
for tool in cast(list[dict], inlined_tools): # cast-ok: a deep copy keeps the caller's tool shape
openai_function_object: ChatCompletionToolParamFunctionChunk | None = None
if "function" in tool: # tools list
_openai_function_object = ChatCompletionToolParamFunctionChunk(**tool["function"])

View file

@ -65,13 +65,27 @@ _FORCED_FUNCTION_CALLING_MODES: Final = frozenset({"ANY", "VALIDATED"})
_NAME_IDENTIFIED_TYPES: Final = frozenset({"function", "custom", "namespace", *_HOSTED_SERVER_NAME_FIELDS})
_TEXT_EDITOR_TOOL_NAMES: Final = frozenset({"str_replace_editor", "str_replace_based_edit_tool"})
_TYPE_VERSION_SUFFIX: Final = re.compile(r"_\d{8}$")
_GEMINI_HOSTED_TOOL_KEYS: Final = frozenset(tool.value.replace("_", "").lower() for tool in VertexToolName)
_GEMINI_TYPED_HOSTED_TOOLS: Final = ("computer_use", "web_search", "web_search_preview")
_NAMED_CHOICE_TYPES: Final = frozenset({"tool", "function", "custom", "allowed_tools", "any", "auto", "none"})
_DECODED_ARGUMENTS: Final = TypeAdapter(object)
_REQUEST_DATA: Final = TypeAdapter(object)
_ToolTarget: TypeAlias = tuple[str | None, str | None]
def _snake_case(name: str) -> str:
return re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower()
def _camel_case(name: str) -> str:
head, *rest = name.split("_")
return head + "".join(part.title() for part in rest)
_GEMINI_HOSTED_TOOL_KEYS: Final = frozenset(
chain.from_iterable((_snake_case(tool.value), _camel_case(tool.value)) for tool in VertexToolName)
)
def _name_identifies_tool(name: str, tool_type: str) -> bool:
stem: Final = _TYPE_VERSION_SUFFIX.sub("", tool_type)
return (
@ -128,10 +142,10 @@ def _namespace_function_targets(tool: object) -> tuple[_ToolTarget, ...]:
def _gemini_hosted_key_targets(tool: object) -> tuple[_ToolTarget, ...]:
fields: Final = _object_mapping(tool)
if fields is None:
if fields is None or fields.get("type") in _GEMINI_TYPED_HOSTED_TOOLS:
return ()
hosted_keys: Final = (key for key in fields if key.replace("_", "").lower() in _GEMINI_HOSTED_TOOL_KEYS)
return tuple(_part_target(fields, key) for key in hosted_keys)
keys: Final = tuple(key for key in fields if key != "type")
return (_part_target(fields, keys[0]),) if len(keys) == 1 and keys[0] in _GEMINI_HOSTED_TOOL_KEYS else ()
def _namespace_members(tool: object) -> Sequence[object]:
@ -242,6 +256,7 @@ def _extra_body_tools(data: object) -> Sequence[object] | None:
def _sent_tools(data: object) -> tuple[object, ...]:
return (
*(_request_list(data, "tools") or ()),
*(_request_list(data, "functions") or ()),
*(_extra_body_tools(data) or ()),
*_tools_in_items(_input_items(data)),
)
@ -991,9 +1006,6 @@ class ToolPermissionGuardrail(CustomGuardrail):
return (*named_targets, (None, normalized_type))
return ((None, tool_type),) if tool_type is not None else ()
def _get_legacy_function_name(self, function: object) -> str | None:
return self._get_mapping_value(function, "name")
def _get_named_tool_choice(self, tool_choice: object) -> str | None:
if not tool_choice or tool_choice in ("auto", "none", "required"):
return None
@ -1031,9 +1043,7 @@ class ToolPermissionGuardrail(CustomGuardrail):
request_tools.extend(map(_mcp_server_target, _request_list(request, "mcp_servers") or ()))
for function in _request_list(request, "functions") or ():
function_name = self._get_legacy_function_name(function)
if function_name is not None:
request_tools.append((function_name, "function"))
request_tools.extend(self._get_request_tool_targets(function))
listed_names: Final = frozenset(name for name, _ in request_tools)
for forced_tool_name in (
@ -1167,13 +1177,9 @@ class ToolPermissionGuardrail(CustomGuardrail):
}
functions: Final = _request_list(request, "functions")
kept_functions: Final = (
None
if functions is None
else [function for function in functions if self._get_legacy_function_name(function) not in denied_names]
)
kept_functions: Final = None if functions is None else kept_tool_list(functions)
if kept_functions is not None:
data["functions"] = kept_functions # rebind-ok: rewrite mode edits the request in place
data["functions"] = list(kept_functions) # rebind-ok: rewrite mode edits the request in place
mcp_servers: Final = _request_list(request, "mcp_servers")
kept_mcp_servers: Final = (
@ -1198,6 +1204,7 @@ class ToolPermissionGuardrail(CustomGuardrail):
kept_sent_tools: Final = (
*(kept_tools or ()),
*(kept_functions or ()),
*(kept_extra_body_tools or ()),
*_tools_in_items(kept_input_items),
)
@ -1205,7 +1212,7 @@ class ToolPermissionGuardrail(CustomGuardrail):
kept_types: Final = self._tool_types_in(kept_sent_tools)
removed_types: Final = self._tool_types_in(sent_tools) - kept_types
tools_remain: Final = bool(kept_sent_tools) or bool(kept_functions) or bool(kept_mcp_servers)
tools_remain: Final = bool(kept_sent_tools) or bool(kept_mcp_servers)
tool_choice: Final = _REQUEST_DATA.validate_python(data.get("tool_choice"))
if tool_choice is not None:
rewritten_choice: Final = self._rewritten_tool_choice(
@ -1214,7 +1221,7 @@ class ToolPermissionGuardrail(CustomGuardrail):
data["tool_choice"] = rewritten_choice # rebind-ok: rewrite mode edits the request in place
named_function_call: Final = self._get_named_function_call(data)
if named_function_call in denied_names:
if named_function_call in removed_names:
data["function_call"] = "none" # rebind-ok: rewrite mode edits the request in place
functions_remain: Final = "function" in kept_types

View file

@ -1783,6 +1783,24 @@ def test_vertex_ai_map_tools():
assert tools == new_tools
def test_vertex_ai_map_tools_declares_a_namespace_by_its_members() -> None:
namespace = {
"type": "namespace",
"name": "github",
"tools": [
{"type": "function", "name": "create_issue", "parameters": {"type": "object", "properties": {}}},
{"type": "function", "function": {"name": "close_issue"}},
],
}
tools = VertexGeminiConfig().map_function(value=[namespace], optional_params={})
assert [declaration["name"] for declaration in tools[0]["function_declarations"]] == [
"create_issue",
"close_issue",
]
def test_vertex_ai_map_tool_with_anyof():
"""
Related issue: https://github.com/BerriAI/litellm/issues/11164

View file

@ -2430,6 +2430,59 @@ class TestToolPermissionGuardrailAnthropicMessages:
assert result["tools"] == [weather]
@pytest.mark.asyncio
@pytest.mark.parametrize("on_disallowed_action", ["block", "rewrite"])
async def test_pre_call_leaves_a_gemini_hosted_key_that_sits_beside_a_function(
self, on_disallowed_action: Literal["block", "rewrite"]
) -> None:
guardrail = self._pre_call(
[{"id": "allow_weather", "tool_name": r"^get_weather$", "decision": "allow"}],
on_disallowed_action,
default_action="deny",
)
tool = {"type": "function", "function": {"name": "get_weather"}, "googleSearch": {}}
data = {"model": "gemini-3", "messages": [{"role": "user", "content": "hi"}], "tools": [tool]}
result = await self._run_pre_call(guardrail, data, "acompletion")
assert result["tools"] == [tool]
@pytest.mark.asyncio
@pytest.mark.parametrize("on_disallowed_action", ["block", "rewrite"])
@pytest.mark.parametrize(
("function", "denied"),
[
({"type": "function", "function": {"name": "delete_db"}}, "delete_db"),
({"name": "get_weather", "function": {"name": "delete_db"}}, "delete_db"),
({"googleSearch": {}}, "googleSearch"),
],
ids=["function_object", "function_object_behind_an_allowed_name", "gemini_hosted_key"],
)
async def test_pre_call_judges_a_legacy_functions_entry_like_a_tool(
self, function: dict[str, object], denied: str, on_disallowed_action: Literal["block", "rewrite"]
) -> None:
guardrail = self._pre_call(
[{"id": "allow_weather", "tool_name": r"^get_weather$", "decision": "allow"}],
on_disallowed_action,
default_action="deny",
)
weather = {"name": "get_weather"}
data = {
"model": "gemini-3",
"messages": [{"role": "user", "content": "hi"}],
"functions": [weather, function],
}
if on_disallowed_action == "block":
with pytest.raises(HTTPException) as excinfo:
await self._run_pre_call(guardrail, data, "acompletion")
assert excinfo.value.detail["detection_message"] == f"Tool '{denied}' denied by default action"
return
result = await self._run_pre_call(guardrail, data, "acompletion")
assert result["functions"] == [weather]
@pytest.mark.asyncio
async def test_rewrite_keeps_an_allowed_function_whose_declaration_list_is_denied(self) -> None:
guardrail = self._pre_call(