Skip to content

Conversation

@ajatprabha
Copy link

Description

Adds hyphen (-) sanitization to sanitize_relationship_for_cypher() function to prevent Neo4j Cypher syntax errors when relationship names contain hyphens.

Problem: The sanitize_relationship_for_cypher() function in mem0/memory/utils.py handles 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 the char_map following 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Manual Testing:

  1. Created a mem0 Memory instance with Neo4j graph store enabled
  2. Added memory with natural language containing hyphens: "Mark all Y things as non-compliant"
  3. Before fix: Cypher syntax error on MERGE ()-[r:non-compliant]->()
  4. After fix: Relationship created successfully as non_dash_compliant

Test Configuration:

  • Python 3.13
  • Neo4j 5.x (via Docker)
  • Qdrant vector store
  • Azure OpenAI (gpt-5-mini)

Verified the sanitization follows existing patterns and doesn't break other character replacements.

  • Test Script (manually reproduced the issue with real Neo4j instance)
  • Unit Test (should be added in future PR)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas (not needed - follows existing pattern)
  • I have made corresponding changes to the documentation (not applicable)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works (should be added separately)
  • New and existing unit tests pass locally with my changes (no tests exist for this function yet)
  • Any dependent changes have been merged and published in downstream modules (not applicable)
  • I have checked my code and corrected any misspellings

Maintainer Checklist

@CLAassistant
Copy link

CLAassistant commented Dec 16, 2025

CLA assistant check
All committers have signed the CLA.

@ajatprabha
Copy link
Author

Testing Summary

Test Environment

  • Setup: Local mem0 installation via uv pip install -e /path/to/mem0
  • Stack: Qdrant (vector store) + Neo4j (graph store) + Azure OpenAI (gpt-5-mini)
  • Test script: Custom memory spike with 8 demos covering various relationship extraction scenarios

Test Cases

1. Explicit Hyphenated Relationship Types

Forced the LLM to extract relationship types containing hyphens by using compound terms in natural language:

Test Case: "Acme Corp is a sub-contractor of Mega Industries for the Q4 project"

Before fix:
neo4j.exceptions.CypherSyntaxError: Invalid input '-': expected a parameter...
"MERGE (source)-[r:sub-contractor]->(destination)"
^

After fix:

# Successfully created relationship with sanitized type
MATCH ()-[r]->() WHERE type(r) = 'sub_dash_contractor'
# Returns: 1 relationship

2. Additional Test Scenarios

- "follow-up": "Invoice INV-002 is a follow-up to Invoice INV-001"
- LLM rephrased naturally to follows_up_on (no hyphen in output)
- "by-product": "Chemical XYZ is a by-product of manufacturing process ABC"
- LLM rephrased to is_by_product_of (no hyphen in output)

3. Entity Name Verification

Confirmed that entity names (node properties) with hyphens work correctly:
MATCH (n)-[r]->(m) RETURN n.name, type(r), m.name
# Results show entity names like "invoice_inv-002", "non-ar" work fine
# Hyphens in property values don't cause Cypher syntax errors

Verification Steps

1. Run memory extraction: Created memories from conversations with hyphenated terms
2. Check Neo4j database:
docker exec neo4j cypher-shell "MATCH ()-[r]->() RETURN DISTINCT type(r)"
3. Confirm sanitization: Found sub_dash_contractor relationship type

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.

Bug: sanitize_relationship_for_cypher() doesn't handle hyphens causing Neo4j Cypher syntax errors

2 participants