fix(ai): prevent message parts carryover in streaming responses #8228
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.
Issue
#8227
Summary
Fixes a bug where parts from previous messages were carried over to subsequent messages when multiple assistant messages arrived in the same HTTP streaming response. This resulted in message contamination where later messages contained both their own parts and parts from earlier messages.
Changes
Core Fix
createFreshAssistantMessage()
: Creates a fresh assistant message with empty parts arrayresetActiveStreamingParts()
: Resets all active streaming parts to prevent carryoverstart
case handling inprocessUIMessageStream()
to detect new messages and reset state appropriatelyTest Coverage
multiple messages in same HTTP response
that reproduces the exact scenario described in the issueTechnical Details
Before (Problematic Behavior)
After (Fixed Behavior)
Example Scenario Fixed
Input Stream:
Before (Buggy): Message 2 contained parts from Message 1 + its own parts
After (Fixed): Message 2 contains only its own parts
Please check if the PR fulfills the following requirements:
-->
pnpm changeset
in the project root)pnpm prettier-fix
in the project root)Related Issues