Skip to content

Commit e865a4c

Browse files
authored
fix(factory.py): Add reasoning content handling for missing assistant… (#10688)
* fix(factory.py): Add reasoning content handling for missing assistant content * fix(factory.py): Improve handling of thinking blocks for assistant content * test(factory.py): Add test for Bedrock processing of thinking blocks with None content
1 parent 0eb0cf4 commit e865a4c

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

litellm/litellm_core_utils/prompt_templates/factory.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3040,6 +3040,19 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915
30403040
)
30413041
)
30423042
_assistant_content = assistant_message_block.get("content", None)
3043+
thinking_blocks = cast(
3044+
Optional[List[ChatCompletionThinkingBlock]],
3045+
assistant_message_block.get("thinking_blocks"),
3046+
)
3047+
3048+
if thinking_blocks is not None:
3049+
converted_thinking_blocks = BedrockConverseMessagesProcessor.translate_thinking_blocks_to_reasoning_content_blocks(
3050+
thinking_blocks
3051+
)
3052+
assistant_content = BedrockConverseMessagesProcessor.add_thinking_blocks_to_assistant_content(
3053+
thinking_blocks=converted_thinking_blocks,
3054+
assistant_parts=assistant_content,
3055+
)
30433056

30443057
if _assistant_content is not None and isinstance(
30453058
_assistant_content, list
@@ -3053,7 +3066,10 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915
30533066
cast(ChatCompletionThinkingBlock, element)
30543067
]
30553068
)
3056-
assistants_parts.extend(thinking_block)
3069+
assistants_parts = BedrockConverseMessagesProcessor.add_thinking_blocks_to_assistant_content(
3070+
thinking_blocks=thinking_block,
3071+
assistant_parts=assistants_parts,
3072+
)
30573073
elif element["type"] == "text":
30583074
assistants_part = BedrockContentBlock(
30593075
text=element["text"]

tests/litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from litellm.litellm_core_utils.prompt_templates.factory import (
88
BAD_MESSAGE_ERROR_STR,
99
ollama_pt,
10+
BedrockConverseMessagesProcessor,
1011
)
1112

12-
1313
def test_ollama_pt_simple_messages():
1414
"""Test basic functionality with simple text messages"""
1515
messages = [
@@ -43,6 +43,41 @@ def test_ollama_pt_consecutive_user_messages():
4343
assert isinstance(result, dict)
4444
assert result["prompt"] == expected_prompt
4545

46+
@pytest.mark.asyncio
47+
async def test_anthropic_bedrock_thinking_blocks_with_none_content():
48+
"""
49+
Test the specific function that processes thinking_blocks when content is None
50+
"""
51+
mock_assistant_message = {
52+
"content": "None", # content is None
53+
"role": "assistant",
54+
"thinking_blocks": [
55+
{
56+
"type": "thinking",
57+
"thinking": "This is a test thinking block",
58+
"signature": "test-signature"
59+
}
60+
],
61+
"reasoning_content": "This is the reasoning content"
62+
}
63+
messages = [
64+
{"role": "user", "content": "What is the capital of France?"},
65+
mock_assistant_message
66+
]
67+
68+
# test _bedrock_converse_messages_pt_async
69+
result = await BedrockConverseMessagesProcessor._bedrock_converse_messages_pt_async(
70+
messages=messages,
71+
model="us.anthropic.claude-3-7-sonnet-20250219-v1:0",
72+
llm_provider="bedrock"
73+
)
74+
75+
76+
# verify the result
77+
assert len(result) == 2
78+
assert result[1]["content"][0]["reasoningContent"]["reasoningText"]["text"] == "This is a test thinking block"
79+
80+
4681

4782
# def test_ollama_pt_consecutive_system_messages():
4883
# """Test handling consecutive system messages"""

0 commit comments

Comments
 (0)