From 82df8a948938084926d42d3e333cbdbfc3c5853d Mon Sep 17 00:00:00 2001 From: yucheng Date: Sat, 8 Aug 2026 03:37:38 +0000 Subject: [PATCH] fix(responses-bridge): flatten namespace tools without recursion Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../namespace_tools.py | 10 +++---- .../transformation.py | 2 +- .../test_litellm_completion_responses.py | 28 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/namespace_tools.py b/litellm/responses/litellm_completion_transformation/namespace_tools.py index 2624d4238e9..5e1ad9fced8 100644 --- a/litellm/responses/litellm_completion_transformation/namespace_tools.py +++ b/litellm/responses/litellm_completion_transformation/namespace_tools.py @@ -5,7 +5,9 @@ Completions providers. A namespace tool is a grouping container: it carries no callable schema of its own and holds its callable tools under ``tools``. Chat Completions has no equivalent container, so the bridge replaces each namespace with the tools it contains, which then go through -the same conversion as any top level tool. +the same conversion as any top level tool. Namespaces are flat in practice, and a +namespace that somehow contains another one keeps the inner container, which the +conversion then drops as an unsupported type. """ from collections.abc import Sequence @@ -19,9 +21,7 @@ ResponsesAPITool: TypeAlias = FunctionToolParam | OpenAIMcpServerTool def flatten_namespace_tools(tools: Sequence[ResponsesAPITool]) -> tuple[ResponsesAPITool, ...]: - """Replace every namespace tool with its nested tools, recursively.""" + """Replace every namespace tool with the tools it contains.""" return tuple( - nested - for tool in tools - for nested in (flatten_namespace_tools(tool.get("tools") or ()) if tool.get("type") == "namespace" else (tool,)) + nested for tool in tools for nested in (tool.get("tools") or () if tool.get("type") == "namespace" else (tool,)) ) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 3fb63029968..f54cb4322ce 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -1324,7 +1324,7 @@ class LiteLLMCompletionResponsesConfig: chat_completion_tools.append(converted) else: _tool_type = tool.get("type") - if _tool_type in ("computer_use", "image_generation", "shell"): + if _tool_type in ("computer_use", "image_generation", "namespace", "shell"): # Drop unsupported Responses-API-only tool types that have no # Chat Completions equivalent. Passing them through verbatim # causes providers to reject the request with "'function' is a diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py index ca743a655d3..85118c76f91 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py @@ -1633,6 +1633,34 @@ class TestToolTransformation: "read_file", ] + def test_transform_nested_namespace_container_is_dropped(self): + """A namespace inside a namespace is not something clients send, but its inner + container must never reach the provider as a tool without a function schema.""" + tools = [ + { + "type": "namespace", + "name": "outer", + "tools": [ + {"type": "function", "name": "outer_tool"}, + { + "type": "namespace", + "name": "inner", + "tools": [{"type": "function", "name": "inner_tool"}], + }, + ], + } + ] + + ( + result_tools, + _, + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( + tools=tools + ) + + assert [tool["function"]["name"] for tool in result_tools] == ["outer_tool"] + assert not any(tool.get("type") == "namespace" for tool in result_tools) + def test_bedrock_anthropic_drops_derived_web_search_options(self): """ Regression for LIT-3858: a Responses web_search tool becomes a derived