From 29126ea4dea5d630ca3fb9d6e8d2a8462bf9784c Mon Sep 17 00:00:00 2001 From: adhavan18 Date: Thu, 24 Sep 2026 09:16:26 +0530 Subject: [PATCH] fix(adapter): gate thinking blocks by target --- .../adapters/transformation.py | 8 +++- ...al_pass_through_adapters_transformation.py | 48 +++++++++++++++++++ 2 files changed, 54 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 85431a5a637..d3e24cdd11d 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -625,11 +625,15 @@ class LiteLLMAnthropicMessagesAdapter: assistant_message = ChatCompletionAssistantMessage( role="assistant", content=assistant_content, - thinking_blocks=(thinking_blocks if len(thinking_blocks) > 0 else None), ) if len(tool_calls) > 0: assistant_message["tool_calls"] = tool_calls - if len(thinking_blocks) > 0: + if len(thinking_blocks) > 0 and ( + model is None + or LiteLLMAnthropicMessagesAdapter.is_anthropic_claude_model( + model + ) + ): assistant_message["thinking_blocks"] = thinking_blocks reasoning_content = reasoning_content_from_thinking_blocks(thinking_blocks) if reasoning_content: 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 d03174bc2c6..92383e3d621 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 @@ -481,6 +481,54 @@ def test_translate_anthropic_messages_to_openai_thinking_blocks(): assert result[1]["tool_calls"][0]["id"] == "toolu_01234" +def test_translate_anthropic_messages_to_openai_keeps_thinking_blocks_for_claude(): + anthropic_messages = [ + AnthopicMessagesAssistantMessageParam( + role="assistant", + content=[ + { + "type": "thinking", + "thinking": "My reasoning here.", + "signature": "sig123", + }, + {"type": "text", "text": "Answer text."}, + ], + ) + ] + + result = LiteLLMAnthropicMessagesAdapter().translate_anthropic_messages_to_openai( + messages=anthropic_messages, + model="anthropic/claude-sonnet-4-5", + ) + + assert result[0]["thinking_blocks"][0]["thinking"] == "My reasoning here." + assert result[0]["reasoning_content"] == "My reasoning here." + + +def test_translate_anthropic_messages_to_openai_omits_thinking_blocks_for_non_claude(): + anthropic_messages = [ + AnthopicMessagesAssistantMessageParam( + role="assistant", + content=[ + { + "type": "thinking", + "thinking": "My reasoning here.", + "signature": "sig123", + }, + {"type": "text", "text": "Answer text."}, + ], + ) + ] + + result = LiteLLMAnthropicMessagesAdapter().translate_anthropic_messages_to_openai( + messages=anthropic_messages, + model="openai/my-vllm-model", + ) + + assert "thinking_blocks" not in result[0] + assert result[0]["reasoning_content"] == "My reasoning here." + + def test_translate_anthropic_messages_to_openai_drops_bridge_encrypted_reasoning_blocks(): """A session that moves from an OpenAI reasoning model to a chat provider replays reasoning only OpenAI can read.