redis_openai_agents.JSONSession#
- class JSONSession(session_id, user_id, redis_url='redis://localhost:6379', ttl=None)[source]#
Native RedisJSON-based session storage for OpenAI Agents SDK.
Uses JSON.* commands for atomic operations: - JSON.ARRAPPEND for message storage (no race conditions) - JSON.NUMINCRBY for counters (atomic increments) - JSON.SET for partial field updates - JSONPath queries for server-side filtering
Example
>>> session = JSONSession( ... session_id="abc123", ... user_id="user_1", ... redis_url="redis://localhost:6379", ... ) >>> await session.create() >>> await session.add_message(role="user", content="Hello") >>> messages = await session.get_messages()
Initialize a JSONSession.
- Parameters:
session_id (str) – Unique session identifier
user_id (str) – User identifier
redis_url (str) – Redis connection URL
ttl (int | None) – Time-to-live in seconds (None = no expiration)
- __init__(session_id, user_id, redis_url='redis://localhost:6379', ttl=None)[source]#
Initialize a JSONSession.
- Parameters:
session_id (str) – Unique session identifier
user_id (str) – User identifier
redis_url (str) – Redis connection URL
ttl (int | None) – Time-to-live in seconds (None = no expiration)
- Return type:
None
Methods
__init__(session_id, user_id[, redis_url, ttl])Initialize a JSONSession.
add_message(role, content[, agent, tokens])Add a message to the session using atomic JSON operations.
clear()Clear all messages from the session.
close()Close the Redis connection.
create()Create a new session document in Redis.
delete()Delete the entire session from Redis.
get_messages([limit, role])Get messages from the session with optional filtering.
get_metadata()Get session metadata (not full messages).
load(session_id[, redis_url])Load an existing session from Redis.
to_agent_inputs()Convert session messages to OpenAI Agents SDK input format.
track_agent(agent_name)Track agent usage in this session.
Attributes
session_idGet the session ID.
user_idGet the user ID.