Skip to content

Add AND-capable trigger conditions to Python SummarizationMiddleware (parity with LangChain.js) #34442

@yasirali0

Description

@yasirali0

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 ContextSize tuple 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:

  1. Keep existing behavior unchanged:
    • trigger=("messages", 50)
    • trigger=[("fraction", 0.8), ("messages", 100)] # OR semantics remains as documented
  2. Add AND-capable trigger object(s):
    • trigger={"tokens": 4000, "messages": 10} # AND across keys
    • trigger=[{"tokens": 5000, "messages": 3}, {"tokens": 3000, "messages": 6}] # OR across dicts
  3. Implementation sketch (conceptual)
    Normalize trigger into 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.

Alternatives Considered

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestRequest for an enhancement / additional functionalitylangchain`langchain` package issues & PRs

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions