updates for summarization triggers#4377
Merged
Merged
Conversation
Contributor
|
Thanks for opening a docs PR, Mason Daugherty (@mdrxy)! When it's ready for review, please add the relevant reviewers:
|
Mason Daugherty (mdrxy)
added a commit
to langchain-ai/langchain
that referenced
this pull request
Jun 9, 2026
…nMiddleware` (#34576) Closes #34442 [Docs](langchain-ai/docs#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>
Contributor
|
Mintlify preview branch generated: Site preview: https://langchain-5e9cc07a-preview-mdrxys-1781047952-fedf90d.mintlify.app Important Preview links may take a few minutes to start working while the deployment finishes. Changed documentation pages (preview deep links): |
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.
depends on langchain-ai/langchain#34576