DynamoDBSession¶
DynamoDB-based session storage for openai-agents.
Class Definition¶
DynamoDBSession
¶
Bases: SessionABC
DynamoDB-based session storage for openai-agents.
Stores conversation history in a DynamoDB table. Each session is stored as a single item with the session_id as the partition key.
The table schema should have:
- Partition key: session_id (String)
The session data is stored in a conversation_data attribute as a JSON-encoded list,
with an optional ttl attribute for automatic expiration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Unique identifier for this session. |
required |
dynamodb_client
|
DynamoDBClient
|
An aiobotocore DynamoDB client. |
required |
table_name
|
str
|
Name of the DynamoDB table. |
required |
ttl_seconds
|
int | None
|
Time-to-live in seconds. None means no expiration.
Requires TTL to be enabled on the table with attribute name |
None
|
Example
from aiobotocore.session import get_session
from openai_agents_session import DynamoDBSession
session = get_session()
async with session.create_client("dynamodb", region_name="us-east-1") as client:
db_session = DynamoDBSession(
session_id="user-123",
dynamodb_client=client,
table_name="agent_sessions",
ttl_seconds=3600,
)
Source code in src/openai_agents_session/dynamodb.py
get_items
async
¶
Retrieve the conversation history for this session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int | None
|
Maximum number of items to retrieve. If None, returns all items.
When specified, returns the most recent |
None
|
Returns:
| Type | Description |
|---|---|
list[TResponseInputItem]
|
List of conversation items in chronological order. |
Source code in src/openai_agents_session/dynamodb.py
add_items
async
¶
Add new items to the conversation history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list[TResponseInputItem]
|
List of items to add to the session. |
required |
Source code in src/openai_agents_session/dynamodb.py
pop_item
async
¶
Remove and return the most recent item from the session.
Returns:
| Type | Description |
|---|---|
TResponseInputItem | None
|
The most recent item, or None if the session is empty. |
Source code in src/openai_agents_session/dynamodb.py
clear_session
async
¶
Usage Example¶
from aiobotocore.session import get_session
from openai_agents_session import DynamoDBSession
# Create 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,
)
# Add items
await session.add_items([
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"},
])
# Get items
items = await session.get_items()
# Clear session
await session.clear_session()
Parameters¶
session_id¶
- Type:
str - Required: Yes
Unique identifier for the session. Used as the partition key in DynamoDB.
dynamodb_client¶
- Type:
DynamoDBClient - Required: Yes
An aiobotocore DynamoDB client instance.
table_name¶
- Type:
str - Required: Yes
Name of the DynamoDB table to use.
ttl_seconds¶
- Type:
int | None - Default:
None
Time-to-live in seconds. If set, items will be automatically deleted after this duration. Requires TTL to be enabled on the table with attribute name ttl.
Methods¶
get_items¶
Retrieve conversation history.
Parameters:
limit: Maximum number of items to return. Returns the most recent items.
Returns: List of conversation items in chronological order.
add_items¶
Add new items to the conversation history.
Parameters:
items: List of items to add.
pop_item¶
Remove and return the most recent item.
Returns: The removed item, or None if the session is empty.
clear_session¶
Delete the entire session from DynamoDB.
Helper Functions¶
create_table_if_not_exists¶
create_table_if_not_exists
async
¶
Create the DynamoDB table if it doesn't exist.
This is a helper function for development/testing. In production, you should create the table through Infrastructure as Code (Terraform, CDK, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
DynamoDBClient
|
DynamoDB client. |
required |
table_name
|
str
|
Name of the table to create. |
required |
enable_ttl
|
bool
|
Whether to enable TTL on the table. |
True
|
Source code in src/openai_agents_session/dynamodb.py
DynamoDB Item Structure¶
Each session is stored as a single item:
| Attribute | Type | Description |
|---|---|---|
session_id |
String (S) | Partition key |
conversation_data |
String (S) | JSON-encoded list of items |
updated_at |
Number (N) | Unix timestamp |
ttl |
Number (N) | TTL timestamp (optional) |