Skip to content

feat: add Apache AGE graph store support#4448

Merged
kartik-mem0 merged 3 commits intomainfrom
feat/apache-age-graph-store
Mar 20, 2026
Merged

feat: add Apache AGE graph store support#4448
kartik-mem0 merged 3 commits intomainfrom
feat/apache-age-graph-store

Conversation

@utkarsh240799
Copy link
Copy Markdown
Contributor

@utkarsh240799 utkarsh240799 commented Mar 20, 2026

Description

Add Apache AGE as a graph store provider for mem0's graph memory, providing an open-source PostgreSQL-based alternative to Neo4j. Apache AGE adds graph database capabilities (Cypher queries) to PostgreSQL, letting users run graph + relational + vector operations on a single Postgres instance.

Closes #3292

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

62 tests total — 19 unit + 43 end-to-end integration tests, all passing.

Unit tests (tests/memory/test_apache_age_memory.py — 19 tests)

Run without any external dependencies (all mocked):

  • TestCosineSimilarity (3) — identical, orthogonal, and zero vectors
  • TestRetrieveNodesFromData (4) — normal extraction, malformed entity handling, missing keys, null tool_calls
  • TestEstablishNodesRelationsFromData (3) — null LLM response, empty tool_calls, valid entities
  • TestRemoveSpacesFromEntities (1) — space/case sanitization
  • TestFindSimilarNode (3) — empty graph, threshold matching, agent_id filtering
  • TestDeleteAll (1) — commit called
  • TestGetAll (2) — result formatting, limit enforcement
  • TestAdd (1) — orchestration returns correct shape
  • TestSearch (1) — empty search output

E2E integration tests (tests/memory/test_apache_age_e2e.py — 43 tests)

Run against a real Apache AGE instance via Docker (docker run -d -p 15432:5432 apache/age):

  • TestExecCypher (3) — vertex creation/return, scalar column mapping, empty results
  • TestMergeNode (3) — first-create, idempotent merge with mentions increment, agent_id storage
  • TestRelationships (2) — single and multiple relationship creation with type(r) queries
  • TestEmbeddingSimilarity (5) — embedding roundtrip, exact match, threshold filtering, closest-pick, user_id isolation
  • TestPublicAPICRUD (5) — get_all, user isolation, limit, delete_all, reset
  • TestDeleteEntities (1) — selective relationship deletion preserving other relationships
  • TestAddEntities (3) — new entity creation, merge to existing similar node, multiple relationships
  • TestSearchGraphDB (2) — similarity-based relationship discovery, empty results
  • TestAddSearchIntegration (2) — full add()→search()→delete_all() cycle, relationship update (delete old + add new)
  • TestMultiTenantIsolation (1) — cross-user data isolation, delete doesn't affect other users
  • TestAgentRunIdFiltering (4) — get_all/delete_all with agent_id filter, get_all/delete_all with run_id filter
  • TestEdgeCases (7) — underscored names, apostrophe (known driver limitation), empty graph operations, idempotent merge, bidirectional relationships, self-referencing relationships
  • TestSearchGraphDBFiltering (2) — search with agent_id, _find_similar_node with run_id
  • TestFullLifecycleWithAgentId (1) — complete add→get_all→search→delete_all cycle scoped by agent_id
  • TestMergeNodeTimestamp (1) — created timestamp preserved across merges

Test categories

  • Unit Test
  • Integration Test

Files Changed

Usage

from mem0 import Memory

config = {
    "graph_store": {
        "provider": "apache_age",
        "config": {
            "host": "localhost",
            "port": 5432,
            "database": "mem0_db",
            "username": "mem0_user",
            "password": "mem0_pass",
            "graph_name": "mem0_graph",
        },
    },
}

m = Memory.from_config(config_dict=config)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • 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

Maintainer Checklist

Add Apache AGE as a graph store provider for graph memory operations,
enabling users to run graph queries on PostgreSQL without a separate
graph database.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@kartik-mem0 kartik-mem0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution — the test coverage is excellent (62 tests) and the code follows our existing patterns well. A few things to address:

Must fix:

  1. get_all() — add LIMIT to the Cypher query instead of slicing in Python (results[:limit]). See how graph_memory.py and kuzu_memory.py both use LIMIT $limit in the query. This matters at scale.

  2. _delete_entities / _add_entities — fix partial commit risk. Currently self.ag.commit() is called inside the loop after each item. If the 2nd of 3 operations fails, the 1st is already committed, leaving the graph in an inconsistent state. Move the commit after the loop completes, or add rollback on failure.

  3. _merge_node() — combine into a single query. The agent_id and run_id SET clauses (lines 283-292) should be part of the initial MERGE query, not separate round-trips. That's up to 3 queries per node merge when it should be 1.

Nice to have:

  • Consider adding a close() method or __del__ to clean up the self.ag connection.
  • The similarity search logic in _find_similar_node and _search_graph_db is duplicated — could be extracted into a shared helper.

1. get_all(): use LIMIT in Cypher query instead of Python slicing
2. _delete_entities/_add_entities: single commit after loop with
   rollback on failure to prevent partial commits
3. _merge_node(): combine agent_id/run_id into single SET query
   (was up to 3 round-trips, now always 1)
4. Add close() method for connection cleanup
5. Extract _get_similar_nodes() helper to deduplicate similarity logic

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kartik-mem0 kartik-mem0 merged commit 305ce7b into main Mar 20, 2026
9 checks passed
jamebobob pushed a commit to jamebobob/mem0-vigil-recall that referenced this pull request Mar 29, 2026
Co-authored-by: utkarsh240799 <utkarsh240799@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

Add Apache AGE database support for Graph operations

2 participants