Skip to content

feat(integrations): add MongoDB-backed memory service - #7512

Closed
theshanbhag wants to merge 14 commits into
google:mainfrom
theshanbhag:mongodb-memory-service
Closed

theshanbhag wants to merge 14 commits into
google:mainfrom
theshanbhag:mongodb-memory-service

Conversation

@theshanbhag

@theshanbhag theshanbhag commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Adds MongoDbMemoryService: session events ingested into a keyword-indexed memory collection for long-term recall, exported from google.adk.integrations.mongodb behind the new MONGODB_MEMORY_SERVICE experimental feature (default on, matching the integration's existing flags).

Split 4 of 4 from #7291 (reviewer-requested split). Contains the session-service PR (#7511) beneath it; the memory-service commits are the last ones.

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

Problem:

ADK's MongoDB integration has no long-term memory service; sessions end, and their content is not recallable across sessions.

Solution:

MongoDbMemoryService stores one memory document per ingested event, keyed by <app_name>/<user_id>/<session_id>/<event_id> (so re-ingesting a session overwrites rather than duplicates), each carrying the lowercase keywords extracted from its text (stop-word list customizable). search_memory matches documents whose keyword array intersects the query's keywords, scoped per app and user. ensure_indexes=True optionally creates the app_user_keywords multikey index.

Keyword-only retrieval is intentional (reviewer question on #7291): it needs no embedding model, no Google credentials, and no vector index, so it works on any MongoDB deployment (including standalone and free-tier clusters) at zero per-query cost and with predictable behavior. Vector recall is a reasonable follow-up that can build directly on this integration's own mongodb_vector_search tooling; this PR keeps the first version dependency-free. This rationale is stated in the README as well.

Not in this PR, per the #7291 review: pickling for Agent Engine (comes with the follow-up that wires the services into Agent Engine).

NO_UNIT_GUIDE=The integration README documents the service, including its storage layout and least-privilege setup; a per-unit guide will follow when the experimental API stabilizes.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

pytest tests/unittests/integrations/mongodb/ tests/unittests/features/ → 119 passed (mongomock-backed): ingest/recall round trips, per-user scoping, ingestion idempotency, stop-word handling, index creation, and feature-registry tests. All pre-commit hooks pass.

Manual End-to-End (E2E) Tests:

Against a live Atlas 8.0.34 cluster: ingesting sessions and recalling by keyword returns the ingested events, memories are scoped per user, and re-ingesting the same session never duplicates entries.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Add `use_mongodb_auto_embedding` and `mongodb_auto_embedding_model` to
MongoDbToolSettings. When enabled, the search tools send the query as
plain text (`query.text`) instead of embedding it through the genai
client first: Atlas generates the embedding itself with the Voyage AI
model configured on the index's `autoEmbed` field (Atlas Automated
Embedding, Preview), so deployments that already store data on Atlas
need no Google embedding credentials. The default mode (Google
embedding models via `queryVector`) is unchanged.

Split from google#7291.
MongoDbSessionService persists sessions, events, and app:/user:-scoped
state in MongoDB, exported from google.adk.integrations.mongodb behind
the MONGODB_SESSION_SERVICE experimental feature (default on).

Concurrency model: within a process a per-session lock serializes
append_event; across runner replicas the optimistic-concurrency
revision on the session document is the only guard, and the losing
writer gets StaleSessionError. Multi-document writes run in a
transaction on replica sets / sharded clusters / Atlas; on deployments
without transaction support (standalone mongod) the verdict is cached
and writes fall back to sequential with the revision check. Optional
`ensure_indexes` creates the recommended secondary indexes on first
use. mongomock joins the test dependencies for the new unit tests.

NO_UNIT_GUIDE=The integration README documents the service, including
its document layout, concurrency model, and least-privilege setup; a
per-unit guide will follow when the experimental API stabilizes.

Split from google#7291.
MongoDbMemoryService ingests session events into a memory collection
and recalls them by keyword matching, exported from
google.adk.integrations.mongodb behind the MONGODB_MEMORY_SERVICE
experimental feature (default on). Ingestion is idempotent (memory
documents are keyed by app/user/session/event), and `ensure_indexes`
creates the app/user/keywords index on first use.

Retrieval is intentionally keyword-only: it needs no embedding model,
no Google credentials, and no vector index, so it works on any MongoDB
deployment at zero per-query cost. Vector recall is a possible
follow-up building on this integration's own search tools.

NO_UNIT_GUIDE=The integration README documents the service, including
its storage layout and least-privilege setup; a per-unit guide will
follow when the experimental API stabilizes.

Split from google#7291.
…b-session-service

# Conflicts:
#	constraints-3.11.txt
#	constraints-3.12.txt
#	constraints-3.13.txt
#	constraints-3.14.txt
…n MongoDbSessionService

Review follow-up from google#7533: list_sessions fetched each user's state
with a separate find_one per user (N+1); it now issues a single $in
query against the user_states collection. Index creation on first use
is also serialized with a thread lock (double-checked flag), so
concurrent first calls no longer issue duplicate create_index calls.

Addresses gemini-code-assist review comments on google#7533.
Review follow-up from google#7533: add_session_to_memory issued one
replace_one round-trip per event; it now batches all upserts into a
single bulk_write (ordered=False). First-use index creation is
serialized with a thread lock (double-checked flag) so concurrent
startup calls no longer issue duplicate create_index calls. Tests
carry a shim for mongomock's BulkOperationBuilder, which rejects the
`sort` key pymongo 4.9+ adds to bulk operations.

Addresses gemini-code-assist review comments on google#7533.
…b-session-service

# Conflicts:
#	docs/guides/integrations/mongodb/mongodb_toolset/index.md
#	src/google/adk/integrations/mongodb/README.md
#	tests/unittests/integrations/mongodb/test_search_tool.py
These local-only sample/utility files (mongodb_samples/, .codegraph/,
setup_mongodb_sample.py) were never meant to be committed; a git add -A
in the merge commit included them by mistake. They stay on disk, just
untracked again.
# Conflicts:
#	src/google/adk/integrations/mongodb/README.md
copybara-service Bot pushed a commit that referenced this pull request Oct 23, 2026
Merge #7512

PiperOrigin-RevId: 1005681851
@adk-bot

adk-bot commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Thank you @theshanbhag for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit cbca417.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 23, 2026
@adk-bot adk-bot closed this Oct 23, 2026
adk-bot pushed a commit that referenced this pull request Oct 23, 2026
Merge #7512

PiperOrigin-RevId: 1005681851
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants