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) |
Problem
BaseSessionServicehas no public method to read user-scoped state (user_statestable) without an activesession_id.The only workaround today is calling
list_sessionsand 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_statesfor that user. Becauseget_sessionreturnsNonefor a brand-newsession_id— even whenuser_statesalready 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 toBaseSessionService:Contract:
user:prefix on keys (consistent with how user state is stored internally).{}when no user state exists for the given(app_name, user_id).BaseSessionServiceraisesNotImplementedErrorto preserve backward compatibility for existing custom subclasses.Implementations covered:
InMemorySessionServiceself.user_state[app_name][user_id]DatabaseSessionServiceStorageUserStateby(app_name, user_id)withread_only=TrueSqliteSessionService_get_user_statehelperVertexAiSessionServiceNotImplementedError(Vertex AI Agent Engine API does not expose user state independently of a session)