Skip to content

fix: sanitize hyphens in Neo4j Cypher relationship names#4154

Merged
kartik-mem0 merged 1 commit intomem0ai:mainfrom
lamost423:fix/sanitize-hyphen-in-cypher-relationship
Mar 25, 2026
Merged

fix: sanitize hyphens in Neo4j Cypher relationship names#4154
kartik-mem0 merged 1 commit intomem0ai:mainfrom
lamost423:fix/sanitize-hyphen-in-cypher-relationship

Conversation

@lamost423
Copy link
Copy Markdown
Contributor

Problem

When an LLM extracts relationship names containing hyphens (e.g. manages_via_low-cost_models), Neo4j throws a CypherSyntaxError because - is not a valid character in unquoted Cypher identifiers:

neo4j.exceptions.CypherSyntaxError: Invalid input '-': expected a parameter, '&', '*', ':', 'WHERE', ']', '{' or '|'
"MERGE (source)-[r:manages_via_low-cost_models]->(destination)"

Root Cause

sanitize_relationship_for_cypher() in mem0/memory/utils.py handles many special characters (/, @, !, etc.) but does not handle - (hyphen/minus).

Fix

Add "-": "_" to the character mapping in sanitize_relationship_for_cypher(), consistent with how other special characters are already handled.

Before: manages_via_low-cost_models → CypherSyntaxError
After: manages_via_low-cost_modelsmanages_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 function

Testing

All 5 new tests pass:

tests/memory/test_neo4j_cypher_syntax.py::TestSanitizeRelationshipForCypher::test_hyphen_replaced_with_underscore PASSED
tests/memory/test_neo4j_cypher_syntax.py::TestSanitizeRelationshipForCypher::test_multiple_hyphens PASSED
tests/memory/test_neo4j_cypher_syntax.py::TestSanitizeRelationshipForCypher::test_no_special_chars_unchanged PASSED
tests/memory/test_neo4j_cypher_syntax.py::TestSanitizeRelationshipForCypher::test_spaces_not_handled_here PASSED
tests/memory/test_neo4j_cypher_syntax.py::TestSanitizeRelationshipForCypher::test_existing_chars_still_sanitized PASSED

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 27, 2026

CLA assistant check
All committers have signed the CLA.

@lamost423
Copy link
Copy Markdown
Contributor Author

m

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.
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.

4 participants