|
31 | 31 | from google.adk.platform import time as platform_time |
32 | 32 | from google.adk.platform import uuid as platform_uuid |
33 | 33 |
|
| 34 | +_sqlalchemy_import_error: ImportError | None = None |
34 | 35 | try: |
35 | 36 | from sqlalchemy import delete |
36 | 37 | from sqlalchemy import event |
|
49 | 50 | from sqlalchemy.ext.asyncio import AsyncSession as DatabaseSessionFactory |
50 | 51 | from sqlalchemy.ext.asyncio import create_async_engine |
51 | 52 | from sqlalchemy.pool import StaticPool |
52 | | -except ImportError: |
53 | | - pass |
| 53 | +except ImportError as e: |
| 54 | + # Re-raised by __init__, so the module still imports without the db extra. |
| 55 | + _sqlalchemy_import_error = e |
54 | 56 | from typing_extensions import override |
55 | 57 |
|
56 | 58 | from . import _session_util |
@@ -352,12 +354,15 @@ def __init__( |
352 | 354 | ValueError: If neither or both db_url and db_engine are provided, or if |
353 | 355 | engine creation fails. |
354 | 356 | """ |
355 | | - try: |
356 | | - import sqlalchemy # noqa: F401 |
357 | | - except ImportError as e: |
| 357 | + # Re-importing cannot tell whether the imports above failed: without |
| 358 | + # greenlet, SQLAlchemy 2.1 raises on the first import of its asyncio |
| 359 | + # extension and lets later ones succeed. |
| 360 | + if _sqlalchemy_import_error is not None: |
358 | 361 | from ..utils._dependency import missing_extra |
359 | 362 |
|
360 | | - raise missing_extra("sqlalchemy", "db") from e |
| 363 | + raise missing_extra( |
| 364 | + "sqlalchemy[asyncio]", "db" |
| 365 | + ) from _sqlalchemy_import_error |
361 | 366 |
|
362 | 367 | if (db_url is None) == (db_engine is None): |
363 | 368 | raise ValueError( |
|
0 commit comments