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(factory.py): Add reasoning content handling for missing assistant… (#10688)

LouisSharke865a4cddae5
diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py
index ced66528c97..15100b5a842 100644
--- a/litellm/litellm_core_utils/prompt_templates/factory.py
+++ b/litellm/litellm_core_utils/prompt_templates/factory.py
@@ -3040,6 +3040,19 @@ class BedrockConverseMessagesProcessor:
)
)
_assistant_content = assistant_message_block.get("content", None)
+ thinking_blocks = cast(
+ Optional[List[ChatCompletionThinkingBlock]],
+ assistant_message_block.get("thinking_blocks"),
+ )
+
+ if thinking_blocks is not None:
+ converted_thinking_blocks = BedrockConverseMessagesProcessor.translate_thinking_blocks_to_reasoning_content_blocks(
+ thinking_blocks
+ )
+ assistant_content = BedrockConverseMessagesProcessor.add_thinking_blocks_to_assistant_content(
+ thinking_blocks=converted_thinking_blocks,
+ assistant_parts=assistant_content,
+ )
if _assistant_content is not None and isinstance(
_assistant_content, list
@@ -3053,7 +3066,10 @@ class BedrockConverseMessagesProcessor:
cast(ChatCompletionThinkingBlock, element)
]
)
- assistants_parts.extend(thinking_block)
+ assistants_parts = BedrockConverseMessagesProcessor.add_thinking_blocks_to_assistant_content(
+ thinking_blocks=thinking_block,
+ assistant_parts=assistants_parts,
+ )
elif element["type"] == "text":
assistants_part = BedrockContentBlock(
text=element["text"]
diff --git a/tests/litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py b/tests/litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
index de0c27d0f43..f78b933c92e 100644
--- a/tests/litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
+++ b/tests/litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
@@ -7,9 +7,9 @@ import litellm
from litellm.litellm_core_utils.prompt_templates.factory import (
BAD_MESSAGE_ERROR_STR,
ollama_pt,
+ BedrockConverseMessagesProcessor,
)
-
def test_ollama_pt_simple_messages():
"""Test basic functionality with simple text messages"""
messages = [
@@ -43,6 +43,41 @@ def test_ollama_pt_consecutive_user_messages():
assert isinstance(result, dict)
assert result["prompt"] == expected_prompt
+async def test_anthropic_bedrock_thinking_blocks_with_none_content():
+ """
+ Test the specific function that processes thinking_blocks when content is None
+ """
+ mock_assistant_message = {
+ "content": "None", # content is None
+ "role": "assistant",
+ "thinking_blocks": [
+ {
+ "type": "thinking",
+ "thinking": "This is a test thinking block",
+ "signature": "test-signature"
+ }
+ ],
+ "reasoning_content": "This is the reasoning content"
+ }
+ messages = [
+ {"role": "user", "content": "What is the capital of France?"},
+ mock_assistant_message
+ ]
+
+ # test _bedrock_converse_messages_pt_async
+ result = await BedrockConverseMessagesProcessor._bedrock_converse_messages_pt_async(
+ messages=messages,
+ model="us.anthropic.claude-3-7-sonnet-20250219-v1:0",
+ llm_provider="bedrock"
+ )
+
+
+ # verify the result
+ assert len(result) == 2
+ assert result[1]["content"][0]["reasoningContent"]["reasoningText"]["text"] == "This is a test thinking block"
+
+
# def test_ollama_pt_consecutive_system_messages():
# """Test handling consecutive system messages"""