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(bedrock-converse): drop blank-text fallback for empty thinking blocks (#27850)

Mateo Wang93706bfb9a94
diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py
index abe9e016e26..d40ca4e3597 100644
--- a/litellm/litellm_core_utils/prompt_templates/factory.py
+++ b/litellm/litellm_core_utils/prompt_templates/factory.py
@@ -4977,8 +4977,9 @@ class BedrockConverseMessagesProcessor:
)
if reasoning_text and not reasoning_text.get("signature"):
reasoning_text_text = reasoning_text["text"]
- assistants_part = BedrockContentBlock(text=reasoning_text_text)
- assistant_parts.append(assistants_part)
+ if reasoning_text_text.strip():
+ assistants_part = BedrockContentBlock(text=reasoning_text_text)
+ assistant_parts.append(assistants_part)
else:
filtered_thinking_blocks.append(block)
if len(filtered_thinking_blocks) > 0:
diff --git a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
index 27a3ddb553d..6c8f7585abb 100644
--- a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
+++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
@@ -91,6 +91,81 @@ async def test_anthropic_bedrock_thinking_blocks_with_none_content():
)
+def test_bedrock_converse_assistant_with_empty_thinking_block_and_tool_calls():
+ """
+ Regression: Claude Code (with extended thinking enabled) replays prior
+ assistant turns that include an empty thinking block alongside tool_use
+ blocks, e.g.
+
+ content=[
+ {"type": "text", "text": ""},
+ {"type": "thinking", "thinking": "", "signature": ""},
+ {"type": "tool_use", ...},
+ ]
+
+ After the Anthropic→OpenAI adapter, this becomes assistant message with
+ content="" and thinking_blocks=[{thinking:"", signature:""}] plus
+ tool_calls. The Bedrock Converse fallback for unsigned reasoning content
+ was emitting `BedrockContentBlock(text="")`, which Bedrock rejects with:
+
+ "The text field in the ContentBlock object at messages.X.content.0
+ is blank."
+
+ Verify no blank-text ContentBlocks are produced.
+ """
+ messages = [
+ {"role": "user", "content": "tell me about this repo"},
+ {
+ "role": "assistant",
+ "content": "",
+ "thinking_blocks": [
+ {"type": "thinking", "thinking": "", "signature": ""},
+ ],
+ "tool_calls": [
+ {
+ "id": "tooluse_aC8Izm8kl5DqVkgLA4XqcH",
+ "type": "function",
+ "function": {"name": "Bash", "arguments": '{"command": "ls"}'},
+ },
+ {
+ "id": "tooluse_31BEsgAjDwZxsUofwmdVPS",
+ "type": "function",
+ "function": {"name": "Bash", "arguments": '{"command": "pwd"}'},
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "tooluse_aC8Izm8kl5DqVkgLA4XqcH",
+ "content": "file1\nfile2",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "tooluse_31BEsgAjDwZxsUofwmdVPS",
+ "content": "/repo",
+ },
+ ]
+
+ result = _bedrock_converse_messages_pt(
+ messages=messages,
+ model="us.anthropic.claude-opus-4-7",
+ llm_provider="bedrock",
+ )
+
+ assistant_blocks = [m for m in result if m["role"] == "assistant"]
+ assert len(assistant_blocks) == 1
+ for block in assistant_blocks[0]["content"]:
+ if "text" in block:
+ assert block[
+ "text"
+ ].strip(), (
+ f"Bedrock Converse rejects blank-text ContentBlocks; got {block!r}"
+ )
+ # toolUse blocks must still be present
+ tool_use_blocks = [b for b in assistant_blocks[0]["content"] if "toolUse" in b]
+ assert len(tool_use_blocks) == 2
+
+
def test_convert_to_azure_openai_messages():
"""Test coverting image_url to azure_openai spec"""