Skip to content

feat(sessions): add get_user_state(app_name, user_id) to BaseSessionService #5592

@nicolasmota

Description

@nicolasmota

Problem

BaseSessionService has no public method to read user-scoped state (user_states table) without an active session_id.

The only workaround today is calling list_sessions and inspecting events, which is expensive and not intended for this use case.

This creates a practical gap for callers that need to bootstrap user context before a new session exists — a pattern that arises whenever long-lived, cross-session data (e.g. user preferences, cached identity) must be seeded into agents without paying the cost of a full session lookup.

A concrete example: an authentication layer that resolves user identity from an external service and wants to skip the resolution if a fresh snapshot is already stored in user_states for that user. Because get_session returns None for a brand-new session_id — even when user_states already has data — callers are forced to maintain a separate process-level in-memory cache as a workaround, which defeats the purpose of persisting state to the database.

Proposed solution

Add a get_user_state(app_name, user_id) method to BaseSessionService:

async def get_user_state(
    self, *, app_name: str, user_id: str
) -> dict[str, Any]:
    ...

Contract:

  • Returns the raw user-scoped dictionary, without the user: prefix on keys (consistent with how user state is stored internally).
  • Returns {} when no user state exists for the given (app_name, user_id).
  • The default implementation in BaseSessionService raises NotImplementedError to preserve backward compatibility for existing custom subclasses.

Implementations covered:

Service Approach
InMemorySessionService Reads from self.user_state[app_name][user_id]
DatabaseSessionService Queries StorageUserState by (app_name, user_id) with read_only=True
SqliteSessionService Delegates to the existing _get_user_state helper
VertexAiSessionService Raises NotImplementedError (Vertex AI Agent Engine API does not expose user state independently of a session)

Metadata

Metadata

Labels

needs review[Status] The PR/issue is awaiting review from the maintainerservices[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions