fix: sanitize hyphens in Cypher relationship names to prevent syntax errors #3839
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.
Description
Adds hyphen (
-) sanitization tosanitize_relationship_for_cypher()function to prevent Neo4j Cypher syntax errors when relationship names contain hyphens.Problem: The
sanitize_relationship_for_cypher()function inmem0/memory/utils.pyhandles 40+ special characters but was missing hyphen sanitization. This caused Cypher syntax errors when mem0's LLM extracted relationship names with hyphens from natural language (e.g., "non-compliant", "sub-category", "co-worker").Error Example:
neo4j.exceptions.CypherSyntaxError: Invalid input '-': expected a parameter...
"MERGE (source)-[r:non-compliant]->(destination)"
^
Solution: Added
"-": "_dash_"to thechar_mapfollowing the existing naming convention (similar to"/": "_slash_","+": "_plus_").Impact: Common use cases with hyphenated terms now work correctly with graph memory enabled.
Fixes #3838
Type of change
How Has This Been Tested?
Manual Testing:
"Mark all Y things as non-compliant"MERGE ()-[r:non-compliant]->()non_dash_compliantTest Configuration:
Verified the sanitization follows existing patterns and doesn't break other character replacements.
Checklist:
Maintainer Checklist