Skip to content

feat: Add agent_users table to associate sessions with users#2809

Draft
ecanlar wants to merge 2 commits intoopenai:mainfrom
ecanlar:feat/agent-users-table
Draft

feat: Add agent_users table to associate sessions with users#2809
ecanlar wants to merge 2 commits intoopenai:mainfrom
ecanlar:feat/agent-users-table

Conversation

@ecanlar
Copy link
Copy Markdown

@ecanlar ecanlar commented Mar 31, 2026

Summary

  • Introduces an agent_users table across all SQL-based session backends (SQLiteSession, AsyncSQLiteSession, SQLAlchemySession) to associate sessions with application-level users
  • Adds optional user_id parameter to session constructors and a get_sessions_for_user() query method
  • Mirrors the User → Session relationship pattern from Google's ADK, enabling multi-user agent applications without custom mapping layers

Motivation

Currently sessions exist independently with no built-in way to link them to users. Every developer building multi-user applications ends up creating their own user-session mapping layer on top of the SDK. This PR adds first-class support for this common pattern.

Changes

Schema

-- New table
CREATE TABLE agent_users (
    user_id TEXT PRIMARY KEY,
    metadata TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- New column on agent_sessions
ALTER TABLE agent_sessions ADD COLUMN user_id TEXT REFERENCES agent_users(user_id) ON DELETE SET NULL;
CREATE INDEX idx_agent_sessions_user_id ON agent_sessions (user_id);

API

# Create a session linked to a user
session = SQLiteSession("session-1", db_path="./data.db", user_id="alice")

# Retrieve all sessions for a user
session_ids = await session.get_sessions_for_user("alice")

Files modified

File Change
src/agents/memory/session.py Add user_id attribute to Session protocol and SessionABC
src/agents/memory/sqlite_session.py Add agent_users table, user_id FK, get_sessions_for_user()
src/agents/extensions/memory/async_sqlite_session.py Same changes for async SQLite backend
src/agents/extensions/memory/sqlalchemy_session.py Same changes for SQLAlchemy backend
tests/test_session.py Add tests for user association and user_id attribute

Test plan

  • All 34 existing session tests pass (backward compatible — user_id defaults to None)
  • New test_sqlite_session_user_association — verifies multi-user session queries
  • New test_sqlite_session_user_id_attribute — verifies user_id is stored on instance

Closes #2808

Introduces user-session association across all SQL-based session backends
(SQLiteSession, AsyncSQLiteSession, SQLAlchemySession), similar to how
Google's ADK models the User → Session relationship.

Changes:
- Add agent_users table with user_id, metadata, and timestamps
- Add user_id foreign key to agent_sessions table
- Add optional user_id parameter to session constructors
- Add get_sessions_for_user() method to query sessions by user
- Add user_id attribute to Session protocol and SessionABC
- Add tests for user association functionality

Closes openai#2808

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Adds limit and offset parameters to get_sessions_for_user() across all
three SQL backends, consistent with how get_items() supports limiting
retrieved history. This enables paginated retrieval of user sessions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ecanlar ecanlar changed the title Add agent_users table to associate sessions with users feat: Add agent_users table to associate sessions with users Mar 31, 2026
ecanlar added a commit to ecanlar/openai-agents-python that referenced this pull request Mar 31, 2026
…ends

Introduces two new classmethods for all SQL-based session backends:
- create_session(user_id, ...): Creates a new session with an auto-generated
  UUID session_id, persisting user and session rows immediately.
- get_session(user_id, session_id, ...): Retrieves an existing session,
  verifying it belongs to the given user. Returns None if not found.

This builds on the agent_users table from openai#2809 and provides a proper
factory pattern where the session_id is generated internally rather than
requiring the caller to invent one.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@seratch seratch marked this pull request as draft March 31, 2026 09:54
@seratch
Copy link
Copy Markdown
Member

seratch commented Mar 31, 2026

For the reason I mentioned at #2808 (comment), we don't plan to have this change at least for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: agent_users table to associate sessions with users

2 participants