HugeGraph Python Client Quick Start
hugegraph-python-client is the Python SDK for HugeGraph. It manages schemas, reads and writes graph data, and executes Gremlin queries. HugeGraph-LLM and HugeGraph-ML also use this client.
The module lives in the hugegraph-ai repository under hugegraph-python-client/. The import name is pyhugegraph.
Requirements
- Python 3.9 or later for the client itself. The HugeGraph-AI workspace requires Python 3.10 or later, and CI runs the client tests on 3.10 and 3.11.
- HugeGraph Server 1.5.0 or later. The client refuses to connect to older servers; use client v1.3.x for those.
uv(recommended) orpip
Runtime dependencies are decorator, requests, setuptools, urllib3 and rich.
Installation
The released package is published on PyPI as hugegraph-python:
The PyPI release lags behind the repository. In the source tree the distribution is declared as
hugegraph-python-clientand versioned with the rest of HugeGraph-AI, so install from source if you need the newest code.
To use the latest repository code, sync the workspace from the root of the HugeGraph-AI repository. hugegraph-python-client is a workspace member exposed through the python-client extra, so plain uv sync does not pull it in:
Connect and Write Data
Client Parameters
PyHugeClient(url, graph, user, pwd, graphspace=None, timeout=None)
| Parameter | Type | Default | Description |
|---|---|---|---|
url | str | required | Base URL of HugeGraph Server. If the value has no scheme, http:// is prepended, so 127.0.0.1:8080 also works. |
graph | str | required | Graph name. This is the second positional parameter. |
user | str | required | Username, sent as HTTP basic auth. |
pwd | str | required | Password, sent as HTTP basic auth. |
graphspace | str or None | None | GraphSpace name. See below for how None is resolved. |
timeout | tuple[float, float] or None | None | (connect, read) timeouts in seconds. None becomes (0.5, 15.0). |
Every HTTP session retries three times with a 0.1 backoff factor on 500, 502 and 504 responses.
Server Version and GraphSpace
The client resolves GraphSpace at construction time:
- A non-empty
graphspacestring turns GraphSpace mode on directly. - Otherwise the client sends
GET {url}/versionsand readsversions.core. - A server older than 1.5.0 raises
RuntimeErrorasking you to upgrade the server or use client v1.3.x. - A server newer than 1.5.0 gets
graphspaceset toDEFAULTand GraphSpace mode turned on, with a warning in the log. A server at exactly 1.5.0 keeps GraphSpace mode off. - If the probe fails for network reasons, GraphSpace mode stays off.
The mode decides the request prefix: /graphspaces/<graphspace>/graphs/<graph>/... when GraphSpace is on, /graphs/<graph>/... when it is off.
Managers on the Client
Each accessor builds its manager lazily and gives it a dedicated HTTP session.
| Accessor | Manager | Covers |
|---|---|---|
client.schema() | SchemaManager | Property keys, vertex labels, edge labels, index labels |
client.graph() | GraphManager | Vertex and edge CRUD, batch writes, paging |
client.gremlin() | GremlinManager | Gremlin execution |
client.graphs() | GraphsManager | Graph list, graph info, config, clear data |
client.traverser() | TraverserManager | Traversal and path algorithms |
client.variable() | VariableManager | Graph variables |
client.task() | TaskManager | Async task list, query, cancel, delete |
client.auth() | AuthManager | Users, groups, targets, belongs, accesses |
client.metrics() | MetricsManager | Server metrics |
client.version() | VersionManager | Server version |
RankManager, RebuildManager and ServicesManager also ship in pyhugegraph.api, but PyHugeClient does not expose accessors for them yet; construct them directly with a session if you need them.
Common Operations
Build the Schema
The schema builders are fluent. Call create() last, or append(), eliminate() and remove() to change an existing definition.
Query the Schema
Read, Update and Delete Graph Data
The graph API takes property dictionaries, not chained property builders:
addVertex returns a VertexData with id, label, type and properties. addEdge returns an EdgeData with id, label, type, outV, outVLabel, inV, inVLabel and properties.
Vertex ids passed to the client may be strings, integers or uuid.UUID values. Booleans are rejected, and integers must fit the Java signed long range.
Batch Writes
addVertices takes (label, properties) pairs, and addEdges takes (label, out_id, in_id, out_label, in_label, properties) tuples. Both return objects that carry only the generated ids.
Paging and Conditional Queries
Execute Gremlin
exec binds the graph and g aliases for you, based on the graph name and the resolved GraphSpace, and returns the result field of the server response. A response missing requestId, status or result raises ResponseParseError.
Traverse the Graph
TraverserManager wraps the server traverser endpoints. Its methods use snake_case.
The POST-based variants take request bodies: advanced_paths, customized_paths, template_paths, customized_crosspoints and fusiform_similarity.
Graph Variables
Async Tasks
Server Metrics and Graph Info
Authentication and Authorization
AuthManager follows the server routing: users, targets, belongs and accesses are mounted under /graphspaces/{graphspace}/auth/..., while groups stay at the server-level /auth/groups. On HugeGraph 1.7.0 and later a graphspace must be resolved, otherwise these calls raise ValueError before any request is sent.
Method Naming
Manager methods written in camelCase, such as addVertex and getVertexById, also get a snake_case alias generated at construction time. graph.add_vertex(...) and graph.addVertex(...) reach the same method. The camelCase spellings are marked deprecated in the debug log, so prefer snake_case in new code.
Error Handling
Exceptions live in pyhugegraph.utils.exceptions:
| Exception | Raised when |
|---|---|
NotAuthorizedError | The server answers 401 |
NotFoundError | The server answers 404, or a required argument is missing |
ServerError | Any other non-2xx response, with the server message attached |
ResponseParseError | A successful response cannot be parsed into the expected shape |
ServiceUnavailableError | The server reports ServiceUnavailableException |
InvalidParameterError, CreateError, RemoveError, UpdateError, DataFormatError | Raised by individual builders and structures |
Request and response bodies are logged with password, token and secret values redacted.
API parameters may change with the HugeGraph REST API version. If an interface is incompatible, first check the REST API documentation for the current server version and the client test cases.
Development Checks
Run formatting and static checks from the root of the HugeGraph-AI repository:
Run the tests the same way CI does:
CI runs the integration job against the hugegraph/hugegraph:1.7.0 image. HUGEGRAPH_GRAPHSPACE is also read when you need a non-default space.
The source code and tests are under hugegraph-python-client/src/pyhugegraph/ and hugegraph-python-client/src/tests/. A runnable example is at hugegraph-python-client/src/pyhugegraph/example/hugegraph_example.py.