fix(summarization): tag summary LLM calls nostream to stop phantom stream messages (#2503)#3378
Open
18062706139fcz wants to merge 1 commit into
Open
Conversation
…ream messages (bytedance#2503) The SummarizationMiddleware runs its summary LLM call inside a before_model hook. Without a nostream tag the summary tokens were captured by LangGraph's messages-tuple stream callback and broadcast to the frontend as a phantom AI message. Generate a dedicated summary model copy tagged with "nostream" (merged on top of any existing tags such as "middleware:summarize" so RunJournal attribution is preserved) and override _create_summary / _acreate_summary to invoke it directly. This avoids temporarily swapping the shared self.model, which would otherwise leak the RunnableBinding across concurrent runs and break parent logic that inspects the raw model (profile / _get_ls_params). Add regression tests covering nostream tagging, concurrent-run isolation, raw model preservation, and existing-tag merge.
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.
Problem
Fixes #2503. The
SummarizationMiddlewareruns its summary LLM call inside abefore_modelhook. Without anostreamtag, the summary tokens are captured by LangGraph's messages-tuple stream callback and broadcast to the frontend as a phantom AI message.Fix
"nostream"so the LangGraph streaming handler skips it (TAG_NOSTREAM).middleware:summarize) instead of overwriting them, so RunJournal / tracing attribution is preserved (RunnableBinding.with_configdoes a shallow merge)._create_summary/_acreate_summaryto invoke the tagged model directly, rather than temporarily swapping the sharedself.model. The previous swap approach would leak theRunnableBindingacross concurrent runs and break parent logic that inspects the raw model (profile/_get_ls_params).Tests
Added regression tests covering:
nostreamtagging on the summary modelself.modelis never mutated mid-await)middleware:summarize+nostream, no duplicates)All tests pass:
pytest tests/test_summarization_middleware.py→ 28 passed.