redis_openai_agents.AgentSession#

class AgentSession(user_id, conversation_id=None, redis_url='redis://localhost:6379', pool=None)[source]#

Persistent session storage for OpenAI Agents SDK conversations.

Built on top of RedisVL’s MessageHistory, this class provides: - Message persistence across Python sessions - Multi-conversation management per user - Agent handoff tracking - Conversation metadata

Example

>>> # Create a new session
>>> session = AgentSession.create(user_id="user_123")
>>>
>>> # Run agent with session
>>> result = Runner.run(agent, input=inputs, session=session)
>>>
>>> # Later, load and continue
>>> session = AgentSession.load(conversation_id="conv_abc")
>>> history = session.to_agent_inputs()
>>> result = Runner.run(agent, input=history + new_inputs, session=session)

Initialize an AgentSession.

Parameters:
  • user_id (str) – User identifier

  • conversation_id (str | None) – Optional conversation ID (generates one if not provided)

  • redis_url (str) – Redis connection URL

  • pool (RedisConnectionPool | None) – Optional shared connection pool

__init__(user_id, conversation_id=None, redis_url='redis://localhost:6379', pool=None)[source]#

Initialize an AgentSession.

Parameters:
  • user_id (str) – User identifier

  • conversation_id (str | None) – Optional conversation ID (generates one if not provided)

  • redis_url (str) – Redis connection URL

  • pool (RedisConnectionPool | None) – Optional shared connection pool

Methods

__init__(user_id[, conversation_id, ...])

Initialize an AgentSession.

aadd_message(role, content, **metadata)

Async version of add_message() - add a message to the session.

add_message(role, content, **metadata)

Add a message to the session.

aget_messages([top_k])

Async version of get_messages() - get recent messages from the session.

astore_agent_result(result)

Async version of store_agent_result() - store messages from Runner result.

clear()

Clear all messages from the session.

create(user_id[, redis_url])

Create a new session with a generated conversation ID.

delete()

Delete the session and all its data from Redis.

get_messages([top_k])

Get recent messages from the session.

get_metadata()

Get session metadata.

list_conversations(user_id[, redis_url])

List all conversations for a user.

load(conversation_id[, user_id, redis_url])

Load an existing session from Redis.

message_count()

Get the number of messages in the session.

store_agent_result(result)

Store messages from OpenAI Agents SDK Runner result.

store_exchange(user_message, assistant_response)

Store a user-assistant message exchange.

to_agent_inputs()

Convert session messages to OpenAI Agents SDK input format.

track_agent(agent_name)

Track agent usage in this session.

Attributes

current_agent

Get the current agent name.