Client API¶
hypabase.client.Hypabase ¶
A hypergraph database client.
The primary interface for creating, querying, and traversing hypergraphs. Supports in-memory and local SQLite backends.
Constructor patterns
Hypabase()— in-memory, ephemeral (SQLite:memory:)Hypabase("file.db")— local persistent SQLite fileHypabase("https://...")— cloud backend (Phase 3, raises NotImplementedError)
Example
close ¶
Close the database connection.
Saves pending changes and releases the SQLite connection. No-op for in-memory instances.
save ¶
Persist current state to SQLite.
No-op for in-memory instances. Normally called automatically after each mutation; use this for explicit manual saves.
database ¶
Return a scoped view into a named namespace.
The returned instance shares the same SQLite connection and stores dict, but reads/writes only the given namespace's data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Namespace name. |
required |
Returns:
| Type | Description |
|---|---|
Hypabase
|
A new Hypabase instance scoped to the namespace. |
databases ¶
List all namespaces.
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of namespace names. |
delete_database ¶
Delete a namespace and all its data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Namespace to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
context ¶
Set default provenance for all edges created within the block.
Edges created inside the context inherit source and confidence
unless overridden per-edge. Contexts can be nested; the innermost wins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Provenance source string (e.g., |
required |
confidence
|
float
|
Default confidence score, 0.0-1.0. |
1.0
|
node ¶
Create or update a node.
If a node with the given ID exists, its type and properties are updated. Otherwise a new node is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Unique node identifier. |
required |
type
|
str
|
Node classification (e.g., |
'unknown'
|
**properties
|
Any
|
Arbitrary key-value metadata stored on the node. |
{}
|
Returns:
| Type | Description |
|---|---|
Node
|
The created or updated Node. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
get_node ¶
Get a node by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
The node ID to look up. |
required |
Returns:
| Type | Description |
|---|---|
Node | None
|
The Node if found, or |
nodes ¶
Query nodes, optionally filtered by type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str | None
|
If provided, return only nodes of this type. |
None
|
Returns:
| Type | Description |
|---|---|
list[Node]
|
List of matching nodes. |
find_nodes ¶
Find nodes matching all specified properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**properties
|
Any
|
Key-value pairs that must match node properties. |
{}
|
Returns:
| Type | Description |
|---|---|
list[Node]
|
List of matching nodes. |
has_node ¶
Check if a node exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
The node ID to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
delete_node ¶
Delete a node by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
The node ID to delete. |
required |
cascade
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
|
delete_node_cascade ¶
Delete a node and all its incident edges.
.. deprecated:: 0.2.0
Use delete_node(id, cascade=True) instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The node ID to delete. |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, int]
|
Tuple of |
edge ¶
edge(
nodes: list[str],
*,
type: str,
directed: bool = False,
source: str | None = None,
confidence: float | None = None,
properties: dict[str, Any] | None = None,
id: str | None = None,
) -> Edge
Create a hyperedge linking two or more nodes in one relationship.
Nodes are auto-created if they don't exist. Provenance values
fall back to the active context() block if not set explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list[str]
|
Node IDs to connect. Must contain at least 2. |
required |
type
|
str
|
Edge type (e.g., |
required |
directed
|
bool
|
If |
False
|
source
|
str | None
|
Provenance source. Falls back to context or |
None
|
confidence
|
float | None
|
Confidence score 0.0-1.0. Falls back to context or |
None
|
properties
|
dict[str, Any] | None
|
Arbitrary key-value metadata. |
None
|
id
|
str | None
|
Optional edge ID. Auto-generated UUID if omitted. |
None
|
Returns:
| Type | Description |
|---|---|
Edge
|
The created Edge. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 nodes or any node ID is empty. |
get_edge ¶
Get an edge by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
The edge ID to look up. |
required |
Returns:
| Type | Description |
|---|---|
Edge | None
|
The Edge if found, or |
edges ¶
edges(
*,
containing: list[str] | None = None,
type: str | None = None,
match_all: bool = False,
source: str | None = None,
min_confidence: float | None = None,
) -> list[Edge]
Query edges by contained nodes, type, source, and/or confidence.
All filters are combined with AND logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
containing
|
list[str] | None
|
Node IDs that must appear in the edge. |
None
|
type
|
str | None
|
Filter to edges of this type. |
None
|
match_all
|
bool
|
If |
False
|
source
|
str | None
|
Filter to edges from this provenance source. |
None
|
min_confidence
|
float | None
|
Filter to edges with confidence >= this value. |
None
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
List of matching edges. |
find_edges ¶
Find edges matching all specified properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**properties
|
Any
|
Key-value pairs that must match edge properties. |
{}
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
List of matching edges. |
has_edge_with_nodes ¶
Check if an edge with the exact vertex set exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_ids
|
set[str]
|
Exact set of node IDs. |
required |
edge_type
|
str | None
|
If provided, also filter by edge type. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
sources ¶
Summarize provenance sources across all edges.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dicts with keys |
list[dict[str, Any]]
|
and |
edges_by_vertex_set ¶
O(1) lookup: find edges with exactly this set of nodes.
Uses the SHA-256 vertex-set hash index for constant-time lookup.
Order of nodes does not matter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list[str]
|
The exact set of node IDs to match. |
required |
Returns:
| Type | Description |
|---|---|
list[Edge]
|
Edges whose node set matches exactly. |
delete_edge ¶
Delete an edge by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
The edge ID to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
neighbors ¶
Find all nodes connected to the given node via shared edges.
The query node itself is excluded from the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The node to find neighbors of. |
required |
edge_types
|
list[str] | None
|
If provided, only traverse edges of these types. |
None
|
Returns:
| Type | Description |
|---|---|
list[Node]
|
List of neighboring nodes. |
paths ¶
paths(
start: str,
end: str,
*,
max_hops: int = 5,
edge_types: list[str] | None = None,
) -> list[list[str]]
Find paths between two nodes through hyperedges.
Uses breadth-first search. Each path is a list of node IDs
from start to end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
str
|
Starting node ID. |
required |
end
|
str
|
Target node ID. |
required |
max_hops
|
int
|
Maximum number of hops (default 5). |
5
|
edge_types
|
list[str] | None
|
If provided, only traverse edges of these types. |
None
|
Returns:
| Type | Description |
|---|---|
list[list[str]]
|
List of paths, where each path is a list of node IDs. |
find_paths ¶
find_paths(
start_nodes: set[str],
end_nodes: set[str],
*,
max_hops: int = 3,
max_paths: int = 10,
min_intersection: int = 1,
edge_types: list[str] | None = None,
direction_mode: str = "undirected",
) -> list[list[Edge]]
Find paths between two groups of nodes through shared edges.
Returns paths as sequences of edges. Supports set-based start/end nodes and configurable overlap requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_nodes
|
set[str]
|
Set of possible starting node IDs. |
required |
end_nodes
|
set[str]
|
Set of possible ending node IDs. |
required |
max_hops
|
int
|
Maximum path length in edges (default 3). |
3
|
max_paths
|
int
|
Maximum number of paths to return (default 10). |
10
|
min_intersection
|
int
|
Minimum node overlap between consecutive edges (default 1). |
1
|
edge_types
|
list[str] | None
|
If provided, only traverse edges of these types. |
None
|
direction_mode
|
str
|
|
'undirected'
|
Returns:
| Type | Description |
|---|---|
list[list[Edge]]
|
List of paths, where each path is a list of Edge objects. |
node_degree ¶
Count how many edges touch a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The node to measure. |
required |
edge_types
|
list[str] | None
|
If provided, only count edges of these types. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The degree (edge count) of the node. |
edge_cardinality ¶
Count how many distinct nodes an edge contains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
str
|
The edge to measure. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Count of distinct node IDs in the edge. |
hyperedge_degree ¶
Add up the edge counts of every node in a set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_set
|
set[str]
|
Set of node IDs to aggregate. |
required |
edge_type
|
str | None
|
If provided, only count edges of this type. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Sum of individual node degrees. |
validate ¶
Check the hypergraph for internal consistency.
Returns:
| Type | Description |
|---|---|
ValidationResult
|
A |
to_hif ¶
Export the graph to HIF (Hypergraph Interchange Format).
Returns:
| Type | Description |
|---|---|
dict
|
A dict representing the hypergraph in HIF JSON structure. |
from_hif
classmethod
¶
Build a new Hypabase instance from HIF (Hypergraph Interchange Format) data.
Creates an in-memory instance populated from the HIF structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hif_data
|
dict
|
A dict in HIF JSON format. |
required |
Returns:
| Type | Description |
|---|---|
Hypabase
|
A new Hypabase instance containing the imported data. |
upsert_edge_by_vertex_set ¶
upsert_edge_by_vertex_set(
node_ids: set[str],
edge_type: str,
properties: dict[str, Any] | None = None,
*,
source: str | None = None,
confidence: float | None = None,
merge_fn: Any = None,
) -> Edge
Create or update an edge matched by its exact set of nodes.
Finds an existing edge with the same nodes, or creates a new one. Useful for idempotent ingestion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_ids
|
set[str]
|
Set of node IDs for the edge. |
required |
edge_type
|
str
|
Edge type string. |
required |
properties
|
dict[str, Any] | None
|
Key-value metadata. Merged on update. |
None
|
source
|
str | None
|
Provenance source. Falls back to context or |
None
|
confidence
|
float | None
|
Confidence score 0.0-1.0. Falls back to context or |
None
|
merge_fn
|
Any
|
Optional callable |
None
|
Returns:
| Type | Description |
|---|---|
Edge
|
The created or updated Edge. |
edges_of_node ¶
Get all edges incident to a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The node to query. |
required |
edge_types
|
list[str] | None
|
If provided, only return edges of these types. |
None
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
List of edges containing this node. |
batch ¶
Group write operations and save them all at once.
Reduces disk I/O for bulk inserts. Batches can nest; only the outermost batch triggers a save.
Note
Provides batched persistence, not transaction rollback. If an exception occurs mid-batch, partial in-memory changes remain and are persisted when the batch exits.
stats ¶
Get node and edge counts by type.
Returns:
| Type | Description |
|---|---|
HypergraphStats
|
A |
HypergraphStats
|
|