| --- |
| title: Configurable Graph Threshold |
| description: "Configure the graph store threshold parameter to control how strictly nodes are matched during data ingestion." |
| --- |
|
|
| |
|
|
| The graph store threshold parameter controls how strictly nodes are matched during graph data ingestion based on embedding similarity. This feature allows you to customize the matching behavior to prevent false matches or enable entity merging based on your specific use case. |
|
|
| |
|
|
| Add the `threshold` parameter to your graph store configuration: |
|
|
| ```python |
| from mem0 import Memory |
|
|
| config = { |
| "graph_store": { |
| "provider": "neo4j", |
| "config": { |
| "url": "bolt://localhost:7687", |
| "username": "neo4j", |
| "password": "password" |
| }, |
| "threshold": 0.7 |
| } |
| } |
|
|
| memory = Memory.from_config(config) |
| ``` |
|
|
| |
|
|
| | Parameter | Type | Default | Range | Description | |
| |-----------|------|---------|-------|-------------| |
| | `threshold` | float | 0.7 | 0.0 - 1.0 | Minimum embedding similarity score required to match existing nodes during graph ingestion | |
|
|
| |
|
|
| |
|
|
| Use higher thresholds (0.95-0.99) when working with identifiers that should remain distinct: |
|
|
| ```python |
| config = { |
| "graph_store": { |
| "provider": "neo4j", |
| "config": {...}, |
| "threshold": 0.95 |
| } |
| } |
| ``` |
|
|
| **Example:** Prevents UUID collisions like `MXxBUE18QVBQTElDQVRJT058MjM3MTM4NjI5` being matched with `MXxBUE18QVBQTElDQVRJT058MjA2OTYxMzM` |
|
|
| |
|
|
| Use lower thresholds (0.6-0.7) when entity variations should be merged: |
|
|
| ```python |
| config = { |
| "graph_store": { |
| "threshold": 0.6 |
| } |
| } |
| ``` |
|
|
| **Example:** Merges similar entities like "Bob" and "Robert" as the same person. |
|
|
| |
|
|
| | Use Case | Recommended Threshold | Behavior | |
| |----------|----------------------|----------| |
| | UUIDs, IDs, Keys | 0.95 - 0.99 | Prevent false matches between similar identifiers | |
| | Structured Data | 0.85 - 0.9 | Balanced precision and recall | |
| | General Purpose | 0.7 - 0.8 | Default recommendation | |
| | Natural Language | 0.6 - 0.7 | Allow entity variations to merge | |
|
|
| |
|
|
| |
|
|
| ```python |
| from mem0 import Memory |
|
|
| config = { |
| "graph_store": { |
| "provider": "neo4j", |
| "config": { |
| "url": "bolt://localhost:7687", |
| "username": "neo4j", |
| "password": "password" |
| }, |
| "threshold": 0.98 |
| } |
| } |
|
|
| memory = Memory.from_config(config) |
|
|
| |
| memory.add( |
| [{"role": "user", "content": "MXxBUE18QVBQTElDQVRJT058MjM3MTM4NjI5 relates to Project A"}], |
| user_id="user1" |
| ) |
|
|
| memory.add( |
| [{"role": "user", "content": "MXxBUE18QVBQTElDQVRJT058MjA2OTYxMzM relates to Project B"}], |
| user_id="user1" |
| ) |
| ``` |
|
|
| |
|
|
| ```python |
| config = { |
| "graph_store": { |
| "provider": "neo4j", |
| "config": {...}, |
| "threshold": 0.6 |
| } |
| } |
|
|
| memory = Memory.from_config(config) |
|
|
| |
| memory.add([{"role": "user", "content": "Bob works at Google"}], user_id="user1") |
| memory.add([{"role": "user", "content": "Robert works at Google"}], user_id="user1") |
| ``` |
|
|
| |
|
|
| ```python |
| |
| memory_strict = Memory.from_config({ |
| "graph_store": {"threshold": 0.95} |
| }) |
|
|
| |
| memory_permissive = Memory.from_config({ |
| "graph_store": {"threshold": 0.6} |
| }) |
| ``` |
|
|
| |
|
|
| The threshold parameter works with all graph store providers: |
|
|
| - ✅ Neo4j |
| - ✅ Memgraph |
| - ✅ Kuzu |
| - ✅ Neptune (both Analytics and DB) |
|
|
| |
|
|
| When adding a relation to the graph: |
|
|
| 1. **Embedding Generation**: The system generates embeddings for source and destination entities |
| 2. **Node Search**: Searches for existing nodes with similar embeddings |
| 3. **Threshold Comparison**: Compares similarity scores against the configured threshold |
| 4. **Decision**: |
| - If similarity ≥ threshold: Uses the existing node |
| - If similarity < threshold: Creates a new node |
| |
| ```python |
| # Pseudocode |
| if node_similarity >= threshold: |
| use_existing_node() |
| else: |
| create_new_node() |
| ``` |
|
|
| |
|
|
| |
|
|
| **Symptom**: Expected nodes to merge but they're created separately |
|
|
| **Solution**: Lower the threshold |
| ```python |
| config = {"graph_store": {"threshold": 0.6}} |
| ``` |
|
|
| |
|
|
| **Symptom**: Different entities incorrectly matched as the same node |
|
|
| **Solution**: Raise the threshold |
| ```python |
| config = {"graph_store": {"threshold": 0.95}} |
| ``` |
|
|
| |
|
|
| **Symptom**: `ValidationError: threshold must be between 0.0 and 1.0` |
|
|
| **Solution**: Ensure threshold is in valid range |
| ```python |
| config = {"graph_store": {"threshold": 0.7}} |
| ``` |
|
|
| |
|
|
| - **Default Value**: 0.7 (maintains existing behavior) |
| - **Optional Parameter**: Existing code works without any changes |
| - **No Breaking Changes**: Graceful fallback if not specified |
|
|
| |
|
|
| - [Graph Memory](/platform/features/graph-memory) |
| - [Issue |
|
|