adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Missing state from the list sessions and AttributeError: 'InMemorySessionService' object has no attribute 'database_session_factory'

Open feketedavid1012 opened this issue 1 year ago • 0 comments

Hi all,

I don't see the state values when I would like to list all of my sessions. I tried to implement a custom endpoint and it worked fine, but since to the latest version update I got AttributeError: 'InMemorySessionService' object has no attribute 'database_session_factory'

Here is my code: `def get_session_service(session_service_uri=None,agents_dir=None): agent_engine_id=None if session_service_uri: if session_service_uri.startswith("agentengine://"): agent_engine_id_or_resource_name = session_service_uri.split("://")[1] project, location, agent_engine_id = _parse_agent_engine_resource_name( agent_engine_id_or_resource_name,agents_dir ) session_service = VertexAiSessionService( project=project, location=location, agent_engine_id=agent_engine_id, ) else: from google.adk.sessions.database_session_service import DatabaseSessionService

  # Database session additional settings
  if session_db_kwargs is None:
    session_db_kwargs = {}
  session_service = DatabaseSessionService(
      db_url=session_service_uri, **session_db_kwargs
  )

else: session_service = InMemorySessionService() return session_service,agent_engine_id

def _parse_agent_engine_resource_name(agent_engine_id_or_resource_name,agents_dir): if not agent_engine_id_or_resource_name: raise click.ClickException( "Agent engine resource name or resource id can not be empty." )

# "projects/my-project/locations/us-central1/reasoningEngines/1234567890",
if "/" in agent_engine_id_or_resource_name:
  # Validate resource name.
  if len(agent_engine_id_or_resource_name.split("/")) != 6:
    raise click.ClickException(
        "Agent engine resource name is mal-formatted. It should be of"
        " format :"
        " projects/{project_id}/locations/{location}/reasoningEngines/{resource_id}"
    )
  project = agent_engine_id_or_resource_name.split("/")[1]
  location = agent_engine_id_or_resource_name.split("/")[3]
  agent_engine_id = agent_engine_id_or_resource_name.split("/")[-1]
else:
  envs.load_dotenv_for_agent("", agents_dir)
  project = os.environ["GOOGLE_CLOUD_PROJECT"]
  location = os.environ["GOOGLE_CLOUD_LOCATION"]
  agent_engine_id = agent_engine_id_or_resource_name
return project, location, agent_engine_id

def list_sessions( session_service, app_name: str, user_id: str ) -> ListSessionsResponseCustom: with session_service.database_session_factory() as sessionFactory: results = ( sessionFactory.query(StorageSession) .filter(StorageSession.app_name == app_name) .filter(StorageSession.user_id == user_id) .all() ) sessions = [] for storage_session in results:

    session = CustomSession(
        app_name=app_name,
        user_id=user_id,
        id=storage_session.id,
        state=storage_session.state,
        last_update_time=storage_session.update_time.timestamp(),
        creation_time=storage_session.create_time.timestamp(),
    )
    sessions.append(session)
  return ListSessionsResponseCustom(sessions=sessions)

session_service,agent_engine_id = get_session_service(os.getenv("SESSION_DB_URL"),os.getenv("AGENT_DIR")) [ session for session in domain.list_sessions(session_service=session_service, app_name=app_name, user_id=user_id ).sessions ] `

feketedavid1012 avatar Jul 26 '25 11:07 feketedavid1012