From eb1d8919b9ee7fa7dbfad67ae7321b4e73bd515d Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 19:06:46 -0700 Subject: [PATCH] fix(responses): check allowed_tools entries without recursion and drop a nested allowed_tools The repo's recursion check refuses recursive helpers, so allowed_tools entries now go through a separate per-entry check. OpenAI rejects an allowed_tools entry nested inside allowed_tools with a 400, so that choice now drops under drop_params. --- litellm/llms/openai/responses/transformation.py | 14 +++++++++++--- .../test_openai_responses_transformation.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.py index 340f158da46..193bfc25b33 100644 --- a/litellm/llms/openai/responses/transformation.py +++ b/litellm/llms/openai/responses/transformation.py @@ -337,11 +337,19 @@ def _names_a_sent_tool(tool_choice: object, tools: tuple[Mapping[str, object], . choice: Final = as_str_mapping(tool_choice) if choice is None: return False + if choice.get("type") != "allowed_tools": + return _sent_tools_include(choice, tools) + allowed: Final = choice.get("tools") + entries: Final = tuple(map(as_str_mapping, _json_array_or_empty(allowed))) + return not isinstance(allowed, list | tuple) or all( + entry is not None and _sent_tools_include(entry, tools) for entry in entries + ) + + +def _sent_tools_include(choice: Mapping[str, object], tools: tuple[Mapping[str, object], ...]) -> bool: match choice.get("type"): case "allowed_tools": - allowed: Final = choice.get("tools") - entries: Final = _json_array_or_empty(allowed) - return not isinstance(allowed, list | tuple) or all(_names_a_sent_tool(entry, tools) for entry in entries) + return False case "function": return any(tool.get("type") == "function" and tool.get("name") == choice.get("name") for tool in tools) case "custom": diff --git a/tests/unit/llms/openai/responses/test_openai_responses_transformation.py b/tests/unit/llms/openai/responses/test_openai_responses_transformation.py index bed173388c2..38d848086b5 100644 --- a/tests/unit/llms/openai/responses/test_openai_responses_transformation.py +++ b/tests/unit/llms/openai/responses/test_openai_responses_transformation.py @@ -3346,6 +3346,23 @@ class TestMalformedParamsFollowDropParams: }, id="allowed-tools-naming-a-tool-not-sent", ), + pytest.param( + { + "tool_choice": { + "type": "allowed_tools", + "mode": "auto", + "tools": [ + { + "type": "allowed_tools", + "mode": "auto", + "tools": [{"type": "function", "name": "get_weather"}], + } + ], + }, + "tools": [_WEATHER_TOOL, {"type": "web_search"}], + }, + id="allowed-tools-nested-in-allowed-tools", + ), pytest.param({"parallel_tool_calls": "true"}, id="parallel-tool-calls-string"), pytest.param({"previous_response_id": 12}, id="previous-response-id-int"), pytest.param({"previous_response_id": ""}, id="previous-response-id-empty"),