Skip to content

fix(litellm): report a finish-less text stream as truncated, not stopped - #7741

Closed
xxwtiancai wants to merge 2 commits into
google:mainfrom
xxwtiancai:fix/litellm-stream-truncation-7693
Closed

xxwtiancai wants to merge 2 commits into
google:mainfrom
xxwtiancai:fix/litellm-stream-truncation-7693

Conversation

@xxwtiancai

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Refs #7693 — this lands the ADK-side half of the direction agreed with @surajksharma07 in the issue: harden the stream finalization fallback to FinishReason.OTHER + error_code, while the litellm synthetic-stop substitution is raised upstream separately.

The defect. When a stream ends without ever delivering a finish_reason chunk, the post-loop finalizers defaulted the reason to "stop", so buffered text was finalized as a clean STOP with no error — a cut-off answer looked complete to the agent, callbacks and telemetry. On live litellm this is masked by litellm substituting a synthetic stop (verified on 1.85.7 and 1.106.2, see the probe in the issue), but the fallback is reachable through custom LiteLlm.llm_client implementations that stream raw chunks — which the maintainer also reproduced on 2.13.0 / litellm 1.84.0 — and older litellm versions end hard drops the same way.

The fix maps the finish-less text finalization to _STREAM_ENDED_WITHOUT_FINISH_REASON, an unmapped sentinel that resolves to FinishReason.OTHER, so the existing _apply_provider_finish_reason convention stamps error_code plus an explicit message ("The stream ended without a finish reason; the response may be truncated.") instead of a clean stop. Deliberately unchanged, each pinned by existing tests:

  • Tool-call streams keep their own completeness discriminator — whether the accumulated arguments parse. Complete arguments with a missing terminal chunk still yield the finished call (test_streaming_tool_call_cut_off_after_complete_arguments_is_kept); unparseable arguments already report MALFORMED_FUNCTION_CALL through the existing truncation branch.
  • The empty-stream default stays a clean stop (test_generate_content_async_stream_with_only_stop_finish_reason[None]): with nothing buffered there is no partial answer to report as truncated.
  • A finish chunk followed by a usage-only chunk keeps its clean stop; the sentinel only applies when no finish reason was ever seen for the segment.

No public API changes.

Testing Plan

Unit tests and checks:

  • New cases in tests/unittests/models/test_litellm.py, all with mocked streams as requested:
    • test_generate_content_async_stream_without_finish_reason_reports_truncated_text: two text deltas, no finish chunk → single finalized response keeps the buffered text, finish_reason=OTHER, error_code=OTHER, explicit message. Fails on the unpatched base (clean STOP, no error).
    • test_generate_content_async_stream_trailing_usage_chunk_keeps_stop: stop chunk followed by a usage-only chunk (the litellm pattern) → clean STOP with no error, unchanged. Control passes on base too.
  • Revert control: with only the production file restored to upstream main, the truncated-text case fails and the trailing-usage control passes, exactly as claimed above.
  • Full tests/unittests/models/test_litellm.py on the PR head: 770 passed, 0 failed — including the pinned tool-call and empty-stream contracts listed above.
  • Pre-commit on both changed files and mypy on the source file: clean.

Manual end-to-end tests

The issue's live-server probe (local OpenAI-compatible SSE server that closes without a finish chunk) was validated earlier in the issue thread: on litellm 1.106.2 and 1.85.7 the graceful-close path is masked by litellm's synthetic stop, so live end-to-end behavior for stock litellm configurations is unchanged by this PR by construction — the contract change is observable to custom llm_client streams and to any future litellm that stops substituting, which the mocked-stream tests pin directly.

A stream that never delivers a finish_reason chunk finalized its
buffered text as a clean STOP, so a cut-off answer looked complete to
the agent, callbacks and telemetry. litellm usually hides this by
substituting a synthetic stop, but the fallback is reachable through
custom llm_client implementations streaming raw chunks, and older
litellm versions end hard drops the same way. Finalize that case as
FinishReason.OTHER with error_code and an explicit message. Tool-call
streams keep their existing completeness discriminator (argument
parseability) and the empty-stream default stays a clean stop.

Refs google#7693 (ADK-side half; the litellm synthetic-stop substitution
needs an upstream litellm fix as discussed in the issue).
copybara-service Bot pushed a commit that referenced this pull request Nov 4, 2026
Merge #7741

When a LiteLlm stream buffered text or reasoning but never delivered a
finish_reason, the final response was a clean STOP. It now keeps the text
or reasoning but reports finish_reason OTHER with error_code OTHER and a
message saying the response may be truncated, so callers that act on
error_code see it as an error.
Stock litellm usually substitutes a stop chunk itself, so this mainly shows
for custom llm_client streams. Tool-call streams and empty streams are
unchanged.

Part of #7693

PiperOrigin-RevId: 1013946473
@adk-bot

adk-bot commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Thank you @xxwtiancai for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit f1f63d3.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Nov 4, 2026
@adk-bot adk-bot closed this Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants