Skip to content
41 changes: 41 additions & 0 deletions src/google/adk/sessions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

from typing_extensions import override
import importlib
from typing import TYPE_CHECKING

Expand All @@ -28,6 +29,45 @@
from .in_memory_session_service import InMemorySessionService
from .vertex_ai_session_service import VertexAiSessionService

try:
from .database_session_service import DatabaseSessionService
except ImportError:
# This handles the case where optional dependencies (like sqlalchemy)
# are not installed. A placeholder class ensures the symbol is always
# available for documentation tools and static analysis.
# We use type: ignore[no-redef, misc] to satisfy strict mypy checks.
class DatabaseSessionService(BaseSessionService): # type: ignore[no-redef, misc]
"""Placeholder for DatabaseSessionService when dependencies are not installed."""

_ERROR_MESSAGE = (
'DatabaseSessionService requires sqlalchemy>=2.0, please ensure it is'
' installed correctly.'
)

def __init__(self, *args: Any, **kwargs: Any) -> None:
raise ImportError(self._ERROR_MESSAGE)
Comment thread
Akshat8510 marked this conversation as resolved.

@override
async def create_session(self, *args: Any, **kwargs: Any) -> Any:
raise ImportError(self._ERROR_MESSAGE)

@override
async def get_session(self, *args: Any, **kwargs: Any) -> Any:
raise ImportError(self._ERROR_MESSAGE)

@override
async def list_sessions(self, *args: Any, **kwargs: Any) -> Any:
raise ImportError(self._ERROR_MESSAGE)

@override
async def delete_session(self, *args: Any, **kwargs: Any) -> Any:
raise ImportError(self._ERROR_MESSAGE)

@override
async def append_event(self, *args: Any, **kwargs: Any) -> Any:
raise ImportError(self._ERROR_MESSAGE)
Comment thread
Akshat8510 marked this conversation as resolved.
Comment thread
Akshat8510 marked this conversation as resolved.
Comment thread
Akshat8510 marked this conversation as resolved.
Comment thread
Akshat8510 marked this conversation as resolved.
Comment thread
Akshat8510 marked this conversation as resolved.


__all__ = [
'BaseSessionService',
'DatabaseSessionService',
Expand Down Expand Up @@ -55,3 +95,4 @@ def __getattr__(name: str):
raise missing_extra('sqlalchemy', 'db') from e
return vars(module)['DatabaseSessionService']
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')