fix: prevent in-place mutation of metadata in _create_memory#4529
Merged
kartik-mem0 merged 1 commit intomainfrom Mar 25, 2026
Merged
fix: prevent in-place mutation of metadata in _create_memory#4529kartik-mem0 merged 1 commit intomainfrom
kartik-mem0 merged 1 commit intomainfrom
Conversation
…d _create_procedural_memory Addresses #2648. The metadata dict passed by callers was mutated in-place inside _create_memory and _create_procedural_memory (both sync and async), causing unsafe behavior under concurrent/async usage. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Closed
14 tasks
kartik-mem0
approved these changes
Mar 25, 2026
farrrr
added a commit
to farrrr/mem0
that referenced
this pull request
Mar 25, 2026
* feat/falkordb-graph-store: feat(falkordb): per-user graph isolation using native multi-graph test+docs(falkordb): add unit tests, integration docs, and dependency fix(falkordb): handle dict and Pydantic embedder config in __init__ style(falkordb): backtick-escape __Entity__ label in Cypher queries feat(falkordb): add fallback LLM for graph entity/relation extraction perf(falkordb): batch embedding for entity writes fix(falkordb): add defensive validation for incomplete LLM entity output feat(falkordb): add soft-delete relationships and timestamps refactor(falkordb): use list-based MERGE property assembly in _add_entities feat(graph): add FalkorDB as graph store provider fix: prevent in-place mutation of metadata in _create_memory (mem0ai#4529)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Closes #2648
The
metadatadict passed by callers to_create_memoryand_create_procedural_memory(both sync and async) was mutated in-place. These methods directly set keys likedata,hash,created_at, andmemory_typeon the caller's original dict object.This causes two problems:
asyncio.gather), one call's mutations bleed into another, producing corrupted payloads.Affected methods
_create_memoryMemorymetadata["data"] = ...,metadata["hash"] = ...,metadata["created_at"] = ..._create_memoryAsyncMemory_create_procedural_memoryMemorymetadata["memory_type"] = ..._create_procedural_memoryAsyncMemoryNote:
_update_memoryand_add_to_vector_storealready usedeepcopyand are not affected.Solution
Use
deepcopy(metadata)at the entry point of each affected method before any mutation, storing the result in anew_metadatalocal variable. The caller's original dict is never touched.This is a minimal, backward-compatible change — no caller reads back from the metadata dict after passing it to these methods.
Testing
Added 12 new tests in
TestMetadataNotMutatedcovering:_create_memory,_update_memory, and_add_to_vector_store(sync + async)_create_memorycalls and verify it remains unchanged, while each call stores the correct distinct payloaddeepcopyprotects nested dicts and lists inside metadatadata,hash,created_at,role,actor_id, custom fields)roleandactor_idfrom metadata flow through correctly toadd_historyafter deepcopymetadata=Nonestill works correctly_add_to_vector_store(infer=False)with multiple messages doesn't leak metadata between iterations (sync + async)All 109 existing + new tests pass.
🤖 Generated with Claude Code