Skip to content

fix(anthropic): preserve reasoning fallback with empty thinking blocks - #32105

Open
jesco-absolut wants to merge 5 commits into
BerriAI:mainfrom
jesco-absolut:litellm_fix_messages_stream_text_delta_transition
Open

jesco-absolut wants to merge 5 commits into
BerriAI:mainfrom
jesco-absolut:litellm_fix_messages_stream_text_delta_transition

Conversation

@jesco-absolut

@jesco-absolut jesco-absolut commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves

  • Empty or redacted thinking blocks hide usable reasoning

How it solves it

  • Falls back to reasoning when thinking payloads are unusable
  • Preserves usable thinking and signature precedence

User Flow

Before, an Anthropic-compatible client can lose reasoning while streaming

  1. Send POST http://localhost:4000/v1/messages with stream=true to a reasoning backend
  2. When the backend supplies reasoning alongside empty or redacted thinking blocks, no thinking delta reaches the client

After, the same request preserves that reasoning

  1. Send POST http://localhost:4000/v1/messages with stream=true to the same reasoning backend
  2. The client receives a thinking block and its reasoning delta

Relevant issues

Related to #30765 and #29441, following #31022. #35289 already merged the mixed-payload stream splitting behavior, so this PR only covers the remaining reasoning fallback

Validation

Rebased onto main at 2d896d57f28de591fe7a6c1fe360788ee47ee4dd on October 10. The rebase follows the adapter and unit-test directory moves and preserves the content-block method's compatibility alias

The helper now reads typed fields and builds immutable reasoning values, satisfying the current lint and type gates without suppressions or higher limits

uv run --no-sync pytest -q \
  tests/unit/llms/anthropic/pass_through/adapters/test_streaming_iterator_first_delta.py \
  tests/unit/llms/anthropic/pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
# 294 passed
make BASE_REF=2d896d57f28de591fe7a6c1fe360788ee47ee4dd check
# PASS, including Python lint, type gates, test-tree lint and test-quality gate
git diff --check

These are local unit and static checks, not real-provider end-to-end proof

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible
  • I have received a current Greptile Confidence Score of at least 4/5 before requesting maintainer review

Type

Bug Fix

Test

Caveats

Low

  • Real-provider before/after QA remains outstanding
  • CI, coverage and bot reviews must refresh after this push

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@jesco-absolut

Copy link
Copy Markdown
Contributor Author

@greptileai

@codecov

codecov Bot commented Jul 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@greptile-apps

greptile-apps Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a dropped-text regression that occurred when a provider streaming backend emitted a single LiteLLM chunk carrying both reasoning_content (or a populated thinking_blocks) and visible content at the same time. The _CombinedChunkSplitter._split method is extended to detect that "mixed payload" case and split it into a reasoning-only chunk followed by a response-only chunk before the Anthropic SSE state machine classifies the next content block, keeping thinking_delta on a thinking block and text_delta on a text block. The companion fix in transformation.py merges two independent elif branches into one so that thinking_blocks=[] (an empty list, not a missing attribute) no longer silently hides a co-present reasoning_content.

  • _CombinedChunkSplitter._split now produces up to three chunks from a single input: a reasoning-only chunk, a response-only chunk, and (when finish_reason is set) a finish-only chunk; four new static helpers extract the repeated delta-inspection logic.
  • transformation.py replaces two separate elif isinstance(choice, StreamingChoices) branches with one, making reasoning_content a fallthrough inside the same block so an empty thinking_blocks attribute cannot shadow it.
  • Tests add four new integration scenarios (sync + async) covering the mixed-chunk path, the thinking_blocks=[] edge case, and redacted_thinking coexistence with reasoning_content.

Confidence Score: 5/5

Safe to merge — the change is contained within the experimental pass-through adapter layer, all modified code paths are covered by the new and updated tests, and no existing assertions were weakened.

The logic in _split was carefully traced through all reachable flag combinations (mixed+finish, mixed-only, combined-only, passthrough) and produces correct, non-empty chunk lists in every case. The transformation.py restructuring correctly unifies the two previously independent elif branches without introducing new control-flow gaps. The only observation is _is_combined becoming dead production code, which is a documentation/cleanup concern, not a behavioral one.

streaming_iterator.py — _is_combined is now unreachable from _split and could be removed or documented to avoid future confusion.

Important Files Changed

Filename Overview
litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py Refactored _CombinedChunkSplitter._split to handle two distinct cases: (1) mixed reasoning+response payloads in a single chunk (no finish_reason required) and (2) content+finish_reason. New helper statics are clean. _is_combined is no longer called by _split but remains defined — now dead production code.
litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py Fixes two independent elif branches for thinking_blocks and reasoning_content into a single elif isinstance(choice, StreamingChoices): block with a nested if/elif fallthrough, so an empty thinking_blocks=[] no longer hides a co-present reasoning_content.
tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_combined_chunk.py Updated test_split_clears_reasoning_and_thinking_on_finish_chunk to expect the new 3-chunk split (reasoning → content → finish). Formatting-only changes elsewhere; no coverage is weakened.
tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_first_delta.py Adds 4 new integration-style tests covering mixed chunk splitting (sync/async), empty thinking_blocks fallthrough, and redacted_thinking + reasoning_content coexistence. New _thinking_deltas helper. No existing assertions changed.

Reviews (3): Last reviewed commit: "fix(anthropic): align reasoning stream f..." | Re-trigger Greptile

@jesco-absolut

Copy link
Copy Markdown
Contributor Author

@greptileai

@jinbagi

jinbagi commented Jul 7, 2026

Copy link
Copy Markdown

@yucheng-berri

@jesco-absolut
jesco-absolut force-pushed the litellm_fix_messages_stream_text_delta_transition branch from 31e984f to 227e3e6 Compare July 15, 2026 23:48
@codspeed

codspeed Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 31 untouched benchmarks


Comparing jesco-absolut:litellm_fix_messages_stream_text_delta_transition (02422da) with main (f57906c)

Open in CodSpeed

@jesco-absolut
jesco-absolut force-pushed the litellm_fix_messages_stream_text_delta_transition branch from 227e3e6 to 6caf2c3 Compare July 29, 2026 23:48
@jesco-absolut jesco-absolut changed the title fix(anthropic): split mixed reasoning and text stream chunks fix(anthropic): preserve reasoning fallback with empty thinking blocks Aug 1, 2026
@jesco-absolut
jesco-absolut force-pushed the litellm_fix_messages_stream_text_delta_transition branch from f0b1b5e to 0d7217c Compare August 1, 2026 03:47
@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:28
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 15:52
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 15:52
@jesco-absolut
jesco-absolut force-pushed the litellm_fix_messages_stream_text_delta_transition branch from e3fb8b1 to 02422da Compare October 10, 2026 07:00
@veria-ai

veria-ai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Reviews

This branch has not been deployed

No deployments
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.

3 participants