diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index 667f9dcaab0..77a243f53e0 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -757,7 +757,15 @@ class LiteLLMAnthropicMessagesAdapter: name=truncated_name, ) if "input_schema" in tool: - function_chunk["parameters"] = tool["input_schema"] + # Shallow-copy so the additional-kwargs loop below cannot mutate + # the caller's schema in place. Callers reuse the same tool list + # (e.g. the pass-through guardrail translates the tools, then the + # request is translated again for the real call), so aliasing the + # source dict here leaks non-schema keys such as display_width_px + # back into it on the first pass and forwards a polluted schema on + # the second. translate_anthropic_output_format_to_openai in this + # same file already copies its schema for the same reason. + function_chunk["parameters"] = dict(tool["input_schema"] or {}) if "description" in tool: function_chunk["description"] = tool["description"] diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py index 9145829ecb2..56d2085aa15 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py @@ -3694,3 +3694,43 @@ def test_tool_result_plain_text_unchanged_by_openai_transform(): assert len(tool_messages) == 1 assert tool_messages[0]["content"] == "42 files found" assert _image_urls_in_user_messages(result) == [] + + +def test_translate_anthropic_tools_to_openai_does_not_mutate_input_schema(): + """Regression for #34510: translation must not alias and mutate the caller's + ``input_schema``. Extra top-level tool keys (e.g. ``display_width_px`` on a + computer tool) are merged into the OpenAI function ``parameters``; if that + dict is the caller's ``input_schema`` by reference, those non-schema keys + leak back into the source. Callers reuse the same tool list across passes + (guardrail pre-call translation, then the real request), so on the second + pass a polluted schema gets forwarded.""" + import copy + + adapter = LiteLLMAnthropicMessagesAdapter() + tool = { + "name": "computer", + "type": "computer_20241022", + "input_schema": {"type": "object", "properties": {"action": {"type": "string"}}}, + "display_width_px": 1024, + "display_height_px": 768, + } + original = copy.deepcopy(tool) + + new_tools, _ = adapter.translate_anthropic_tools_to_openai(tools=[tool]) + + # The source tool and its schema are untouched. + assert tool == original + assert "display_width_px" not in tool["input_schema"] + assert "display_height_px" not in tool["input_schema"] + + # The translated parameters still carry the schema plus the vendor kwargs. + params = new_tools[0]["function"]["parameters"] + assert params["type"] == "object" + assert params["properties"] == {"action": {"type": "string"}} + assert params["display_width_px"] == 1024 + assert params["display_height_px"] == 768 + + # Re-translating the reused tool keeps the source clean (the pass-through + # double-translation scenario from the issue). + adapter.translate_anthropic_tools_to_openai(tools=[tool]) + assert tool == original