Skip to main content

Utilities for Langchain and langgraph

Project description

langgraph-reducer

This package provides:

  • 🧠 PrunableStateFactory: Auto-prunes message history without extra effort, works as a drop-in replacement for MessageState.
  • 🧹 MessagePrunerNode: A LangGraph node that summarizes or deletes messages when they exceed a count.
  • ⚠️ Important difference:
    • PrunableState runs on every node execution — better for lightweight, fast pruning.
    • MessagePrunerNode runs only where you add it — better for summarization or heavier logic (e.g., calling LLMs).

🚀 Install

pip install langgraph-reducer

🔁 PrunableState — Auto Cleanup

Use this when you just want to keep the latest N messages and forget the rest. No node logic needed — pruning is automatic.

from langraph_reducer import PrunableStateFactory

# Parameters:
# - min_messages: number of most recent messages to retain
# - max_messages: pruning is triggered once this threshold is exceeded

PrunableMessagesState = PrunableStateFactory.create_prunable_state(
    min_messages=10,
    max_messages=15
)

Use the PrunableMessagesState as the state type in your LangGraph:

from langgraph.graph import StateGraph

graph = StateGraph(PrunableMessagesState)

That’s it — pruning will be handled automatically during graph execution.


🧠 MessagePrunerNode — Summary or Delete

Use this when you want to summarize or remove old messages but need control over where in your graph this happens (e.g., before END).

from langchain_openai import ChatOpenAI
from langgraph_reducer import MessagePrunerNode

llm = ChatOpenAI(model="gpt-4o")

# model_func: Optional. If provided, node summarizes older messages.
#             If not provided, node simply deletes older messages.

def model_func(messages):
    return llm.invoke(messages)

pruner_node = MessagePrunerNode(
    min_messages=4,       # Retain at least 4 most recent messages
    max_messages=6,       # Trigger pruning if messages exceed 6
    model_func=model_func # Optional. Use LLM to summarize old messages.
)

Then wire it into your graph like a normal LangGraph node:

workflow.add_node("summarize_conversation", pruner_node)
workflow.add_edge("summarize_conversation", END)

Use a conditional edge to trigger the pruner only when needed.


🧩 Extending Prunable State

Want to add custom fields to the state? Just subclass the factory-created state:

from typing import Annotated
from langgraph.graph.message import add_messages

# Base state with auto-pruning
BaseState = PrunableStateFactory.create_prunable_state(10, 15)

# Extend it with your own fields
class MyState(BaseState):
    summary: str = ""
    profile_id: str = ""

Use MyState in your graph instead of the base one.

⚠️ Important:
Do not override or redefine the messages field when subclassing a prunable state.
The messages field is automatically managed for pruning and must retain its behavior.

If you override it, pruning logic will break silently.


📓 Examples

  • notebooks/basic_pruner_node.ipynb: shows how to integrate a summarizing node.
  • scripts/prunable_state_lambda.py: shows full pipeline with PrunableMessagesState in AWS Lambda.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langgraph_reducer-0.0.2.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langgraph_reducer-0.0.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file langgraph_reducer-0.0.2.tar.gz.

File metadata

  • Download URL: langgraph_reducer-0.0.2.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for langgraph_reducer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7d627006948afabb036832ce1ce6af6c517b5b2307c535c5e9aad12ea6a151d3
MD5 1700785ba3e48b95d1a448f58bfaafe1
BLAKE2b-256 58e80d5eabd01cdbb9542389a282a674b2d39098899a539119441532bbad3718

See more details on using hashes here.

File details

Details for the file langgraph_reducer-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for langgraph_reducer-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3a1160d793cacade378df9ed991bd71c524365430483e510d317b97427ff2f45
MD5 fd911d279c9c65083aa17d7e736ece79
BLAKE2b-256 f70143f8ad12762d8068e56f5109375b3d6eb7c7d720448df088e2ffc724a607

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page