Skip to content

fix(streaming): don't attach finish_reason to content-bearing chunk - #462

Merged
EnjoyBacon7 merged 1 commit into
mainfrom
fix/streaming-finish-reason-on-content-chunk
Jun 12, 2026
Merged

fix(streaming): don't attach finish_reason to content-bearing chunk#462
EnjoyBacon7 merged 1 commit into
mainfrom
fix/streaming-finish-reason-on-content-chunk

Conversation

@EnjoyBacon7

@EnjoyBacon7 EnjoyBacon7 commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

stream_with_source_filtering (openrag/components/utils.py) emits the final
content of a streamed chat completion in a chunk that also carries
finish_reason: "stop"
.

At end-of-stream the function builds the tail chunk via
copy.deepcopy(chunk_template), and chunk_template at that point is the
upstream finish chunk (the one whose choices[0].finish_reason is set).
The resulting tail chunk therefore contains both the remaining delta.content
and finish_reason: "stop":

[0] finish_reason='stop'  content="Bonjour ! Comment puis-je vous aider ..."   <-- both
[1] finish_reason='stop'  content=None
[2] [DONE]

Spec-compliant OpenAI streaming clients treat a chunk with a non-null
finish_reason as terminal and ignore its delta. Such clients (e.g. the
Twake Workplace RAG integration) therefore drop the tail content:

  • short answers render blank (the whole answer fits in the held-back
    look-ahead window and is flushed only in the finish-tagged tail chunk);
  • long answers lose their final ~look-ahead window of text (silent
    truncation).

The HTTP response is still 200, so the regression is invisible server-side.
Behaviour was correct in 1.1.7, which emitted content chunks with
finish_reason: null and a separate empty finish chunk.

Fix

Clear finish_reason on the content-bearing tail chunk. The separate finish
chunk that immediately follows still emits finish_reason with an empty
delta, matching the OpenAI streaming contract.

[0] finish_reason=None    content="Bonjour ! Comment puis-je vous aider ..."
[1] finish_reason='stop'  content=None
[2] [DONE]

Verification

  • Short answer: full content delivered in a non-terminal chunk.
  • Long answer (202 chunks): 0 content-chunks-carrying-finish_reason, single
    finish chunk, complete text (no truncation), sources still delivered in
    extra.
  • Non-streaming path unaffected.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed streamed response termination so trailing content is no longer mistaken for the final signal. Streams now emit the content tail separately from the final termination notification, ensuring clients do not prematurely close and the final finish message carries the correct filtered metadata.

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@EnjoyBacon7, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 3 minutes and 23 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d95ec5c8-f6ab-4bcb-a025-06e1b00e1794

📥 Commits

Reviewing files that changed from the base of the PR and between 7d45bd2 and 714f2a8.

📒 Files selected for processing (1)
  • openrag/components/utils.py
📝 Walkthrough

Walkthrough

The PR nullifies choices[0]["finish_reason"] on the final content tail chunk in stream_with_source_filtering during the [DONE] path so the tail chunk is non-terminal; a separate finish chunk still carries the terminal finish_reason.

Changes

Stream finish reason handling fix

Layer / File(s) Summary
Tail chunk finish reason separation
openrag/components/utils.py
When emitting the final tail chunk during [DONE] handling, choices[0].finish_reason is now explicitly set to None so the chunk carries only delta content while a separate finish chunk later provides the terminal finish_reason.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • linagora/openrag#257: Both PRs modify stream_with_source_filtering end-of-stream chunk emission logic, specifically how the buffered tail content and finish chunk terminal signaling are handled.

Suggested labels

fix

Suggested reviewers

  • paultranvan

Poem

🐰 I nudged the tail to speak, then hush —
The finish waits, no final rush,
Content hops out, calm and clear,
The terminal note comes after, near,
A rabbit's nod to proper flow ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: preventing finish_reason from being attached to content-bearing chunks in streaming responses.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/streaming-finish-reason-on-content-chunk

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot added the fix Fix issue label Jun 12, 2026
@EnjoyBacon7
EnjoyBacon7 force-pushed the fix/streaming-finish-reason-on-content-chunk branch 2 times, most recently from 4a18e95 to 7d45bd2 Compare June 12, 2026 13:22
stream_with_source_filtering builds the final content chunk by deep-copying
chunk_template, which at end-of-stream is the upstream *finish* chunk and so
carries finish_reason. The emitted tail chunk therefore contained both content
and finish_reason=stop.

Spec-compliant OpenAI streaming clients treat a chunk with a non-null
finish_reason as terminal and ignore its delta. Such clients (e.g. the Twake
Workplace RAG integration) drop the tail content: short answers render blank,
long answers lose their final ~lookahead-window of text. The server still
returns 200, so the regression is invisible server-side.

Clear finish_reason on the content-bearing tail chunk; the separate finish
chunk that follows still emits finish_reason with an empty delta, matching the
OpenAI streaming contract.
@EnjoyBacon7
EnjoyBacon7 force-pushed the fix/streaming-finish-reason-on-content-chunk branch from 7d45bd2 to 714f2a8 Compare June 12, 2026 13:37
@EnjoyBacon7
EnjoyBacon7 merged commit baf9fb1 into main Jun 12, 2026
4 checks passed
@EnjoyBacon7
EnjoyBacon7 deleted the fix/streaming-finish-reason-on-content-chunk branch June 12, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Fix issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant