fix: sanitize hyphens in Neo4j Cypher relationship names#4154
Merged
kartik-mem0 merged 1 commit intomem0ai:mainfrom Mar 25, 2026
Merged
Conversation
Contributor
Author
i have signed it, recheck please |
Hyphens in relationship names (e.g. 'manages_via_low-cost_models') cause Neo4j CypherSyntaxError because '-' is not valid in unquoted Cypher identifiers. Add '-' -> '_' mapping to sanitize_relationship_for_cypher() in utils.py, consistent with how other special characters are already handled. Added tests for the sanitization function.
44265f2 to
03441d0
Compare
utkarsh240799
approved these changes
Mar 25, 2026
This was referenced Mar 25, 2026
This was referenced Mar 26, 2026
farrrr
pushed a commit
to farrrr/mem0
that referenced
this pull request
Mar 27, 2026
Closed
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
When an LLM extracts relationship names containing hyphens (e.g.
manages_via_low-cost_models), Neo4j throws aCypherSyntaxErrorbecause-is not a valid character in unquoted Cypher identifiers:Root Cause
sanitize_relationship_for_cypher()inmem0/memory/utils.pyhandles many special characters (/,@,!, etc.) but does not handle-(hyphen/minus).Fix
Add
"-": "_"to the character mapping insanitize_relationship_for_cypher(), consistent with how other special characters are already handled.Before:
manages_via_low-cost_models→ CypherSyntaxErrorAfter:
manages_via_low-cost_models→manages_via_low_cost_models✅Changes
mem0/memory/utils.py: Add hyphen to sanitization char_map (1 line)tests/memory/test_neo4j_cypher_syntax.py: Add 5 test cases for the sanitization functionTesting
All 5 new tests pass: