[v6.0] fix(langchain): reset streamEvents text between model invocations#16789
Merged
Conversation
…5254) ## Background When `toUIMessageStream` consumes LangGraph **`streamEvents`** (v2), `processStreamEventsEvent` tracks streaming text with `textStarted` / `textMessageId` (and reasoning with `reasoningStarted` / `reasoningMessageId`). Those flags were only cleared at the end of the whole stream, not when a **new** chat model run started. If a graph performs multiple LLM calls in one run (e.g. tool use then model again), **`on_chat_model_start`** can fire again while `textStarted` is still `true`. Subsequent **`text-delta`** chunks then reuse the first text part’s id, so later prose appends to that first block. Tool events still append **new** tool parts after it, so the assistant `UIMessage`’s `parts` no longer reflect chronological order in the UI. ## Summary - On **`on_chat_model_start`**, if the previous model turn left streams open, emit **`reasoning-end`** and **`text-end`** with the correct ids, then reset `reasoningStarted` / `reasoningMessageId` and `textStarted` / `textMessageId`. - After that, set **`messageId`** from `event.run_id` or `event.data.run_id` as before. This matches the intent of ending open streams when a new model invocation begins, so the next **`text-start`** / **`reasoning-start`** pair applies to the new turn. ## Manual Verification - Use a LangGraph agent with **`streamEvents`** (v2) that **invokes the chat model more than once** in the same run (e.g. tools then model again), with text on both segments. - Pipe the iterator through **`toUIMessageStream`** and consume the UI message stream. - Confirm the resulting assistant message has **multiple text parts in order** (one per model segment) and that **tool parts** appear in the order events occurred, not all text merged into the first text part. ## Checklist - [ ] All commits are signed (PRs with unsigned commits cannot be merged) - [ ] Tests have been added / updated (for bug fixes / features) - [ ] Documentation has been added / updated (for bug fixes / features) - [ ] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review) ## Future Work Optional: add an automated test that simulates two `on_chat_model_start` / `on_chat_model_stream` sequences and asserts the emitted chunk sequence includes **`text-end`** before the second segment’s **`text-start`**. --------- Co-authored-by: Lars Grammel <lars.grammel@gmail.com> (cherry picked from commit 3711955)
Contributor
|
|
Contributor
|
🚀 Published in:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport of #15254 to
release-v6.0(clean cherry-pick).on_chat_model_startdid not resettextStarted/reasoningStarted, so multi-invocation streamEvents runs appended later text to the first text part.Part of the v6.0 backport tracking issue #16767.