From 4eabe4367e5bdef8089fc32da11286be4d4c0f1d Mon Sep 17 00:00:00 2001 From: Gennaro Malafronte <8798987+malafronte@users.noreply.github.com> Date: Thu, 23 Apr 2026 01:13:49 +0200 Subject: [PATCH] fix(anthropic): drop empty reasoning sentinel --- .../adapters/transformation.py | 4 ++- ...al_pass_through_adapters_transformation.py | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index e3c62dc9ea7..5c8e26b8855 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -664,7 +664,9 @@ class LiteLLMAnthropicMessagesAdapter: if len(tool_calls) > 0: assistant_message["tool_calls"] = tool_calls # type: ignore assistant_message["reasoning_content"] = ( - "".join(reasoning_content_parts) if len(reasoning_content_parts) > 0 else " " + "".join(reasoning_content_parts) + if len(reasoning_content_parts) > 0 + else None ) # type: ignore if len(thinking_blocks) > 0: assistant_message["thinking_blocks"] = thinking_blocks # type: ignore 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 8b7e5c3f05b..7556d842f7f 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 @@ -1007,6 +1007,34 @@ def test_translate_anthropic_messages_to_openai_tool_use_with_signature(): ) +def test_translate_anthropic_messages_to_openai_tool_use_without_thinking_blocks(): + anthropic_messages = [ + AnthropicMessagesUserMessageParam( + role="user", + content=[{"type": "text", "text": "Use the weather tool."}], + ), + AnthopicMessagesAssistantMessageParam( + role="assistant", + content=[ + { + "type": "tool_use", + "id": "toolu_01234", + "name": "get_weather", + "input": {"location": "Boston"}, + } + ], + ), + ] + + adapter = LiteLLMAnthropicMessagesAdapter() + result = adapter.translate_anthropic_messages_to_openai(messages=anthropic_messages) + + assert len(result) == 2 + assert result[1]["role"] == "assistant" + assert len(result[1]["tool_calls"]) == 1 + assert result[1].get("reasoning_content") is None + + def test_translate_anthropic_messages_to_openai_tool_result_with_multiple_content_items(): """ Test that tool_result with multiple content items creates a single tool message