PostgreSQL session manager for Strands Agents SDK with full ACID guarantees
Project description
Strands PostgreSQL Session Manager
A production-ready session manager for Strands Agents that uses PostgreSQL for persistent storage. This enables agents to maintain conversation history and state with full ACID guarantees, even in distributed environments.
Tested with PostgreSQL 14+ (local, RDS, Cloud SQL, Azure Database, and Supabase).
Features
- Persistent Sessions: Store agent conversations and state in PostgreSQL with full ACID guarantees
- Distributed Ready: Share sessions across multiple application instances
- JSONB Storage: Native JSON support for complex data structures (state, conversation manager, messages)
- Referential Integrity: CASCADE deletes ensure data consistency
- Production Tested: Battle-tested with 18 unit tests + 4 integration tests (100% passing)
- Analytics Ready: SQL queries for conversation analysis and reporting
Installation
pip install strands-postgresql-session-manager
Quick Start
from strands import Agent
from strands_postgresql_session_manager import PostgresSessionManager
from sqlmodel import create_engine, SQLModel
# Create PostgreSQL engine
engine = create_engine("postgresql://user:password@localhost:5432/agents_db")
# Create tables (run once)
SQLModel.metadata.create_all(engine)
# Create session manager with unique session ID
session_manager = PostgresSessionManager(
session_id="user_123",
engine=engine
)
# Create agent with session manager
agent = Agent(session_manager=session_manager)
# Use agent - all messages are automatically persisted to PostgreSQL
agent("Hello! Tell me about PostgreSQL session storage.")
# The conversation is now stored in PostgreSQL and can be resumed later,
# using the same session_id
Storage Structure
The PostgresSessionManager stores data using the following table structure:
sessions (session_id PK, session_type, created_at, updated_at)
└── agents (session_id FK, agent_id, state JSONB, conversation_manager_state JSONB, _internal_state JSONB)
└── messages (session_id FK, agent_id, message_id, message JSONB, redact_message JSONB)
Foreign keys use ON DELETE CASCADE to ensure referential integrity.
API Reference
PostgresSessionManager
PostgresSessionManager(
session_id: str,
engine: Engine
)
Parameters:
session_id: Unique identifier for the sessionengine: Configured SQLAlchemy/SQLModel engine instance
Methods (Note that these methods are used transparently by Strands):
create_session(session): Create a new sessionread_session(session_id): Retrieve session datadelete_session(session_id): Remove session and all associated data (CASCADE)create_agent(session_id, agent): Store agent in sessionread_agent(session_id, agent_id): Retrieve agent dataupdate_agent(session_id, agent): Update agent statecreate_message(session_id, agent_id, message): Store messageread_message(session_id, agent_id, message_id): Retrieve messageupdate_message(session_id, agent_id, message): Update messagelist_messages(session_id, agent_id, limit=None, offset=0): List all messages
Contributing
Setup
# Clone the repository
git clone https://github.com/macuartin/strands-postgresql-session-manager
cd strands-postgresql-session-manager
# Create virtual environment and install dependencies
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -e ".[dev]"
Running Tests
# Run unit tests only (default)
pytest tests/test_unit.py -v
# Run with coverage
pytest tests/test_unit.py --cov=src/strands_postgresql_session_manager --cov-report=html
Integration Tests
Integration tests require a running PostgreSQL instance:
# Start PostgreSQL (Docker)
docker run -d --name postgres-test \
-p 5432:5432 \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=test \
-e POSTGRES_DB=test \
postgres:14
# Set DATABASE_URL
export DATABASE_URL="postgresql://test:test@localhost:5432/test"
# Run integration tests
pytest tests/test_integration.py -v
# Cleanup
docker stop postgres-test && docker rm postgres-test
Requirements
- Python 3.10+
- PostgreSQL 14+
- strands-agents >= 1.0.0
- sqlmodel >= 0.0.14
- psycopg2-binary >= 2.9.0
License
MIT License - see LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strands_postgresql_session_manager-0.1.0.tar.gz.
File metadata
- Download URL: strands_postgresql_session_manager-0.1.0.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89a3994f9028b6874e886851c65aa1897a76395244f1b8847cfea3e1e787db7b
|
|
| MD5 |
cef962a5fd2c2ffd35ae057b759b84c0
|
|
| BLAKE2b-256 |
b9c0f5711ef4fc73562f5fb5203072173df4520c2bd19407804d39244efa9ffd
|
Provenance
The following attestation bundles were made for strands_postgresql_session_manager-0.1.0.tar.gz:
Publisher:
publish.yml on macuartin/strands-postgresql-session-manager
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strands_postgresql_session_manager-0.1.0.tar.gz -
Subject digest:
89a3994f9028b6874e886851c65aa1897a76395244f1b8847cfea3e1e787db7b - Sigstore transparency entry: 736835375
- Sigstore integration time:
-
Permalink:
macuartin/strands-postgresql-session-manager@4b6ce8ab77f69394e498073e9573dca1ef66eb0a -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/macuartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4b6ce8ab77f69394e498073e9573dca1ef66eb0a -
Trigger Event:
release
-
Statement type:
File details
Details for the file strands_postgresql_session_manager-0.1.0-py3-none-any.whl.
File metadata
- Download URL: strands_postgresql_session_manager-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2a5fa6f2a12ab571dc7595e84d48cb9d7fab676d3de8a7dad48637cade0a6ef
|
|
| MD5 |
2839c5085f51c92375ef8c3743756fba
|
|
| BLAKE2b-256 |
ba63f195cf78e41dc623de24de5e591488b94230011ffbdf9469e606a8aee4a6
|
Provenance
The following attestation bundles were made for strands_postgresql_session_manager-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on macuartin/strands-postgresql-session-manager
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strands_postgresql_session_manager-0.1.0-py3-none-any.whl -
Subject digest:
e2a5fa6f2a12ab571dc7595e84d48cb9d7fab676d3de8a7dad48637cade0a6ef - Sigstore transparency entry: 736835380
- Sigstore integration time:
-
Permalink:
macuartin/strands-postgresql-session-manager@4b6ce8ab77f69394e498073e9573dca1ef66eb0a -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/macuartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4b6ce8ab77f69394e498073e9573dca1ef66eb0a -
Trigger Event:
release
-
Statement type: