Skip to content

updates for summarization triggers#4377

Merged
Mason Daugherty (mdrxy) merged 1 commit into
mainfrom
mdrxy/summarization-trigger
Jun 9, 2026
Merged

updates for summarization triggers#4377
Mason Daugherty (mdrxy) merged 1 commit into
mainfrom
mdrxy/summarization-trigger

Conversation

@mdrxy

Copy link
Copy Markdown
Member

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for opening a docs PR, Mason Daugherty (@mdrxy)! When it's ready for review, please add the relevant reviewers:

  • @npentrel or @lnhsingh (LangChain)

@github-actions github-actions Bot added internal docs-infra langchain For docs changes to LangChain oss python For content related to the Python version of LangChain projects labels Jun 9, 2026
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>
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Mintlify preview branch generated: preview-mdrxys-1781047952-fedf90d

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):

@mdrxy Mason Daugherty (mdrxy) merged commit a2d02aa into main Jun 9, 2026
25 checks passed
@mdrxy Mason Daugherty (mdrxy) deleted the mdrxy/summarization-trigger branch June 9, 2026 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-infra internal langchain For docs changes to LangChain oss python For content related to the Python version of LangChain projects

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant