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, a dict as 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#

Get started

Install the driver, connect, and read and write the example graph.

Get started
Using the driver

Connecting, running statements, parameters, graph values and transactions.

Using the driver
Working with data

Bulk loading, DataFrames and Arrow, embedding vectors, and notifications.

Working with data
Schema

Inspecting a graph’s catalogs, and declaring the labels, indexes and constraints it needs.

Schema
Running in production

Pooling, timeouts, retries, untrusted statements, errors and observability.

Running in production
Reference

Every class and function, server compatibility, and the changelog.

Reference

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.