Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(anthropic): prevent dropping thinking when any message has thinking_blocks (#18929)

Ryan Malloyc2366194d4fb
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py
index 57391c152cb..02a873c2a1c 100644
--- a/litellm/llms/anthropic/chat/transformation.py
+++ b/litellm/llms/anthropic/chat/transformation.py
@@ -62,6 +62,7 @@ from litellm.utils import (
ModelResponse,
Usage,
add_dummy_tool,
+ any_assistant_message_has_thinking_blocks,
get_max_tokens,
has_tool_call_blocks,
last_assistant_with_tool_calls_has_no_thinking_blocks,
@@ -1013,10 +1014,16 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
# Drop thinking param if thinking is enabled but thinking_blocks are missing
# This prevents the error: "Expected thinking or redacted_thinking, but found tool_use"
+ #
+ # IMPORTANT: Only drop thinking if NO assistant messages have thinking_blocks.
+ # If any message has thinking_blocks, we must keep thinking enabled, otherwise
+ # Anthropic errors with: "When thinking is disabled, an assistant message cannot contain thinking"
+ # Related issue: https://github.com/BerriAI/litellm/issues/18926
if (
optional_params.get("thinking") is not None
and messages is not None
and last_assistant_with_tool_calls_has_no_thinking_blocks(messages)
+ and not any_assistant_message_has_thinking_blocks(messages)
):
if litellm.modify_params:
optional_params.pop("thinking", None)
diff --git a/litellm/utils.py b/litellm/utils.py
index 1393f1f5cb5..fc8982f382f 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -7195,6 +7195,29 @@ def has_tool_call_blocks(messages: List[AllMessageValues]) -> bool:
return False
+def any_assistant_message_has_thinking_blocks(
+ messages: List[AllMessageValues],
+) -> bool:
+ """
+ Returns true if ANY assistant message has thinking_blocks.
+
+ This is used to prevent dropping the thinking param when some messages
+ in the conversation already contain thinking blocks. Dropping thinking
+ when thinking blocks exist causes Anthropic error:
+ "When thinking is disabled, an assistant message cannot contain thinking"
+
+ Related issue: https://github.com/BerriAI/litellm/issues/18926
+ """
+ for message in messages:
+ if message.get("role") == "assistant":
+ thinking_blocks = message.get("thinking_blocks")
+ if thinking_blocks is not None and (
+ not hasattr(thinking_blocks, "__len__") or len(thinking_blocks) > 0
+ ):
+ return True
+ return False
+
+
def last_assistant_with_tool_calls_has_no_thinking_blocks(
messages: List[AllMessageValues],
) -> bool:
@@ -7207,6 +7230,10 @@ def last_assistant_with_tool_calls_has_no_thinking_blocks(
When thinking is enabled, assistant messages with tool_calls must include thinking_blocks.
If the client didn't preserve thinking_blocks, we need to drop the thinking param.
+ IMPORTANT: This should only be used in conjunction with
+ any_assistant_message_has_thinking_blocks() to ensure we don't drop thinking
+ when other messages in the conversation contain thinking blocks.
+
Related issues: https://github.com/BerriAI/litellm/issues/14194, https://github.com/BerriAI/litellm/issues/9020
"""
# Find the last assistant message with tool_calls
diff --git a/tests/test_litellm/test_utils.py b/tests/test_litellm/test_utils.py
index bfa162e019b..8fb70e7f114 100644
--- a/tests/test_litellm/test_utils.py
+++ b/tests/test_litellm/test_utils.py
@@ -2805,3 +2805,117 @@ def test_azure_ai_claude_provider_config():
provider=LlmProviders.AZURE_AI,
)
assert isinstance(config, AzureAIStudioConfig)
+
+
+# Tests for thinking blocks helper functions
+# Related to issue: https://github.com/BerriAI/litellm/issues/18926
+
+
+def test_any_assistant_message_has_thinking_blocks_with_thinking():
+ """Test that function returns True when any assistant message has thinking_blocks."""
+ from litellm.utils import any_assistant_message_has_thinking_blocks
+
+ messages = [
+ {"role": "user", "content": "Hello"},
+ {
+ "role": "assistant",
+ "thinking_blocks": [{"type": "thinking", "thinking": "Let me think..."}],
+ "tool_calls": [{"id": "123", "function": {"name": "test"}}],
+ },
+ {"role": "tool", "tool_call_id": "123", "content": "result"},
+ {
+ "role": "assistant",
+ "tool_calls": [{"id": "456", "function": {"name": "test2"}}],
+ # No thinking_blocks here - Claude sometimes doesn't include them
+ },
+ ]
+
+ assert any_assistant_message_has_thinking_blocks(messages) is True
+
+
+def test_any_assistant_message_has_thinking_blocks_without_thinking():
+ """Test that function returns False when no assistant message has thinking_blocks."""
+ from litellm.utils import any_assistant_message_has_thinking_blocks
+
+ messages = [
+ {"role": "user", "content": "Hello"},
+ {
+ "role": "assistant",
+ "tool_calls": [{"id": "123", "function": {"name": "test"}}],
+ },
+ {"role": "tool", "tool_call_id": "123", "content": "result"},
+ ]
+
+ assert any_assistant_message_has_thinking_blocks(messages) is False
+
+
+def test_any_assistant_message_has_thinking_blocks_empty_list():
+ """Test that function returns False when thinking_blocks is an empty list."""
+ from litellm.utils import any_assistant_message_has_thinking_blocks
+
+ messages = [
+ {"role": "user", "content": "Hello"},
+ {
+ "role": "assistant",
+ "thinking_blocks": [], # Empty list
+ "tool_calls": [{"id": "123", "function": {"name": "test"}}],
+ },
+ ]
+
+ assert any_assistant_message_has_thinking_blocks(messages) is False
+
+
+def test_last_assistant_with_tool_calls_has_no_thinking_blocks_issue_18926():
+ """
+ Test the scenario from issue #18926 where:
+ - First assistant message HAS thinking_blocks
+ - Second assistant message has NO thinking_blocks
+
+ The old logic would drop thinking because the LAST tool_call message
+ has no thinking_blocks, but this breaks because the first message
+ still has thinking blocks in the conversation.
+ """
+ from litellm.utils import (
+ any_assistant_message_has_thinking_blocks,
+ last_assistant_with_tool_calls_has_no_thinking_blocks,
+ )
+
+ messages = [
+ {"role": "user", "content": "Build a feature"},
+ {
+ "role": "assistant",
+ "thinking_blocks": [
+ {"type": "thinking", "thinking": "Let me analyze the requirements..."}
+ ],
+ "tool_calls": [
+ {"id": "toolu_1", "function": {"name": "file_editor", "arguments": "{}"}}
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "toolu_1",
+ "content": "File contents here...",
+ },
+ {
+ "role": "assistant",
+ # NO thinking_blocks - Claude sometimes doesn't include them
+ "content": [{"type": "text", "text": "Let me explore more..."}],
+ "tool_calls": [
+ {"id": "toolu_2", "function": {"name": "file_editor", "arguments": "{}"}}
+ ],
+ },
+ ]
+
+ # Last assistant with tool_calls has no thinking_blocks
+ assert last_assistant_with_tool_calls_has_no_thinking_blocks(messages) is True
+
+ # But ANY assistant message has thinking_blocks
+ assert any_assistant_message_has_thinking_blocks(messages) is True
+
+ # So we should NOT drop thinking - the combination tells us thinking is in use
+ # The fix uses both checks: only drop if last has none AND no message has any
+ should_drop_thinking = (
+ last_assistant_with_tool_calls_has_no_thinking_blocks(messages)
+ and not any_assistant_message_has_thinking_blocks(messages)
+ )
+ assert should_drop_thinking is False