-
Notifications
You must be signed in to change notification settings - Fork 20.2k
Description
Checked other resources
- This is a feature request, not a bug report or usage question.
- I added a clear and descriptive title that summarizes the feature request.
- I used the GitHub search to find a similar feature request and didn't find it.
- I checked the LangChain documentation and API reference to see if this feature already exists.
- This is not related to the langchain-community package.
Package (Required)
- langchain
- langchain-openai
- langchain-anthropic
- langchain-classic
- langchain-core
- langchain-cli
- langchain-model-profiles
- langchain-tests
- langchain-text-splitters
- langchain-chroma
- langchain-deepseek
- langchain-exa
- langchain-fireworks
- langchain-groq
- langchain-huggingface
- langchain-mistralai
- langchain-nomic
- langchain-ollama
- langchain-perplexity
- langchain-prompty
- langchain-qdrant
- langchain-xai
- Other / not sure / general
Feature Description
In LangChain Python, SummarizationMiddleware(trigger=...) currently supports: (https://reference.langchain.com/python/langchain/middleware/#langchain.agents.middleware.SummarizationMiddleware)
- a single
ContextSizetuple like("tokens", 3000)or("messages", 50) - or a list of tuples, where summarization runs when any threshold is met (OR).
In LangChain.js, trigger supports AND conditions within a single trigger object (e.g. tokens >= 4000 AND messages >= 10), and OR across multiple trigger objects. https://reference.langchain.com/javascript/functions/langchain.index.summarizationMiddleware.html
I’d like Python SummarizationMiddleware(trigger=...) to support the same expressiveness (AND + OR), while remaining fully backward compatible.
Use Case
I want to avoid summarizing too early when only one metric spikes transiently.
Example: summarize only when both tokens and messages are high, e.g.
tokens >= 4000 AND messages >= 10
Also want combined OR cases, e.g.(tokens >= 5000 AND messages >= 3) OR (tokens >= 3000 AND messages >= 6)
Proposed Solution
Extend Python’s trigger parameter to accept either the existing tuple form or a dict-style “conjunction” form:
- Keep existing behavior unchanged:
trigger=("messages", 50)trigger=[("fraction", 0.8), ("messages", 100)]# OR semantics remains as documented
- Add AND-capable trigger object(s):
trigger={"tokens": 4000, "messages": 10}# AND across keystrigger=[{"tokens": 5000, "messages": 3}, {"tokens": 3000, "messages": 6}]# OR across dicts
- Implementation sketch (conceptual)
Normalizetriggerinto a list of “clauses”, where each clause is a dict of thresholds:- tuple
("tokens", 3000)⇒{"tokens": 3000} - dict
{"tokens": 4000, "messages": 10}⇒ as-is
Then evaluate: - a clause matches when all thresholds in that clause are met (AND)
- summarization triggers when any clause matches (OR)
This exactly matches LangChain.js semantics.
- tuple
Alternatives Considered
No response
Additional Context
No response