From 0b49acea78758e4c4ed6a345dadd1a54df485fba Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 18:34:04 +0000 Subject: [PATCH] fix(anthropic): stop injecting empty cache_control on translated thinking blocks Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../adapters/transformation.py | 4 +- ...al_pass_through_adapters_transformation.py | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index 573a461e89e..ca3e5cd8187 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -528,15 +528,15 @@ class LiteLLMAnthropicMessagesAdapter: type="thinking", thinking=content.get("thinking") or "", signature=content.get("signature") or "", - cache_control=content.get("cache_control", {}), ) + self._add_cache_control_if_applicable(content, thinking_block, model) thinking_blocks.append(thinking_block) elif content.get("type") == "redacted_thinking": redacted_thinking_block = ChatCompletionRedactedThinkingBlock( type="redacted_thinking", data=content.get("data") or "", - cache_control=content.get("cache_control", {}), ) + self._add_cache_control_if_applicable(content, redacted_thinking_block, model) thinking_blocks.append(redacted_thinking_block) if ( 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 2d74c00071b..2819c5703cf 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 @@ -362,6 +362,56 @@ def test_translate_anthropic_messages_to_openai_thinking_blocks(): assert result[1]["tool_calls"][0]["id"] == "toolu_01234" +def test_translate_anthropic_to_openai_thinking_blocks_only_preserve_cache_control(): + adapter = LiteLLMAnthropicMessagesAdapter() + request_without_cache_control = { + "model": "claude-sonnet-5", + "max_tokens": 10, + "messages": [ + {"role": "user", "content": "hi"}, + { + "role": "assistant", + "content": [ + {"type": "thinking", "thinking": "hmm", "signature": "sig"}, + {"type": "redacted_thinking", "data": "redacted"}, + ], + }, + ], + } + + translated_without_cache_control, _ = adapter.translate_anthropic_to_openai(request_without_cache_control) + thinking_blocks_without_cache_control = translated_without_cache_control["messages"][1]["thinking_blocks"] + + assert len(thinking_blocks_without_cache_control) == 2 + assert "cache_control" not in thinking_blocks_without_cache_control[0] + assert "cache_control" not in thinking_blocks_without_cache_control[1] + + request_with_cache_control = { + "model": "claude-sonnet-5", + "max_tokens": 10, + "messages": [ + {"role": "user", "content": "hi"}, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "hmm", + "signature": "sig", + "cache_control": {"type": "ephemeral"}, + }, + {"type": "redacted_thinking", "data": "redacted"}, + ], + }, + ], + } + + translated_with_cache_control, _ = adapter.translate_anthropic_to_openai(request_with_cache_control) + thinking_blocks_with_cache_control = translated_with_cache_control["messages"][1]["thinking_blocks"] + + assert thinking_blocks_with_cache_control[0]["cache_control"] == {"type": "ephemeral"} + + def test_translate_anthropic_messages_to_openai_sets_reasoning_content(): """Reasoning-aware chat providers read reasoning_content, so thinking text must land there.