mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(guardrails): judge Gemini hosted keys whatever the tool type and a namespace's function
This commit is contained in:
parent
2db8a16723
commit
600547764d
2 changed files with 92 additions and 7 deletions
|
|
@ -35,6 +35,7 @@ from litellm.proxy.guardrails.anthropic_sse import (
|
|||
from litellm.responses.additional_tools import ADDITIONAL_TOOLS_INPUT_ITEM_TYPE
|
||||
from litellm.types.guardrails import GuardrailEventHooks, LitellmParams
|
||||
from litellm.types.llms.anthropic import ANTHROPIC_HOSTED_TOOLS
|
||||
from litellm.types.llms.vertex_ai import VertexToolName
|
||||
from litellm.types.proxy.guardrails.guardrail_hooks.tool_permission import (
|
||||
PermissionError,
|
||||
ToolPermissionRule,
|
||||
|
|
@ -64,6 +65,7 @@ _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)
|
||||
_NAMED_CHOICE_TYPES: Final = frozenset({"tool", "function", "custom", "allowed_tools", "any", "auto", "none"})
|
||||
_DECODED_ARGUMENTS: Final = TypeAdapter(object)
|
||||
_REQUEST_DATA: Final = TypeAdapter(object)
|
||||
|
|
@ -109,12 +111,29 @@ def _request_tool_names(tool: object) -> tuple[str, ...]:
|
|||
server_name: Final = (
|
||||
_non_empty_string(fields.get(server_field)) if fields is not None and server_field is not None else None
|
||||
)
|
||||
function: Final = _object_mapping(fields.get("function")) if fields is not None else None
|
||||
function_name: Final = _non_empty_string(function.get("name")) if function is not None else None
|
||||
extra_names: Final = tuple(name for name in (function_name, custom_name, server_name) if name is not None)
|
||||
extra_names: Final = tuple(name for name in (_function_name(tool), custom_name, server_name) if name is not None)
|
||||
return tuple(dict.fromkeys(anthropic_tool_names(tool) + extra_names))
|
||||
|
||||
|
||||
def _function_name(tool: object) -> str | None:
|
||||
fields: Final = _object_mapping(tool)
|
||||
function: Final = _object_mapping(fields.get("function")) if fields is not None else None
|
||||
return _non_empty_string(function.get("name")) if function is not None else None
|
||||
|
||||
|
||||
def _namespace_function_targets(tool: object) -> tuple[_ToolTarget, ...]:
|
||||
function_name: Final = _function_name(tool)
|
||||
return ((function_name, "function"),) if function_name is not None else ()
|
||||
|
||||
|
||||
def _gemini_hosted_key_targets(tool: object) -> tuple[_ToolTarget, ...]:
|
||||
fields: Final = _object_mapping(tool)
|
||||
if fields is None:
|
||||
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)
|
||||
|
||||
|
||||
def _namespace_members(tool: object) -> Sequence[object]:
|
||||
fields: Final = _object_mapping(tool)
|
||||
members: Final = (
|
||||
|
|
@ -950,8 +969,12 @@ class ToolPermissionGuardrail(CustomGuardrail):
|
|||
return member_targets + part_targets + self._own_tool_targets(tool)
|
||||
|
||||
def _own_tool_targets(self, tool: object) -> tuple[_ToolTarget, ...]:
|
||||
if _keyed_tool_fields(tool) is not None or _namespace_members(tool):
|
||||
if _keyed_tool_fields(tool) is not None:
|
||||
return ()
|
||||
named: Final = _namespace_function_targets(tool) if _namespace_members(tool) else self._named_tool_targets(tool)
|
||||
return named + _gemini_hosted_key_targets(tool)
|
||||
|
||||
def _named_tool_targets(self, tool: object) -> tuple[_ToolTarget, ...]:
|
||||
raw_type: Final = self._get_mapping_value(tool, "type")
|
||||
tool_type: Final = raw_type if isinstance(raw_type, str) else None
|
||||
anthropic_custom: Final = (
|
||||
|
|
@ -1043,7 +1066,8 @@ class ToolPermissionGuardrail(CustomGuardrail):
|
|||
fields: Final = _object_mapping(tool)
|
||||
if fields is None or (group is None and keyed is None):
|
||||
return () if any(map(is_denied, self._get_request_tool_targets(tool))) else (tool,)
|
||||
if any(map(is_denied, self._own_tool_targets(tool))):
|
||||
own_targets: Final = self._own_tool_targets(tool)
|
||||
if any(map(is_denied, own_targets)):
|
||||
return ()
|
||||
kept_members: Final = _kept_members(group, is_denied)
|
||||
denied_keys: Final = (
|
||||
|
|
@ -1060,7 +1084,8 @@ class ToolPermissionGuardrail(CustomGuardrail):
|
|||
for key, value in fields.items()
|
||||
if key not in dropped_keys
|
||||
}
|
||||
parts_left: Final = bool(kept_members) or (keyed is not None and bool(_part_keys(kept_fields)))
|
||||
own_name_left: Final = any(name is not None for name, _ in own_targets)
|
||||
parts_left: Final = bool(kept_members) or own_name_left or (keyed is not None and bool(_part_keys(kept_fields)))
|
||||
return (kept_fields,) if parts_left else ()
|
||||
|
||||
def _kept_tool_list(
|
||||
|
|
|
|||
|
|
@ -2353,6 +2353,12 @@ class TestToolPermissionGuardrailAnthropicMessages:
|
|||
{"type": "function", "function": {"name": "delete_db"}, "function_declarations": [{"name": "get_weather"}]},
|
||||
{"type": "custom", "name": "get_weather", "function": {"name": "delete_db"}},
|
||||
{"type": "mcp", "server_label": "get_weather", "function": {"name": "delete_db"}},
|
||||
{
|
||||
"type": "namespace",
|
||||
"name": "github",
|
||||
"tools": [{"type": "function", "name": "create_issue"}],
|
||||
"function": {"name": "delete_db"},
|
||||
},
|
||||
{
|
||||
"type": "computer_20251124",
|
||||
"name": "computer",
|
||||
|
|
@ -2364,6 +2370,7 @@ class TestToolPermissionGuardrailAnthropicMessages:
|
|||
"snake_case_declarations_list",
|
||||
"custom_top_level_name",
|
||||
"mcp_server_label",
|
||||
"namespace_with_members",
|
||||
"computer_top_level_name",
|
||||
],
|
||||
)
|
||||
|
|
@ -2371,7 +2378,7 @@ class TestToolPermissionGuardrailAnthropicMessages:
|
|||
self, tool: dict[str, object], on_disallowed_action: Literal["block", "rewrite"]
|
||||
) -> None:
|
||||
guardrail = self._pre_call(
|
||||
[{"id": "allow_known", "tool_name": r"^(get_weather|computer)$", "decision": "allow"}],
|
||||
[{"id": "allow_known", "tool_name": r"^(get_weather|computer|github__create_issue)$", "decision": "allow"}],
|
||||
on_disallowed_action,
|
||||
default_action="deny",
|
||||
)
|
||||
|
|
@ -2388,6 +2395,59 @@ class TestToolPermissionGuardrailAnthropicMessages:
|
|||
|
||||
assert result["tools"] == [weather]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("on_disallowed_action", ["block", "rewrite"])
|
||||
@pytest.mark.parametrize(
|
||||
("tool", "hosted_key"),
|
||||
[
|
||||
({"type": "get_weather", "googleSearch": {}}, "googleSearch"),
|
||||
({"type": "function", "codeExecution": {}}, "codeExecution"),
|
||||
({"type": "get_weather", "url_context": {}}, "url_context"),
|
||||
],
|
||||
ids=["named_type_google_search", "function_type_code_execution", "named_type_url_context"],
|
||||
)
|
||||
async def test_pre_call_judges_a_gemini_hosted_tool_key_whatever_the_tool_type(
|
||||
self, tool: dict[str, object], hosted_key: str, on_disallowed_action: Literal["block", "rewrite"]
|
||||
) -> None:
|
||||
guardrail = self._pre_call(
|
||||
[
|
||||
{"id": "allow_weather", "tool_name": r"^get_weather$", "decision": "allow"},
|
||||
{"id": "allow_functions", "tool_type": r"^function$", "decision": "allow"},
|
||||
],
|
||||
on_disallowed_action,
|
||||
default_action="deny",
|
||||
)
|
||||
weather = {"type": "function", "function": {"name": "get_weather"}}
|
||||
data = {"model": "gemini-3", "messages": [{"role": "user", "content": "hi"}], "tools": [weather, tool]}
|
||||
|
||||
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 '{hosted_key}' denied by default action"
|
||||
return
|
||||
|
||||
result = await self._run_pre_call(guardrail, data, "acompletion")
|
||||
|
||||
assert result["tools"] == [weather]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rewrite_keeps_an_allowed_function_whose_declaration_list_is_denied(self) -> None:
|
||||
guardrail = self._pre_call(
|
||||
[{"id": "allow_weather", "tool_name": r"^get_weather$", "decision": "allow"}],
|
||||
"rewrite",
|
||||
default_action="deny",
|
||||
)
|
||||
tool = {
|
||||
"type": "function",
|
||||
"function": {"name": "get_weather"},
|
||||
"functionDeclarations": [{"name": "delete_db"}],
|
||||
}
|
||||
data = {"model": "gemini-3", "messages": [{"role": "user", "content": "hi"}], "tools": [tool]}
|
||||
|
||||
result = await self._run_pre_call(guardrail, data, "acompletion")
|
||||
|
||||
assert result["tools"] == [{"type": "function", "function": {"name": "get_weather"}}]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pre_call_allows_a_custom_tool_by_its_function_name(self) -> None:
|
||||
guardrail = self._pre_call(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue