fix(langchain): allow builtin middleware with custom context_schema #34232
+45
−26
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
Pyright reported:
SummarizationMiddleware" is not assignable to "AgentMiddleware[AgentState[Unknown], Context]because ContextT is invariant and "None" is not the same as "Context" when using
context_schema=Context.This PR fixes Pyright type errors when using
create_agent(..., context_schema=Context)together with builtin middleware such asSummarizationMiddleware.It introduces
ContextAwareAgentMiddleware[StateT, ContextT]and updates builtin middleware to extend it, so that all middleware share the sameContextTand type-check correctly.Issue reference
N/A (no linked GitHub issue).
Breaking changes
None. Existing agent and middleware behavior is preserved; changes are limited to typing/generics and do not alter runtime semantics.
Dependencies on other PRs
None.
Testing
make formatmake lintmake test