From 48d4b994a415bdecd76c5e3f89ae8be9e4488d28 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Mon, 2 Nov 2026 17:17:44 -0800 Subject: [PATCH] fix(tool_choice): keep allowed_tools selector extension fields and leave conflicting selectors untranslated A nested function or custom selector now keeps its extra keys when flattened for the Responses bridge, and a selector that has both a flat name and a nested object, or keys that collide when lifted, makes the bridge send the original restriction untouched instead of guessing which name wins --- .../allowed_tools_tool_choice.py | 28 ++++++++++--------- .../test_allowed_tools_tool_choice.py | 16 ++++++++--- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/litellm/litellm_core_utils/allowed_tools_tool_choice.py b/litellm/litellm_core_utils/allowed_tools_tool_choice.py index 8fc3b7c5535..5c8f5d83ece 100644 --- a/litellm/litellm_core_utils/allowed_tools_tool_choice.py +++ b/litellm/litellm_core_utils/allowed_tools_tool_choice.py @@ -30,13 +30,7 @@ class ChatAllowedToolsToolChoice(BaseModel): _CHAT_ALLOWED_TOOLS_TOOL_CHOICE: Final = TypeAdapter(ChatAllowedToolsToolChoice) -class _NamedTool(BaseModel): - model_config = ConfigDict(frozen=True) - - name: str = Field(min_length=1) - - -_NAMED_TOOL: Final = TypeAdapter(_NamedTool) +_SELECTOR: Final = TypeAdapter(dict[str, object]) class _ToolChoiceType(BaseModel): @@ -83,22 +77,30 @@ def parse_chat_allowed_tools_tool_choice(tool_choice: object) -> ChatAllowedTool return None +def _has_name(selector: Mapping[str, object]) -> bool: + name: Final = selector.get("name") + return isinstance(name, str) and name != "" + + def _responses_allowed_tool(tool: Mapping[str, object]) -> Mapping[str, object] | None: tool_type: Final = tool.get("type") if tool_type not in ("function", "custom"): return tool - if isinstance(tool.get("name"), str): - return tool + if tool_type not in tool: + return tool if _has_name(tool) else None try: - nested: Final = _NAMED_TOOL.validate_python(tool.get(tool_type)) + nested: Final = _SELECTOR.validate_python(tool[tool_type]) except ValidationError: return None - return {"type": tool_type, "name": nested.name} + outer: Final = {key: value for key, value in tool.items() if key != tool_type} + if not _has_name(nested) or outer.keys() & nested.keys(): + return None + return {**outer, **nested} def responses_allowed_tools_tool_choice(choice: ChatAllowedToolsToolChoice) -> ToolChoiceAllowedParam | None: - """The flat Responses API layout of a chat ``allowed_tools`` choice, or ``None`` when an entry is not one the - chat layout defines, so the caller sends the original rather than a rewrite that could mean something looser.""" + """The flat Responses API layout of a chat ``allowed_tools`` choice, or ``None`` when an entry is malformed or + carries conflicting keys, so the caller sends the original rather than a rewrite that could mean something looser.""" flattened: Final = tuple(_responses_allowed_tool(tool) for tool in choice.allowed_tools.tools) if any(tool is None for tool in flattened): return None diff --git a/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py b/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py index 9b7b7eac5e2..6ebbdcf73bf 100644 --- a/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py +++ b/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py @@ -17,7 +17,7 @@ _CHAT_LAYOUT: Final[dict[str, object]] = { "mode": "auto", "tools": [ {"type": "function", "function": {"name": "get_time"}}, - {"type": "custom", "custom": {"name": "apply_patch"}}, + {"type": "custom", "custom": {"name": "apply_patch", "x-vendor": 1}, "x-trace": "a"}, {"type": "mcp", "server_label": "docs", "server_url": "https://docs.example"}, {"type": "function", "name": "already_flat"}, ], @@ -37,7 +37,7 @@ def test_responses_layout_lifts_mode_and_tools_and_flattens_names_in_order(): "mode": "auto", "tools": [ {"type": "function", "name": "get_time"}, - {"type": "custom", "name": "apply_patch"}, + {"type": "custom", "name": "apply_patch", "x-vendor": 1, "x-trace": "a"}, {"type": "mcp", "server_label": "docs", "server_url": "https://docs.example"}, {"type": "function", "name": "already_flat"}, ], @@ -54,9 +54,17 @@ def test_responses_layout_leaves_the_caller_dict_untouched(): @pytest.mark.parametrize( "tool", - [{"type": "function"}, {"type": "function", "function": {}}, {"type": "custom", "custom": {"name": ""}}], + [ + {"type": "function"}, + {"type": "function", "name": ""}, + {"type": "function", "function": {}}, + {"type": "function", "function": "get_time"}, + {"type": "custom", "custom": {"name": ""}}, + {"type": "function", "name": "get_time", "function": {"name": "get_weather"}}, + {"type": "function", "function": {"name": "get_time", "type": "custom"}}, + ], ) -def test_responses_layout_refuses_to_rewrite_an_entry_without_a_name(tool: dict[str, object]): +def test_responses_layout_refuses_to_rewrite_a_malformed_or_conflicting_entry(tool: dict[str, object]): tool_choice: Final[dict[str, object]] = { "type": "allowed_tools", "allowed_tools": {"mode": "required", "tools": [tool]},