fix(anthropic/adapter): stop translate_anthropic_tools_to_openai mutating the caller's input_schema

`translate_anthropic_tools_to_openai` assigned the caller's `input_schema`
straight into `function_chunk["parameters"]`, then merged the tool's remaining
top-level keys into that same dict. Because the assignment aliased rather than
copied, keys such as `display_width_px` on a computer tool were written back
into the caller's schema.

That matters because callers reuse the tool list across passes: the pass-through
guardrail translates the tools, and the request is then translated again for the
real call. The first pass pollutes the source and the second forwards a schema
carrying non-schema keys.

Shallow-copying fixes it. `translate_anthropic_output_format_to_openai` in the
same file already copies its schema for this reason, so this brings the two into
line.

Rebased onto current litellm_internal_staging; the original branch had gone to
conflict on surrounding drift while the line it fixes is unchanged.
This commit is contained in:
ErenAta16 2026-08-16 17:02:56 +03:00
parent 973329e986
commit b8bb43ecc4
2 changed files with 49 additions and 1 deletions

View file

@ -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"]

View file

@ -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