AgensGraph for Python#
agensgraph-python reads and writes an AgensGraph
graph from Python, in blocking or asyncio code. It is built on psycopg 3:
a connection is a psycopg connection, so cursors, COPY, pipelines, LISTEN and two-phase commit
keep working, and Cypher and SQL run on the same connection, in the same transaction.
$ pip install agensgraph-python
import agensgraph
with agensgraph.connect("host=localhost dbname=docs user=agens") as conn:
conn.graph("moviekb")
result = conn.execute_query(
"MATCH (p:person)-[:acted_in]->(m:movie {title: %s}) RETURN p",
("The Matrix",),
)
for (person,) in result.records:
print(person.properties["name"])
A vertex, an edge and a path arrive as Vertex, Edge and Path values, not as text to parse.
Python 3.11 or later; AgensGraph 2.17 or later.
What it does#
Graph values in both of the server’s renderings, hashable and compared on identity, with each property map decoded only when it is read.
Parameters that mean what they say: a string is sent as
text, adictas a property map, and a value that would be read the wrong way is refused before it is sent.Bulk loading with binary
COPY, and upserts that write only what is not already there.Arrow, pandas and polars export, from the bytes on the wire, and loading from a DataFrame.
Embedding vectors through pgvector, with index and search built to agree.
Schema inspection and declaration: read what a graph holds, and reconcile it with what it should.
Pools, deadlines and retries for a service: one budget for the wait and the statement, a pool that retires every connection after a restart, and failures classified by what to do next.
Untrusted statements, such as a language model’s query, run so that the server refuses every write.
Where to go next#
Install the driver, connect, and read and write the example graph.
Connecting, running statements, parameters, graph values and transactions.
Bulk loading, DataFrames and Arrow, embedding vectors, and notifications.
Inspecting a graph’s catalogs, and declaring the labels, indexes and constraints it needs.
Pooling, timeouts, retries, untrusted statements, errors and observability.
Every class and function, server compatibility, and the changelog.
Every example in this manual runs against the AgensGraph manual’s example dataset, a small graph of films and the people who made them, and is checked against a live server.