Skip to content

Commit 5f05749

Browse files
wuliang229copybara-github
authored andcommitted
chore: Lazy import DatabaseSessionService in the adk/sessions/ module
This accounts for about 8% of latency in cold start, because DatabaseSessionService imports from sqlalchemy. After the change, DatabaseSessionService is only imported if it's really needed by the program. The other objects in this file are not affected because they contribute to a small fraction of the latency. Co-authored-by: Liang Wu <wuliang@google.com> PiperOrigin-RevId: 829009868
1 parent 8dd5a79 commit 5f05749

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/google/adk/sessions/__init__.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
import logging
15-
1614
from .base_session_service import BaseSessionService
1715
from .in_memory_session_service import InMemorySessionService
1816
from .session import Session
1917
from .state import State
2018
from .vertex_ai_session_service import VertexAiSessionService
2119

22-
logger = logging.getLogger('google_adk.' + __name__)
23-
24-
2520
__all__ = [
2621
'BaseSessionService',
22+
'DatabaseSessionService',
2723
'InMemorySessionService',
2824
'Session',
2925
'State',
3026
'VertexAiSessionService',
3127
]
3228

33-
try:
34-
from .database_session_service import DatabaseSessionService
3529

36-
__all__.append('DatabaseSessionService')
37-
except ImportError:
38-
logger.debug(
39-
'DatabaseSessionService require sqlalchemy>=2.0, please ensure it is'
40-
' installed correctly.'
41-
)
30+
def __getattr__(name: str):
31+
if name == 'DatabaseSessionService':
32+
try:
33+
from .database_session_service import DatabaseSessionService
34+
35+
return DatabaseSessionService
36+
except ImportError as e:
37+
raise ImportError(
38+
'DatabaseSessionService requires sqlalchemy>=2.0, please ensure it is'
39+
' installed correctly.'
40+
) from e
41+
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')

0 commit comments

Comments
 (0)