Skip to content

Add session-level metadata storage to SQLAlchemySession #1938

@Jash271

Description

@Jash271

Please read this first

  • Have you read the docs? Yes - Agents SDK docs
  • Have you searched for related issues? Yes - no existing issues found

Describe the feature

Add session-level metadata storage to SQLAlchemySession

Problem

SQLAlchemySession currently stores conversation messages but has no built-in way to store session-level metadata like:

  • User ownership (owner_id)
  • Session titles and tags
  • Configuration (incognito mode, preferences)
  • Custom application data

Developers must maintain separate mapping tables or external systems, causing data fragmentation and complexity.

Proposed Solution

Add a session_metadata table using Entity-Attribute-Value (EAV) pattern for flexible key-value storage.

API:

# Set metadata
await session.set_metadata({"owner_id": "user_123", "title": "My Chat", "tags": ["work"]})

# Get metadata (returns None for missing keys)
await session.get_metadata(keys=["owner_id", "title"])
# Returns: {"owner_id": "user_123", "title": "My Chat"}

# Get all metadata
await session.get_metadata()

# Delete metadata
await session.delete_metadata(keys=["tags"])  # Delete specific keys
await session.delete_metadata()  # Delete all

# Find sessions by metadata (cross-session query)
session_ids = await session.find_sessions_by_metadata("owner_id", "user_123")

Schema:

CREATE TABLE agent_sessions_metadata (
    session_id VARCHAR REFERENCES agent_sessions(session_id) ON DELETE CASCADE,
    key VARCHAR(255) NOT NULL,
    value TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (session_id, key)
);
CREATE INDEX idx_session_metadata_key_value ON session_metadata(key, value);

Benefits

  • Unified storage: Messages + metadata in one system
  • Efficient queries: Index enables fast "find all sessions for user X"
  • Flexible: Add any metadata keys without schema changes
  • Cross-database: PostgreSQL, MySQL, SQLite compatible
  • Backward compatible: No breaking changes

Use Cases

  • Multi-tenant apps: Associate sessions with users
  • Organization: Titles, tags, descriptions
  • Privacy: Incognito/ephemeral flags
  • Analytics: Track usage metrics
  • Configuration: Session-specific settings

I'm happy to implement this and submit a PR

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions