LangChain Support
Back to OSS (LangChain and LangGraph)

How do I retrieve the most recent conversations using PostgresStore search?

OSS (LangChain and LangGraph)·1 min read·Jan 30, 2026··

Context

When working with PostgresStore to store conversations, you may need to retrieve the N most recent conversations or filter items by timestamp. This is useful for displaying recent chat history or implementing pagination based on recency.

Answer

PostgresStore automatically orders search results by updated_at in descending order (most recent first), making it easy to retrieve recent conversations.

To get the N most recent conversations:

from langgraph.store.postgres import PostgresStore

# Get the N most recent conversations
# Results are automatically ordered by updated_at DESC (most recent first)
recent_conversations = store.search(
    ("conversations",),
    limit=10  # Returns 10 most recent items
)

To filter by timestamp:

The filter parameter only works on fields inside your stored JSON value, not on the created_at/updated_at metadata columns. If you need to filter by a specific time range (e.g., "conversations from the last 7 days"), store a timestamp inside your value:

# Store conversation with timestamp in the value
store.put(("conversations",), "conv-123", {
    "messages": [...],
    "started_at": datetime.now().isoformat()
})

# Then filter on it
store.search(("conversations",), filter={"started_at": {"$gte": "2025-01-20T00:00:00"}})

This approach leverages the automatic sorting behavior while allowing for custom timestamp filtering when needed.