Graph databases. Pure Python.
Use GQLAlchemy to connect to Memgraph, map graph entities to Python objects, and query your data without writing Cypher. Full access to graph algorithms, streaming, and ML from Python.
from gqlalchemy import Memgraph, Node, Field, match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
from typing import Optional
memgraph = Memgraph("127.0.0.1", 7687)
# Define graph model as Python classes
class Account(Node):
account_id: str = Field(index=True, unique=True, db=memgraph)
risk_score: Optional[float]
# Query builder - no Cypher needed
results = list(
match()
.node(labels="Account", variable="a")
.to(relationship_type="TRANSFER_TO")
.node(labels="Account", variable="b")
.where(item="a.risk_score", operator=Operator.GREATER_THAN,
literal=0.8)
.return_()
.execute()
)Manage graph data as Python objects
GQLAlchemy is Memgraph's object-graph mapper, similar to SQLAlchemy for relational databases. Define your graph schema as Python classes, validate data, and query with a fluent Python query builder instead of writing raw Cypher.
- Map nodes and relationships to Python dataclasses
- Built-in data validation and schema enforcement
- Fluent query builder for complex graph queries
- Access 60+ MAGE graph algorithms from Python
from gqlalchemy import Memgraph, Node, Field, match
from typing import Optional
memgraph = Memgraph("127.0.0.1", 7687)
# OGM - map nodes to Python classes
class Movie(Node):
id: int = Field(index=True, unique=True, db=memgraph)
title: Optional[str]
# Query builder
results = list(
match()
.node(labels="User", variable="u")
.to(relationship_type="RATED")
.node(labels="Movie", variable="m")
.return_()
.execute()
)
for row in results:
print(row["m"])import mgp
import networkx as nx
# Define a custom Cypher procedure in Python
@mgp.read_proc
def betweenness(ctx: mgp.ProcCtx, node: mgp.Vertex
) -> mgp.Record(score=float):
G = nx.Graph()
for v in ctx.graph.vertices():
G.add_node(v.id)
scores = nx.betweenness_centrality(G)
return mgp.Record(score=scores[node.id])Extend Cypher with your own Python logic
When built-in algorithms don't cover your use case, write custom query modules in Python and call them from Cypher. Edit and run them directly from Memgraph Lab without restarting the database.
- Write custom procedures in Python, C, C++, or Rust
- Use igraph and NetworkX inside your modules
- Hot-reload procedures without restarting Memgraph
- Call from Cypher: CALL my_module.procedure()
Build with the community
GQLAlchemy and MAGE are open source. Implement graph algorithms, improve the Python client, and share modules with the community. The library grows when you contribute.
- GQLAlchemy: open source OGM on GitHub
- MAGE: contribute algorithms in Python or Rust
- Discord community for graph analytics developers
- Memgraph Academy: structured Python and graph courses
Object-graph mapper for Memgraph - GQLAlchemy
Write Python instead of Cypher. Model your graph as Python dataclasses with full validation.
60+ graph algorithms, open source - MAGE
PageRank, community detection, GNNs, and more, all callable from Python or Cypher.
Built for the workloads Python developers run.
Performance
Algorithms
Flexibility
Create a graph model from a dataset, run Memgraph with Docker, connect via GQLAlchemy, and perform graph queries, all from a Jupyter Notebook.Watch for free
A Python-first walkthrough for building a fraud detection graph. No Cypher required. GQLAlchemy handles everything from data import to query execution.Read blog