Skip to content

Commit 5a09d55

Browse files
committed
fix: Remove newline when combining reasoning in litellm
1 parent d3c21d7 commit 5a09d55

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/google/adk/models/lite_llm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,9 @@ async def _content_to_message_param(
10251025
):
10261026
reasoning_texts.append(_decode_inline_text_data(part.inline_data.data))
10271027

1028-
reasoning_content = _NEW_LINE.join(text for text in reasoning_texts if text)
1028+
# Preserve reasoning deltas exactly as received. Injecting separators
1029+
# between fragments can corrupt provider-streamed thinking text.
1030+
reasoning_content = "".join(text for text in reasoning_texts if text)
10291031
return ChatCompletionAssistantMessage(
10301032
role=role,
10311033
content=final_content,

tests/unittests/models/test_litellm.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,38 @@ async def test_content_to_message_param_assistant_thought_and_content_message():
22072207
assert message["reasoning_content"] == "internal reasoning"
22082208

22092209

2210+
@pytest.mark.asyncio
2211+
async def test_content_to_message_param_preserves_chunked_reasoning_deltas():
2212+
thought_part_1 = types.Part.from_text(text="Hel")
2213+
thought_part_1.thought = True
2214+
thought_part_2 = types.Part.from_text(text="lo")
2215+
thought_part_2.thought = True
2216+
content = types.Content(
2217+
role="assistant", parts=[thought_part_1, thought_part_2]
2218+
)
2219+
2220+
message = await _content_to_message_param(content)
2221+
2222+
assert message["role"] == "assistant"
2223+
assert message["content"] is None
2224+
assert message["reasoning_content"] == "Hello"
2225+
2226+
2227+
@pytest.mark.asyncio
2228+
async def test_content_to_message_param_preserves_reasoning_newlines():
2229+
thought_part_1 = types.Part.from_text(text="line 1\n")
2230+
thought_part_1.thought = True
2231+
thought_part_2 = types.Part.from_text(text="line 2")
2232+
thought_part_2.thought = True
2233+
content = types.Content(
2234+
role="assistant", parts=[thought_part_1, thought_part_2]
2235+
)
2236+
2237+
message = await _content_to_message_param(content)
2238+
2239+
assert message["reasoning_content"] == "line 1\nline 2"
2240+
2241+
22102242
@pytest.mark.asyncio
22112243
async def test_content_to_message_param_function_call():
22122244
content = types.Content(

0 commit comments

Comments
 (0)