feat: Add agent_users table to associate sessions with users#2809
Draft
ecanlar wants to merge 2 commits intoopenai:mainfrom
Draft
feat: Add agent_users table to associate sessions with users#2809ecanlar wants to merge 2 commits intoopenai:mainfrom
ecanlar wants to merge 2 commits intoopenai:mainfrom
Conversation
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
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>
5 tasks
Member
|
For the reason I mentioned at #2808 (comment), we don't plan to have this change at least for now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
agent_userstable across all SQL-based session backends (SQLiteSession, AsyncSQLiteSession, SQLAlchemySession) to associate sessions with application-level usersuser_idparameter to session constructors and aget_sessions_for_user()query methodMotivation
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
API
Files modified
src/agents/memory/session.pyuser_idattribute toSessionprotocol andSessionABCsrc/agents/memory/sqlite_session.pyagent_userstable,user_idFK,get_sessions_for_user()src/agents/extensions/memory/async_sqlite_session.pysrc/agents/extensions/memory/sqlalchemy_session.pytests/test_session.pyuser_idattributeTest plan
user_iddefaults toNone)test_sqlite_session_user_association— verifies multi-user session queriestest_sqlite_session_user_id_attribute— verifiesuser_idis stored on instanceCloses #2808