feat(memory): add DatabaseMemoryService with SQLAlchemy async backend#5339
feat(memory): add DatabaseMemoryService with SQLAlchemy async backend#5339anmolg1997 wants to merge 4 commits into
Conversation
Adds a durable, SQL-backed memory service that works with any SQLAlchemy-compatible async database (SQLite, PostgreSQL, MySQL/MariaDB). This fills the gap between the volatile InMemoryMemoryService and the cloud-only Firestore/Vertex AI options, giving self-hosted deployments a persistent memory backend with zero cloud dependencies. The implementation mirrors the keyword-extraction approach used by FirestoreMemoryService and reuses the existing SQLAlchemy patterns established by DatabaseSessionService. Closes google#2524 Closes google#2976
|
Response from ADK Triaging Agent Hello @anmolg1997, thank you for creating this PR! Could you please provide logs or a screenshot of the passed This information will help reviewers to review your PR more efficiently. Thanks! |
|
Hi @adk-bot, thanks for the prompt. Full pytest results below. New tests (19/19 passing): Full memory test suite (59/59 passing, no regressions): All CI checks (CLA, header-check, agent-triage) are also passing. |
|
Hi @wuliang229, thanks for approving #5338. Would you be able to take a look at this one as well when you get a chance? It adds a SQL-backed |
Addresses the mypy-new-error gate failures on Python 3.10/3.13:
- memory/__init__.py: add `-> Any` return annotation to the lazy
`__getattr__(name)` so it satisfies `no-untyped-def`. Matches the
convention used by `tools/__init__.py` and `skills/__init__.py`.
- memory/_memory_schemas.py: silence SQLAlchemy's `DeclarativeBase`
type-stub `Any` base-class warning (same pattern as other places
where SQLAlchemy is used; pre-existing analogous errors in
sessions/schemas/{v0,v1}.py are grandfathered on main).
- memory/database_memory_service.py: annotate `__aexit__` parameters
so mypy no longer flags `no-untyped-def`.
Verified locally:
* `mypy .` reports zero errors in the three new files
(the mypy-diff gate compares against main; all remaining errors
under `src/google/adk/memory/` are pre-existing on main).
* All 19 DatabaseMemoryService unit tests still pass.
* pyink --check and isort --check-only are clean.
|
Hi @anmolg1997 , Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
Summary
Adds
DatabaseMemoryService, a durable, SQL-backed memory service that works with any SQLAlchemy-compatible async database (SQLite viaaiosqlite, PostgreSQL viaasyncpg, MySQL/MariaDB, etc.).InMemoryMemoryServiceand the cloud-only Firestore/Vertex AI options. Gives self-hosted deployments a persistent memory backend with zero cloud dependenciesFirestoreMemoryServiceand reuses the SQLAlchemy patterns fromDatabaseSessionServiceadd_session_to_memory,add_events_to_memory(delta ingestion), andsearch_memoryMotivation
Currently, developers not using Vertex AI or Google Cloud Firestore have no persistent memory option.
InMemoryMemoryServiceis explicitly volatile and loses all data on restart. This is a frequently requested feature:Design Decisions
google.adk.memory(notintegrations/)DatabaseSessionServiceplacement insessions/, and uses no external cloud SDKDynamicJSONfromsessions.schemas.shared_memory_schemas.pyfor ORM modelssessions/schemas/pattern, keeps concerns separatedSearchBackendabstraction__getattr__import in__init__.pyDatabaseSessionServicepattern, avoids hard SQLAlchemy dependency for users who don't need itChanges
src/google/adk/memory/database_memory_service.pyDatabaseMemoryServiceimplementationsrc/google/adk/memory/_memory_schemas.pyStorageMemoryEntry)src/google/adk/memory/__init__.pyDatabaseMemoryServicetests/unittests/memory/test_database_memory_service.pyUsage
Test plan
pytest tests/unittests/memory/test_database_memory_service.py)pytest tests/unittests/memory/)isort+pyinkper CONTRIBUTING.md__aenter__/__aexit__) verified