Skip to content

Conversation

@mpotter
Copy link

@mpotter mpotter commented Nov 23, 2025

Description

This PR fixes an issue where entity types with special characters (e.g., "Person-Name", "City/State", "Entity@Type#123") would cause Cypher query syntax errors when used as node labels in Neo4j graph database queries.

Changes:

  • Add sanitizeLabel function to replace special characters with underscores
  • Ensures valid Cypher syntax for node labels in Neo4j queries
  • Labels are now sanitized to use only alphanumeric characters and underscores, matching the pattern used in the Python implementation

Implementation:

  • Modified _addEntities method in graph_memory.ts to sanitize sourceType and destinationType before using them in Cypher queries
  • The sanitization function uses regex pattern /[^A-Za-z0-9_]/g to replace invalid characters with underscores
  • Function is defined at method scope (outside the loop) for efficiency and consistency with existing code patterns

Fixes a similar issue #3020 that was fixed in the python implementation

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test

Added comprehensive unit tests in graph_memory.test.ts covering:

  • Special characters: Entity types like "Person-Name" and "City/State" are sanitized to "Person_Name" and "City_State"
  • Multiple special characters: Labels like "Entity@Type#123" are sanitized to "Entity_Type_123"
  • Edge cases: Labels with only special characters (e.g., "---" → "___") and unknown entity types
  • Normal labels: Labels without special characters (e.g., "Person", "City") remain unchanged

Test Configuration:

  • Tests use mocked Neo4j driver and session to verify Cypher queries contain sanitized labels
  • All 5 test cases pass successfully
  • Tests verify that sanitized labels appear in the generated Cypher queries and original unsanitized labels do not

To reproduce:

cd mem0-ts
npm run test:ts -- graph_memory.test.ts

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
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Made sure Checks passed

- Add sanitizeLabel function to replace special characters with underscores
- Ensures valid Cypher syntax for node labels in Neo4j queries
- Add comprehensive tests for label sanitization covering:
  * Special characters (Person-Name -> Person_Name)
  * Multiple special characters (Entity@Type#123 -> Entity_Type_123)
  * Edge cases (only special characters, unknown types)
  * Normal labels remain unchanged

Fixes issue where entity types with special characters would cause
Cypher query syntax errors. Labels are now sanitized to use only
alphanumeric characters and underscores, matching the pattern used
in the Python implementation.
@CLAassistant
Copy link

CLAassistant commented Nov 23, 2025

CLA assistant check
All committers have signed the CLA.

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.

2 participants