fix(litellm): keep every text part of an assistant message - #7260
Open
paragrudani1 wants to merge 2 commits into
Open
paragrudani1 wants to merge 2 commits into
paragrudani1 wants to merge 2 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
`_content_to_message_param` collapses a list of assistant content blocks to the text of the first block. `_get_content` already returns a plain string for a single text part, so the collapse only runs for multi-part content, and there it silently drops every text part after the first. ADK produces such content itself: progressive SSE streaming stores interleaved thinking as text, thought, text, so a turn continued on a LiteLLM model loses the text after the thought. Join every text block with a newline instead. The result is still a plain string, so ollama_chat and OpenAI-style assistant content keep working, and non-text blocks are handled exactly as before.
paragrudani1
force-pushed
the
fix/litellm-keep-all-assistant-text-parts
branch
from
September 24, 2026 08:22
9f7fb2b to
a2e7d41
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
2. Or, if no issue exists, describe the change:
Problem:
_content_to_message_paramconverts an assistanttypes.Contentto a LiteLLM message. After_get_contentbuilds the content, this block runs:The comment says "a single text object", but the length is never checked.
_get_contentalready returns a plain string for a single text part, so this block only ever runs for multi-part content, and there it keeps the first text block and silently drops the rest.ADK produces multi-part assistant content itself.
StreamingResponseAggregatorin progressive SSE mode starts a new text part whenever the text type changes between thought and regular (streaming_utils.py,process_response). So a Gemini turn with interleaved thinking is stored as[text, thought, text]. When the session continues on a LiteLLM model (a per-agent or per-run model switch), the provider receives only the text before the thought. Content returned from callbacks with several text parts is truncated the same way.Minimal repro on current
main:Solution:
Join every text block with a newline (
_NEW_LINE, the same separator_flatten_ollama_contentuses) instead of keeping only the first. The result is still a plain string, so the original reason for the collapse still holds:ollama_chatneeds a string, and OpenAI-style APIs accept only text in assistant content. The condition and the handling of non-text blocks are unchanged, so existing behaviour (includingtest_content_to_message_param_multipart_content) is preserved. Only the dropped text is restored.Testing Plan
Unit Tests:
New tests in
tests/unittests/models/test_litellm.py:test_content_to_message_param_assistant_keeps_every_text_part: two text parts → both sent.test_content_to_message_param_assistant_text_around_thought: the progressive-streaming[text, thought, text]shape; the text after the thought is kept and the thought still goes toreasoning_content.test_content_to_message_param_assistant_joins_text_around_media:[text, image, text]→ both text blocks kept.test_generate_content_async_sends_full_multi_part_assistant_turn: end to end throughLiteLlm.generate_content_async, asserting themessagespayload passed toacompletion.On current
mainwithout the fix, all four fail, e.g.:With the fix,
pytest tests/unittests/models/test_litellm.py:I ran this on Python 3.11 only; I did not run the full
toxmatrix.pyinkandisortleave both files unchanged.Manual End-to-End (E2E) Tests:
The end-to-end unit test above drives
LiteLlm.generate_content_asyncwith a mockedacompletionand checks the exact provider payload. We found the issue while reviewing sessions for a production agent that uses LiteLLM (DeepSeek) alongside Gemini models.Checklist
Additional context
Related but separate: streamed reasoning fragmentation in
LiteLlmwas fixed in #6896 (not yet in a release). That change does not touch this conversion path.