Skip to content

fix(anthropic): stop tool translation from mutating caller input_schema#34602

Open
cognis-digital wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
cognis-digital:litellm_fix_tool_translation_schema_mutation
Open

fix(anthropic): stop tool translation from mutating caller input_schema#34602
cognis-digital wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
cognis-digital:litellm_fix_tool_translation_schema_mutation

Conversation

@cognis-digital

Copy link
Copy Markdown

TLDR

Problem this solves:

  • translate_anthropic_tools_to_openai assigned the caller's input_schema dict to the translated parameters by reference, then the "pass additional computer kwargs" loop merged extra top-level tool keys (e.g. a computer tool's display_width_px) into that shared dict in place
  • Callers reuse the same in-memory tool list (a pre-call guardrail translates the tools, then the real call translates them again), so the second pass forwarded a schema polluted with non-schema keys, and providers that validate the tool parameters JSON schema can reject the request. The corruption is silent

How it solves it:

  • Deep-copy input_schema before assigning it to function_chunk["parameters"], so translation is a pure read and later kwarg merges land only on the copy
  • This mirrors the sibling translate_anthropic_output_format_to_openai in the same file, which already copy.deepcopys its schema for the same reason; copy is already imported

Relevant issues

Fixes #34510

Linear ticket

Type

🐛 Bug Fix

Changes

litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py: deep-copy input_schema before it becomes the translated OpenAI parameters

Added a regression test test_translate_anthropic_tools_to_openai_does_not_mutate_caller_input_schema that asserts the caller's input_schema is untouched after translation and that the vendor kwargs still reach the translated parameters. It fails on the pre-fix code and passes after

QA runbook

Pure translation logic, so it reproduces with no provider credentials

from litellm.llms.anthropic.experimental_pass_through.adapters.transformation import LiteLLMAnthropicMessagesAdapter
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,
}
before = copy.deepcopy(tool["input_schema"])
adapter.translate_anthropic_tools_to_openai(tools=[tool], model="claude-3-5-sonnet")
print("mutated:", tool["input_schema"] != before)

Before this PR prints mutated: True (the caller's schema now contains display_width_px / display_height_px); after, it prints mutated: False

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes Anthropic tool translation so caller-owned input schemas remain unchanged.

  • Deep-copies input_schema before merging provider-specific tool fields into translated OpenAI parameters.
  • Adds a regression test verifying input immutability, vendor-field propagation, nested schema preservation, and distinct object identity.

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified.

The copied value is a request JSON-schema dictionary, downstream mutations now apply only to the translated object, and the regression test directly covers both immutability and expected translated output.

Important Files Changed

Filename Overview
litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py Deep-copies the caller-provided schema before downstream parameter mutation, preventing translation from corrupting reused request data.
tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py Adds focused regression coverage demonstrating that translation preserves the source schema while retaining provider-specific fields in the translated result.

Reviews (1): Last reviewed commit: "fix(anthropic): stop tool translation fr..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed-hq

codspeed-hq Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing cognis-digital:litellm_fix_tool_translation_schema_mutation (7fa14bd) with litellm_internal_staging (b9b27c2)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Anthropic→OpenAI tool translation aliases & mutates caller's input_schema in place

2 participants