Skip to content

fix(litellm): keep every text part of an assistant message - #7260

Open
paragrudani1 wants to merge 2 commits into
google:mainfrom
paragrudani1:fix/litellm-keep-all-assistant-text-parts
Open

paragrudani1 wants to merge 2 commits into
google:mainfrom
paragrudani1:fix/litellm-keep-all-assistant-text-parts

Conversation

@paragrudani1

@paragrudani1 paragrudani1 commented Sep 24, 2026 •

Copy link
Copy Markdown

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_param converts an assistant types.Content to a LiteLLM message. After _get_content builds the content, this block runs:

if final_content and isinstance(final_content, list):
  # when the content is a single text object, we can use it directly.
  # this is needed for ollama_chat provider which fails if content is a list
  first_content = final_content[0]
  if first_content["type"] == "text":
    final_content = first_content["text"]

The comment says "a single text object", but the length is never checked. _get_content already 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. StreamingResponseAggregator in 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:

content = types.Content(role="model", parts=[
    types.Part.from_text(text="Before."),
    thought_part,                      # Part(text="...", thought=True)
    types.Part.from_text(text="After."),
])
await _content_to_message_param(content)
# {'role': 'assistant', 'content': 'Before.', ...}   <- "After." is gone

Solution:
Join every text block with a newline (_NEW_LINE, the same separator _flatten_ollama_content uses) instead of keeping only the first. The result is still a plain string, so the original reason for the collapse still holds: ollama_chat needs 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 (including test_content_to_message_param_multipart_content) is preserved. Only the dropped text is restored.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

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 to reasoning_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 through LiteLlm.generate_content_async, asserting the messages payload passed to acompletion.

On current main without the fix, all four fail, e.g.:

E     AssertionError: assert 'Before.' == 'Before.\nAfter.'

With the fix, pytest tests/unittests/models/test_litellm.py:

482 passed, 2 warnings

I ran this on Python 3.11 only; I did not run the full tox matrix. pyink and isort leave both files unchanged.

Manual End-to-End (E2E) Tests:

The end-to-end unit test above drives LiteLlm.generate_content_async with a mocked acompletion and checks the exact provider payload. We found the issue while reviewing sessions for a production agent that uses LiteLLM (DeepSeek) alongside Gemini models.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Related but separate: streamed reasoning fragmentation in LiteLlm was fixed in #6896 (not yet in a release). That change does not touch this conversion path.

@google-cla

google-cla Bot commented Sep 24, 2026

Copy link
Copy Markdown

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
paragrudani1 force-pushed the fix/litellm-keep-all-assistant-text-parts branch from 9f7fb2b to a2e7d41 Compare September 24, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants