Skip to content

Conversation

@pawel-twardziak
Copy link

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 as SummarizationMiddleware.
It introduces ContextAwareAgentMiddleware[StateT, ContextT] and updates builtin middleware to extend it, so that all middleware share the same ContextT and 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 format
  • make lint
  • make test

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 commit introduces ContextAwareAgentMiddleware and updates builtin middleware to extend it
so they type-check correctly with custom runtime context.
@github-actions github-actions bot added langchain Related to the package `langchain` fix labels Dec 6, 2025
@pawel-twardziak
Copy link
Author

@sydney-runkle I mainly modified your code, take a look at my PR please.

@pawel-twardziak pawel-twardziak changed the title fix(agents): allow builtin middleware with custom context_schema fix(langchain): allow builtin middleware with custom context_schema Dec 6, 2025
@github-actions github-actions bot added fix and removed fix labels Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix langchain Related to the package `langchain`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant