fix(memory): centralize entity cleanup and skip malformed LLM relation dicts#4515
Merged
whysosaket merged 1 commit intomem0ai:mainfrom Mar 28, 2026
Conversation
whysosaket
approved these changes
Mar 28, 2026
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.
Linked Issue
Closes #3907
Description
Context — earlier PR (#4184)
PR #4184 fixed
_remove_spaces_from_entitiesonly ingraph_memory.py(Neo4j) by skipping empty/incomplete relation dicts from LLM output, which removedKeyErrorwhen the model returned values like[{}]or dicts missingsource/relationship/destinationand the code implementation was perfect only.Why this PR
The same helper logic existed in four places (
graph_memory,memgraph_memory,kuzu_memory,neptune/base). #4184 only updated Neo4j, so Memgraph, Kuzu, and Neptune could still hit the same failure mode. This PR:mem0.memory.utils.remove_spaces_from_entities.sanitize_relationship=Truefor Neo4j/Memgraph (existing Cypher relationship sanitizer),Falsefor Kuzu/Neptune (plain lower + underscores), matching what each file did before.Closing fix: skip malformed entities in _remove_spaces_from_entities #4184
fix: skip malformed entities in _remove_spaces_from_entities #4184 can be closed as superseded by this PR: it solves the same class of bug across all graph backends and includes unit tests for the shared helper. Thank you to the fix: skip malformed entities in _remove_spaces_from_entities #4184 author for the original Neo4j fix and tests.
Type of Change
Breaking Changes
N/A
Test Coverage
Added
tests/memory/test_memory_utils.pycovering malformed lists, both sanitization modes, and non-dict entries. Ran:pytest tests/memory/test_memory_utils.py tests/memory/test_neo4j_cypher_syntax.py tests/memory/test_memgraph_memory.py -vChecklist