Redis and DynamoDB session backends for openai-agents-python
Project description
openai-agents-session
Redis and DynamoDB session backends for openai-agents-python.
Installation
Install with your preferred backend:
# Redis only
pip install "openai-agents-session[redis]"
# DynamoDB only
pip install "openai-agents-session[dynamodb]"
# Both backends
pip install "openai-agents-session[all]"
Quick Start
Redis Session
import redis.asyncio as redis
from agents import Agent, Runner
from openai_agents_session import RedisSession
# Create Redis client
client = redis.from_url("redis://localhost:6379")
# Create session with optional TTL
session = RedisSession(
session_id="user-123",
redis_client=client,
ttl=3600, # 1 hour expiration
)
# Use with openai-agents
agent = Agent(name="Assistant", instructions="You are a helpful assistant.")
runner = Runner(agent=agent, session=session)
# Run conversation
result = await runner.run("Hello!")
print(result.final_output)
# Continue the conversation (history is preserved)
result = await runner.run("What did I just say?")
print(result.final_output)
DynamoDB Session
from aiobotocore.session import get_session
from agents import Agent, Runner
from openai_agents_session import DynamoDBSession
# Create DynamoDB client
boto_session = get_session()
async with boto_session.create_client("dynamodb", region_name="us-east-1") as client:
# Create session
session = DynamoDBSession(
session_id="user-123",
dynamodb_client=client,
table_name="agent_sessions",
ttl_seconds=3600, # 1 hour expiration
)
# Use with openai-agents
agent = Agent(name="Assistant", instructions="You are a helpful assistant.")
runner = Runner(agent=agent, session=session)
result = await runner.run("Hello!")
print(result.final_output)
DynamoDB Table Setup
Create a DynamoDB table with the following schema:
| Attribute | Type | Key |
|---|---|---|
| session_id | String | Partition Key |
For TTL support, enable Time to Live with attribute name ttl.
Using AWS CLI
aws dynamodb create-table \
--table-name agent_sessions \
--attribute-definitions AttributeName=session_id,AttributeType=S \
--key-schema AttributeName=session_id,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
# Enable TTL
aws dynamodb update-time-to-live \
--table-name agent_sessions \
--time-to-live-specification Enabled=true,AttributeName=ttl
Using Helper Function (Development Only)
from openai_agents_session.dynamodb import create_table_if_not_exists
await create_table_if_not_exists(client, "agent_sessions", enable_ttl=True)
API Reference
RedisSession
RedisSession(
session_id: str, # Unique session identifier
redis_client: Redis, # Async Redis client
key_prefix: str = "openai_agents_session", # Redis key prefix
ttl: int | None = None, # TTL in seconds (None = no expiration)
)
DynamoDBSession
DynamoDBSession(
session_id: str, # Unique session identifier
dynamodb_client: DynamoDBClient, # aiobotocore DynamoDB client
table_name: str, # DynamoDB table name
ttl_seconds: int | None = None, # TTL in seconds (None = no expiration)
)
Common Methods (SessionABC)
| Method | Description |
|---|---|
get_items(limit=None) |
Get conversation history (most recent limit items) |
add_items(items) |
Add items to conversation history |
pop_item() |
Remove and return the most recent item |
clear_session() |
Delete all items in the session |
Development
# Clone the repository
git clone https://github.com/xncbf/openai-agents-session.git
cd openai-agents-session
# Install dependencies
uv sync --all-extras
# Run linter
uv run ruff check .
# Run formatter
uv run ruff format .
# Run type checker
uv run ty check src/
# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov
License
MIT License - see LICENSE for details.
Contributing
Contributions are welcome! Please read our contributing guidelines and submit pull requests.
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 openai_agents_session-0.1.0.tar.gz.
File metadata
- Download URL: openai_agents_session-0.1.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9ebdcf1dd913ded550f5f6a0ba2996f98279a9b93cc6b39e74c40a02fb63803
|
|
| MD5 |
eeec3cd6d5495b1a96c528fddd866115
|
|
| BLAKE2b-256 |
21f097fd57de96459e52506bcf4625e66fdf856c1dc6f5096a7a48fed1e15aa6
|
Provenance
The following attestation bundles were made for openai_agents_session-0.1.0.tar.gz:
Publisher:
release.yml on xncbf/openai-agents-session
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openai_agents_session-0.1.0.tar.gz -
Subject digest:
b9ebdcf1dd913ded550f5f6a0ba2996f98279a9b93cc6b39e74c40a02fb63803 - Sigstore transparency entry: 730067805
- Sigstore integration time:
-
Permalink:
xncbf/openai-agents-session@e52908a5dc7bebdb4816a482a85d6cf9e999dd34 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/xncbf
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e52908a5dc7bebdb4816a482a85d6cf9e999dd34 -
Trigger Event:
release
-
Statement type:
File details
Details for the file openai_agents_session-0.1.0-py3-none-any.whl.
File metadata
- Download URL: openai_agents_session-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.9 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 |
af9865eda7e570f4e267560aa55a6f1f4fea25473ebdbec74d800715106b5c84
|
|
| MD5 |
ec07155f6837249b9f8fe2625ecd418a
|
|
| BLAKE2b-256 |
0df6e7a6a6ccb9843ae790d21ee9cec76ff8472ec1b4b0e2cddb0cab476283bc
|
Provenance
The following attestation bundles were made for openai_agents_session-0.1.0-py3-none-any.whl:
Publisher:
release.yml on xncbf/openai-agents-session
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openai_agents_session-0.1.0-py3-none-any.whl -
Subject digest:
af9865eda7e570f4e267560aa55a6f1f4fea25473ebdbec74d800715106b5c84 - Sigstore transparency entry: 730067810
- Sigstore integration time:
-
Permalink:
xncbf/openai-agents-session@e52908a5dc7bebdb4816a482a85d6cf9e999dd34 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/xncbf
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e52908a5dc7bebdb4816a482a85d6cf9e999dd34 -
Trigger Event:
release
-
Statement type: