Skip to content

fix: preserve custom metadata when updating memory#4495

Merged
kartik-mem0 merged 3 commits intomainfrom
fix/preserve-metadata-on-memory-update
Mar 24, 2026
Merged

fix: preserve custom metadata when updating memory#4495
kartik-mem0 merged 3 commits intomainfrom
fix/preserve-metadata-on-memory-update

Conversation

@kartik-mem0
Copy link
Copy Markdown
Contributor

@kartik-mem0 kartik-mem0 commented Mar 23, 2026

Description

MongoDB's update() method in the vector store used {"$set": {"payload": new_payload}} which replaces the entire payload document on update, causing all existing metadata fields (e.g.,
bucket_id, category, source, context_id) to be silently lost.

The fix uses MongoDB dot notation — {"$set": {"payload.data": ..., "payload.hash": ...}} — which only updates the specified fields and preserves everything else. This is the documented MongoDB
behavior
for partial updates on embedded documents.

Fixes #3966

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test

3 tests added/updated in tests/vector_stores/test_mongodb.py:

  • test_update — updated to verify $set uses dot notation (payload.key) instead of full replacement (payload)
  • test_update_payload_only_uses_dot_notation — verifies only specified fields appear in $set, no full payload key
  • test_update_vector_only_does_not_touch_payload — verifies vector-only updates don't touch any payload fields

All 39 tests pass locally (21 in test_memory.py + 18 in test_mongodb.py).

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • 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
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

@kartik-mem0 kartik-mem0 marked this pull request as draft March 23, 2026 06:06
@kartik-mem0 kartik-mem0 marked this pull request as ready for review March 23, 2026 06:21
@utkarsh240799
Copy link
Copy Markdown
Contributor

I think the issue is prevalent in other vector stores as well. On exploration I found that the same metadata loss still happens on Qdrant, Pinecone, Milvus, PGVector, Supabase, and Redis. If that's the case, I feel the better fix would be to have to have the change in _update_memory() in main.py(line ~1257). Something like starting from deepcopy(existing_memory.payload) and overlaying the new fields on top

It would be great if you can check this out once @kartik-mem0 .

@kartik-mem0
Copy link
Copy Markdown
Contributor Author

@whysosaket merging this for now but there will be a follow up for other providers where we will test each providers and see if this problem exist for them or not.

current scope fixes the mongodb part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB Implementation Bug: update replaces metadata entirely

2 participants