Graph¶
Graph is the mutable embedded graph database. Graph() creates an in-memory
graph; Graph(path) or Graph.open(path) opens a SQLite-backed graph.
Method Guides¶
| Methods | Design reference |
|---|---|
neighbors, k_hop, frontier |
Neighbor expansion and k-hop traversal |
bfs |
Breadth-first search |
shortest_path |
Weighted shortest path |
connected_components |
Connected components |
pagerank |
PageRank |
random_walk |
Random walk |
propagate, local_propagate |
Sparse probability transfer |
compute_batch |
Batch compute |
query, query_schema |
Structured path-pattern query DSL |
schema, stats |
Graph introspection for Text2Query, GraphRAG planning, and local diagnostics |
retrieve_context |
GraphRAG retrieval |
import_nodes_csv, import_edges_csv, import_nodes_jsonl, import_edges_jsonl, export_nodes_jsonl, export_edges_jsonl, export_query_rows_jsonl |
Local CSV/JSONL import and export helpers |
create_fulltext_index, drop_fulltext_index, fulltext_indexes, rebuild_fulltext_index, search_text |
Full-text search |
create_vector_index, drop_vector_index, vector_indexes, upsert_vector, upsert_vectors, get_vector, delete_vector, delete_vectors, search_vector, search_vectors |
Vector search |
update_node, update_edge, delete_node, delete_edge |
Persistence and graph mutations |
cypher, transaction |
Cypher compatibility |
add_variable, add_factor_table, add_cpd, add_evidence, compile_active_subgraph, belief_propagation, posterior |
Belief propagation |
compact, open |
Persistence |
Reference¶
tonggraph.Graph ¶
Mutable embedded graph database.
Graph() creates an in-memory graph. Graph(path) or Graph.open(path)
opens a SQLite-backed graph at path and persists metadata, properties,
probabilistic records, and compacted compute segments.
Create an in-memory graph or open a SQLite-backed graph.
add_node ¶
add_node(external_id: str | None = None, labels: Sequence[str] | None = None, properties: Properties | None = None) -> int
Add a node and return its internal ID.
add_edge ¶
Add a directed edge and return its internal ID.
add_nodes ¶
Atomically add node records and return their internal IDs.
add_edges ¶
Atomically add directed edge records and return their internal IDs.
update_node ¶
update_node(node_id: int, *, external_id: str | None = None, add_labels: Sequence[str] | None = None, remove_labels: Sequence[str] | None = None, set_properties: Properties | None = None, remove_properties: Sequence[str] | None = None) -> Node
Update a node and return its new record.
update_edge ¶
update_edge(edge_id: int, *, set_properties: Properties | None = None, remove_properties: Sequence[str] | None = None) -> Edge
Update an edge and return its new record.
delete_node ¶
Delete a node, optionally deleting incident edges.
create_fulltext_index ¶
create_fulltext_index(name: str, properties: Sequence[str], target: str = 'node', tokenizer: str = 'unicode61') -> None
Create a named node or edge full-text index.
drop_fulltext_index ¶
Drop a full-text index and its derived rows.
fulltext_indexes ¶
Return named full-text index definitions ordered by name.
rebuild_fulltext_index ¶
Rebuild one full-text index or every index.
search_text ¶
search_text(index_name: str, query: str, mode: str = 'all', labels: Sequence[str] | None = None, edge_type: str | None = None, properties: Properties | None = None, limit: int = 20, offset: int = 0) -> list[dict[str, Any]]
Search a named full-text index.
create_vector_index ¶
create_vector_index(name: str, dimensions: int, target: str = 'node', metric: str = 'cosine', model: str | None = None, model_version: str | None = None) -> None
Create a named node or edge vector index.
drop_vector_index ¶
Drop a vector index and all vectors stored in it.
upsert_vector ¶
Insert or replace one entity vector.
upsert_vectors ¶
Atomically insert or replace multiple entity vectors.
delete_vector ¶
Idempotently delete one entity vector.
delete_vectors ¶
Idempotently delete multiple entity vectors atomically.
retrieve_context ¶
retrieve_context(*, text_query: str | None = None, text_index: str | None = None, vector_query: Sequence[float] | None = None, vector_index: str | None = None, labels: Sequence[str] | None = None, edge_type: str | None = None, properties: Properties | None = None, radius: int = 1, direction: str = 'both', limit: int = 20, text_weight: float = 1.0, vector_weight: float = 1.0, graph_weight: float = 0.1) -> list[dict[str, Any]]
Return ranked context rows from text/vector candidates plus graph expansion.
import_nodes_csv ¶
Import nodes from CSV and return created node IDs.
import_edges_csv ¶
Import edges from CSV and return created edge IDs.
import_nodes_jsonl ¶
Import nodes from JSONL and return created node IDs.
import_edges_jsonl ¶
Import edges from JSONL and return created edge IDs.
export_nodes_jsonl ¶
Export nodes to JSONL.
export_edges_jsonl ¶
Export edges to JSONL.
export_query_rows_jsonl ¶
Export query or Cypher rows to JSONL.
cypher ¶
cypher(query: str, parameters: Mapping[str, Any] | None = None, profile: bool = False) -> CypherResult
Run a Cypher query in an autocommit transaction.
transaction ¶
Create an explicit staged Cypher transaction.
propagate ¶
propagate(seeds: Mapping[int, float], steps: int, edge_property: str = 'probability', damping: float = 1.0, edge_type: str | None = None) -> dict[int, float]
Transfer probability mass over outgoing edges for a fixed number of steps.
local_propagate ¶
local_propagate(seeds: Mapping[int, float], radius: int = 2, query_nodes: Sequence[int] | None = None, edge_type: str | None = None, edge_property: str = 'probability', damping: float = 1.0) -> dict[int, float]
Transfer probability only inside a radius-limited active graph neighborhood.
add_variable ¶
add_variable(domain: str, owner_id: int | None = None, prior: Properties | None = None, posterior: Properties | None = None, states: Sequence[str] | None = None) -> int
Add a finite discrete variable and return its ID.
add_factor ¶
add_factor(input_variables: Sequence[int], output_variables: Sequence[int], function: str, parameters: Properties | None = None) -> int
Add factor metadata and return its ID.
add_factor_table ¶
Add a dense factor table for ordered variables and return its factor ID.
add_cpd ¶
Add a conditional probability table for variable_id and parents.
add_evidence ¶
Persist evidence metadata for a variable and return the evidence ID.
add_trace ¶
Persist an inference trace metadata record and return its ID.
compile_active_subgraph ¶
compile_active_subgraph(query_variables: Sequence[int], evidence: Mapping[int, str] | None = None, radius: int = 2, max_nodes: int = 10000, max_factors: int = 50000) -> dict[str, Any]
Compile a radius-limited inference subgraph around queries and evidence.
belief_propagation ¶
belief_propagation(query_variables: Sequence[int] | None = None, evidence: Mapping[int, str] | None = None, radius: int = 2, max_iters: int = 1000, tolerance: float = 1e-06, damping: float = 0.2, persist: bool = False) -> dict[str, Any]
Run residual asynchronous sum-product belief propagation.
posterior ¶
Return the current posterior distribution for a variable.
vector_indexes ¶
Return named vector index definitions ordered by name.
get_vector ¶
Return an entity vector, raising KeyError when it is absent.
search_vector ¶
search_vector(index_name: str, query_vector: Sequence[float], labels: Sequence[str] | None = None, edge_type: str | None = None, properties: Properties | None = None, min_score: float | None = None, limit: int = 20, offset: int = 0) -> list[dict[str, Any]]
Search a named vector index by exact similarity.
search_vectors ¶
search_vectors(index_name: str, query_vectors: Sequence[Sequence[float]], labels: Sequence[str] | None = None, edge_type: str | None = None, properties: Properties | None = None, min_score: float | None = None, limit: int = 20, offset: int = 0) -> list[list[dict[str, Any]]]
Search a named vector index for multiple query vectors.
schema ¶
Return graph schema metadata for Text2Query and retrieval planning.
get_node ¶
Return a node record by ID.
Raises:
| Type | Description |
|---|---|
KeyError
|
If the node does not exist. |
get_edge ¶
Return an edge record by ID.
Raises:
| Type | Description |
|---|---|
KeyError
|
If the edge does not exist. |
get_variable ¶
Return a variable record by ID.
Raises:
| Type | Description |
|---|---|
KeyError
|
If the variable does not exist. |
get_factor ¶
Return a factor record by ID.
Raises:
| Type | Description |
|---|---|
KeyError
|
If the factor does not exist. |
get_evidence ¶
Return an evidence record by ID.
Raises:
| Type | Description |
|---|---|
KeyError
|
If the evidence record does not exist. |
get_trace ¶
Return a trace record by ID.
Raises:
| Type | Description |
|---|---|
KeyError
|
If the trace record does not exist. |
get_node_id ¶
Look up an internal node ID by external ID.
nodes_with_property ¶
Return node IDs that contain a property key, optionally filtered by value.
edges_with_property ¶
Return edge IDs that contain a property key, optionally filtered by value.
neighbors ¶
Return adjacent node IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
int
|
Node to expand. |
required |
direction
|
str
|
|
'out'
|
edge_type
|
str | None
|
Optional edge-type filter. |
None
|
k_hop ¶
Return nodes reached within hops traversal steps, excluding start.
frontier ¶
frontier(starts: Sequence[int], steps: int, direction: str = 'out', edge_type: str | None = None) -> list[int]
Return only the nodes reached at the final traversal step.
bfs ¶
bfs(start: int, direction: str = 'out', edge_type: str | None = None, max_depth: int | None = None) -> list[int]
Run breadth-first search and return visited nodes in traversal order.
shortest_path ¶
shortest_path(start: int, target: int, direction: str = 'out', edge_type: str | None = None, weight_property: str | None = None) -> dict[str, Any] | None
Return the shortest path and distance, or None when unreachable.
connected_components ¶
Return weakly connected components using both incoming and outgoing edges.
pagerank ¶
pagerank(iterations: int = 20, damping: float = 0.85, tolerance: float | None = None, edge_type: str | None = None) -> dict[int, float]
Return PageRank scores keyed by node ID.
random_walk ¶
random_walk(start: int, steps: int, direction: str = 'out', edge_type: str | None = None, seed: int | None = None) -> list[int]
Run a random walk and return the path, including the start node.
subgraph ¶
Return a snapshot containing selected nodes and internal edges.
compute_batch ¶
Run multiple compute jobs and return results in input order.
query ¶
Run a structured path-pattern query and return alias-to-ID row bindings.