fix(langchain): isolate middleware model calls from parent streaming …#35339
Draft
NITIN Madan (Nitin75408) wants to merge 1 commit intolangchain-ai:masterfrom
Draft
fix(langchain): isolate middleware model calls from parent streaming …#35339NITIN Madan (Nitin75408) wants to merge 1 commit intolangchain-ai:masterfrom
NITIN Madan (Nitin75408) wants to merge 1 commit intolangchain-ai:masterfrom
Conversation
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.
Summary
_get_internal_model_config()static method toAgentMiddlewarebase class that returns a
RunnableConfigwithcallbacks=[]toprevent streaming callback inheritance from the parent agent context
SummarizationMiddlewareto use isolated config in both_create_summary()and_acreate_summary()LLMToolSelectorMiddlewareto use isolated config in bothwrap_model_call()andawrap_model_call()(previously passed noconfig at all)
Fixes #34382
Why
When
astream()runs,var_child_runnable_configis set with streamingcallbacks.
ensure_config()additively merges this into any config thatdoesn't explicitly override
callbacks. This caused internal middlewaremodel calls to emit tokens into the parent stream — breaking stream
correctness and isolation.
The fix passes
callbacks=[]whichensure_config()merges on top ofthe inherited config, clearing the streaming callbacks. This is backward
compatible: non-streaming usage already has no callbacks to clear.
Review notes
SummarizationMiddlewarepreviously passedconfig={"metadata": {...}}which preserved inherited callbacks. Now uses the isolated config.
LLMToolSelectorMiddlewarepreviously passed no config at all,fully inheriting parent callbacks. Now uses the isolated config.
_get_internal_model_config()method is intentionally private(
_-prefixed) since it's an implementation detail, but available tocustom middleware subclasses.