fix(langchain): reset streamEvents text between model invocations#15254
Merged
Conversation
…end and text-end when the previous model turn left those streams open. Previously textStarted stayed true across LLM invocations, so later text-delta chunks appended to the first text part while new tool parts were appended afterward, breaking chronological order in the UI message.
dqbd
reviewed
May 20, 2026
|
|
||
| switch (event.event) { | ||
| case 'on_chat_model_start': { | ||
| case "on_chat_model_start": { |
Collaborator
|
Please add a patch changeset. |
lgrammel
approved these changes
Jun 3, 2026
Contributor
|
🚀 Published in:
|
gr2m
pushed a commit
that referenced
this pull request
Jun 5, 2026
…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>
This was referenced Jul 6, 2026
Closed
gr2m
added a commit
that referenced
this pull request
Jul 6, 2026
…ons (#16789) Backport of #15254 to `release-v6.0` (clean cherry-pick). `on_chat_model_start` did not reset `textStarted`/`reasoningStarted`, so multi-invocation streamEvents runs appended later text to the first text part. Part of the v6.0 backport tracking issue #16767. Co-authored-by: eyueldk <Eyueldk@gmail.com> Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
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.
Background
When
toUIMessageStreamconsumes LangGraphstreamEvents(v2),processStreamEventsEventtracks streaming text withtextStarted/textMessageId(and reasoning withreasoningStarted/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_startcan fire again whiletextStartedis stilltrue. Subsequenttext-deltachunks 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 assistantUIMessage’spartsno longer reflect chronological order in the UI.Summary
on_chat_model_start, if the previous model turn left streams open, emitreasoning-endandtext-endwith the correct ids, then resetreasoningStarted/reasoningMessageIdandtextStarted/textMessageId.messageIdfromevent.run_idorevent.data.run_idas before.This matches the intent of ending open streams when a new model invocation begins, so the next
text-start/reasoning-startpair applies to the new turn.Manual Verification
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.toUIMessageStreamand consume the UI message stream.Checklist
pnpm changesetin the project root)Future Work
Optional: add an automated test that simulates two
on_chat_model_start/on_chat_model_streamsequences and asserts the emitted chunk sequence includestext-endbefore the second segment’stext-start.