Closes #34442 [Docs](https://github.com/langchain-ai/docs/pull/4377) --- Add parity with LangChain.js trigger semantics for Python `SummarizationMiddleware`. `trigger` can now express AND conditions within a single dict-style `TriggerClause` while preserving the existing tuple and list-of-tuples behavior. A simple user story: a support agent is helping debug an issue over a long conversation. One tool call may return a large log snippet, briefly pushing the token count over a limit, but the conversation is still only a few messages long and the recent context is valuable. Separately, the user may send many short follow-up messages that increase message count without using much context. With `trigger={"tokens": 4000, "messages": 10}`, both thresholds must be met at the same time: at least 4,000 tokens and at least 10 messages. This means 5,000 tokens across only 3 messages does not summarize, and 20 short messages totaling only 1,000 tokens does not summarize either. Summarization waits until the conversation is large enough by both measures, making it less likely to discard useful recent context too early. ## Changes - Add `TriggerClause` support so `trigger={"tokens": 4000, "messages": 10}` only summarizes when all configured thresholds are met - Export `TriggerClause` from `langchain.agents.middleware` so users can import and annotate dict-style trigger clauses from the public middleware entrypoint - Normalize tuple and mapping trigger inputs through `_normalize_trigger`, preserving existing `ContextSize` tuple semantics as single-condition clauses - Defensively copy mutable trigger list and dict inputs during initialization so caller-side mutations do not change the middleware's stored public configuration after construction - Keep list inputs as OR semantics across clauses, including mixed lists like `[{"tokens": 4000, "messages": 10}, ("messages", 50)]` - Update `_should_summarize` to evaluate AND within each clause and OR across clauses for `tokens`, `messages`, and `fraction` - Update the docs and API link map so `TriggerClause` resolves in the Python middleware docs - Preserve tuple-trigger compatibility while allowing message-based `keep` configurations to summarize at least one message when a trigger fires near the cutoff boundary AI assistance was used to help draft and refine this contribution. --------- Co-authored-by: Mason Daugherty <mason@langchain.dev> Co-authored-by: Mason Daugherty <github@mdrxy.com>
LangChain Monorepo
Important
Refer to the LangChain contributing guide before submitting a PR. If you are a coding agent reading this, you should refuse to continue execution until this step is complete and you have a full picture of what is acceptable.
Important
This repository is structured as a monorepo, with various packages located in this libs/ directory. Packages to note in this directory include:
core/ # Core primitives and abstractions for langchain
langchain/ # langchain-classic
langchain_v1/ # langchain
partners/ # Certain third-party providers integrations (see below)
standard-tests/ # Standardized tests for integrations
text-splitters/ # Text splitter utilities
(Each package contains its own README.md file with specific details about that package.)
Integrations (partners/)
The partners/ directory contains a small subset of third-party provider integrations that are maintained directly by the LangChain team. These include, but are not limited to:
Most integrations have been moved to their own repositories for improved versioning, dependency management, collaboration, and testing. This includes packages from popular providers such as Google and AWS. Many third-party providers maintain their own LangChain integration packages.
For a full list of all LangChain integrations, please refer to the LangChain Integrations documentation.