Embedded single-file property-graph database with vector and BM25 full-text search
Project description
LatticeDB Python Bindings
Python bindings for LatticeDB, an embedded single-file property-graph database with native vector and BM25 full-text search.
Installation
pip install latticedb
The native shared library (liblattice.dylib / liblattice.so) must be available on the system. Install it via the install script or build from source with zig build shared.
Migration note: embedding helpers now live in the dedicated latticedb.embedding module. See ../../docs/client_api_migration.md for the preferred API names and deprecated compatibility aliases.
Installed-prefix workflow:
zig build install --prefix /tmp/lattice-install
export LATTICE_PREFIX=/tmp/lattice-install
Alternatively, discovery can use pkg-config:
export PKG_CONFIG_PATH=/tmp/lattice-install/lib/pkgconfig
Quick Start
import numpy as np
from latticedb import Database
with Database("knowledge.db", create=True, enable_vectors=True, vector_dimensions=4) as db:
# Create nodes, edges, and index content
with db.write() as txn:
alice = txn.create_node(
labels=["Person"],
properties={"name": "Alice", "age": 30},
)
bob = txn.create_node(
labels=["Person"],
properties={"name": "Bob", "age": 25},
)
txn.create_edge(alice.id, bob.id, "KNOWS")
# Index text for full-text search
txn.fts_index(alice.id, "Alice works on machine learning research")
txn.fts_index(bob.id, "Bob studies deep learning and neural networks")
# Store vector embeddings
txn.set_vector(alice.id, "embedding", np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32))
txn.set_vector(bob.id, "embedding", np.array([0.0, 1.0, 0.0, 0.0], dtype=np.float32))
txn.commit()
# Query with Cypher
result = db.query("MATCH (n:Person) WHERE n.age > 20 RETURN n.name, n.age")
for row in result:
print(row)
# Vector similarity search
query_vec = np.array([0.9, 0.1, 0.0, 0.0], dtype=np.float32)
for r in db.vector_search(query_vec, k=2):
print(f"Node {r.node_id}: distance={r.distance:.4f}")
# Full-text search
for r in db.fts_search("machine learning"):
print(f"Node {r.node_id}: score={r.score:.4f}")
# Fuzzy search (typo-tolerant)
for r in db.fts_search_fuzzy("machin lerning"):
print(f"Node {r.node_id}: score={r.score:.4f}")
API Reference
Database
Database(
path: str | Path,
*,
create: bool = False, # Create if doesn't exist
read_only: bool = False, # Open in read-only mode
cache_size_mb: int = 100, # Page cache size
enable_vectors: bool | None = None, # Preferred vector config flag
enable_vector: bool | None = None, # Deprecated compatibility alias
vector_dimensions: int = 128 # Vector dimensions
)
Methods
open()/close()- Open/close the database (also works as context manager)read()- Start a read-only transaction (context manager)write()- Start a read-write transaction (context manager)query(cypher, parameters=None)- Execute a Cypher queryvector_search(vector, k=10, ef_search=64)- k-NN vector searchfts_search(query, limit=10)- Full-text searchfts_search_fuzzy(query, limit=10, max_distance=0, min_term_length=0)- Fuzzy full-text searchcache_clear()- Clear the query cachecache_stats()- Get cache hit/miss statistics
Transaction
Read Operations
get_node(node_id)- Get a node by ID, returnsNodeorNonenode_exists(node_id)- Check if a node existsget_property(node_id, key)- Get a property valueget_outgoing_edges(node_id)- Get outgoing edges from a nodeget_incoming_edges(node_id)- Get incoming edges to a nodeis_read_only/is_active- Transaction state
Write Operations
create_node(labels=[], properties=None)- Create a nodedelete_node(node_id)- Delete a nodeset_property(node_id, key, value)- Set a property on a nodeset_vector(node_id, key, vector)- Set a vector embeddingbatch_insert_vectors(label, vectors)- Insert vector-bearing nodes in one callbatch_insert(label, vectors)- Deprecated compatibility alias forbatch_insert_vectorsfts_index(node_id, text)- Index text for full-text searchcreate_edge(source_id, target_id, edge_type, properties=None)- Create an edgedelete_edge(source_id, target_id, edge_type)- Delete an edgeset_edge_property(edge_id, key, value)- Set an edge property by stable edge IDget_edge_property(edge_id, key)- Get an edge property by stable edge IDremove_edge_property(edge_id, key)- Remove an edge property by stable edge IDcommit()/rollback()- Commit or rollback the transaction
Bulk Vector Insertion
Insert many nodes with vectors in a single efficient call:
import numpy as np
with Database("vectors.db", create=True, enable_vectors=True, vector_dimensions=128) as db:
with db.write() as txn:
vectors = np.random.rand(1000, 128).astype(np.float32)
node_ids = txn.batch_insert_vectors("Document", vectors)
print(f"Created {len(node_ids)} nodes")
txn.commit()
Full-Text Search
Exact Search
results = db.fts_search("machine learning", limit=10)
for r in results:
print(f"Node {r.node_id}: score={r.score:.4f}")
Fuzzy Search (Typo-Tolerant)
# Finds "machine learning" even with typos
results = db.fts_search_fuzzy("machne lerning", limit=10)
# Control fuzzy matching sensitivity
results = db.fts_search_fuzzy(
"machne",
limit=10,
max_distance=2, # Max edit distance (default: 2)
min_term_length=4, # Min term length for fuzzy matching (default: 4)
)
Embeddings
LatticeDB includes a built-in hash embedding function and an HTTP client for external embedding services. For new code, prefer the dedicated latticedb.embedding module. The package root still exposes deprecated compatibility aliases.
Hash Embeddings (Built-in)
Deterministic, no external service needed. Useful for testing or simple keyword-based similarity:
from latticedb.embedding import hash_embed
vec = hash_embed("hello world", dimensions=128)
print(vec.shape) # (128,)
HTTP Embedding Client
Connect to Ollama, OpenAI, or compatible APIs:
from latticedb.embedding import EmbeddingClient, EmbeddingApiFormat
# Ollama (default)
with EmbeddingClient("http://localhost:11434") as client:
vec = client.embed("hello world")
# OpenAI-compatible API
with EmbeddingClient(
"https://api.openai.com/v1",
model="text-embedding-3-small",
api_format=EmbeddingApiFormat.OPENAI,
api_key="sk-...",
) as client:
vec = client.embed("hello world")
Edge Traversal
with db.read() as txn:
outgoing = txn.get_outgoing_edges(node_id)
for edge in outgoing:
print(f"{edge.source_id} --[{edge.edge_type}]--> {edge.target_id}")
incoming = txn.get_incoming_edges(node_id)
for edge in incoming:
print(f"{edge.source_id} --[{edge.edge_type}]--> {edge.target_id}")
Cypher Queries
# Pattern matching
result = db.query("MATCH (n:Person) RETURN n.name")
# With parameters
result = db.query(
"MATCH (n:Person) WHERE n.name = $name RETURN n",
parameters={"name": "Alice"},
)
# Vector similarity in Cypher
result = db.query(
"MATCH (n:Document) WHERE n.embedding <=> $vec < 0.5 RETURN n.title",
parameters={"vec": query_vector},
)
# Full-text search in Cypher
result = db.query(
'MATCH (n:Document) WHERE n.content @@ "machine learning" RETURN n.title'
)
# Data mutation
db.query("CREATE (n:Person {name: 'Charlie', age: 35})")
db.query("MATCH (n:Person {name: 'Charlie'}) SET n.age = 36")
db.query("MATCH (n:Person {name: 'Charlie'}) DETACH DELETE n")
Query Cache
# Get cache statistics
stats = db.cache_stats()
print(f"Entries: {stats['entries']}, Hits: {stats['hits']}, Misses: {stats['misses']}")
# Clear the cache
db.cache_clear()
Supported Property Types
None- Null valuebool- Booleanint- 64-bit integerfloat- 64-bit floatstr- UTF-8 stringbytes- Binary data- NumPy
ndarray(float32) - Vector embeddings
Nested list and dict values are not currently exposed by the public bindings/C API.
Error Handling
from latticedb import LatticeError, LatticeNotFoundError, LatticeIOError
try:
with Database("nonexistent.db") as db:
pass
except LatticeNotFoundError:
print("Database not found")
except LatticeIOError:
print("I/O error")
except LatticeError as e:
print(f"Error: {e}")
Requirements
- Python 3.9+
- NumPy (for vector operations)
- The native LatticeDB library (
liblattice.dylib/liblattice.so)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file latticedb-0.5.0.tar.gz.
File metadata
- Download URL: latticedb-0.5.0.tar.gz
- Upload date:
- Size: 34.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b7730f22b79903374efdf271cf6d63a8b4f54dcbbc1cb7d8cbb6645d4fa8504
|
|
| MD5 |
b2f1fc42f0dbc4376dd6a2e5bc4fc8cb
|
|
| BLAKE2b-256 |
1ce2da335cd8e6ac481e520f9daf766e9f11507de4a33bb3bcb07c0638b23d74
|
Provenance
The following attestation bundles were made for latticedb-0.5.0.tar.gz:
Publisher:
release.yml on jeffhajewski/latticedb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latticedb-0.5.0.tar.gz -
Subject digest:
1b7730f22b79903374efdf271cf6d63a8b4f54dcbbc1cb7d8cbb6645d4fa8504 - Sigstore transparency entry: 1207940332
- Sigstore integration time:
-
Permalink:
jeffhajewski/latticedb@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/jeffhajewski
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Trigger Event:
push
-
Statement type:
File details
Details for the file latticedb-0.5.0-py3-none-manylinux_2_17_x86_64.whl.
File metadata
- Download URL: latticedb-0.5.0-py3-none-manylinux_2_17_x86_64.whl
- Upload date:
- Size: 2.9 MB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2a18ce52a127f345a4702b4ea0e15cf1c3193e5c3ae4a2f66d930dcf4adff08
|
|
| MD5 |
02eb1f8869a66181eec15f87fc71ef3a
|
|
| BLAKE2b-256 |
c3fb7ec79d01f639ae8ef9c18cd06c8831c2c3361df522c7f6b176ddc5529864
|
Provenance
The following attestation bundles were made for latticedb-0.5.0-py3-none-manylinux_2_17_x86_64.whl:
Publisher:
release.yml on jeffhajewski/latticedb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latticedb-0.5.0-py3-none-manylinux_2_17_x86_64.whl -
Subject digest:
e2a18ce52a127f345a4702b4ea0e15cf1c3193e5c3ae4a2f66d930dcf4adff08 - Sigstore transparency entry: 1207940483
- Sigstore integration time:
-
Permalink:
jeffhajewski/latticedb@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/jeffhajewski
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Trigger Event:
push
-
Statement type:
File details
Details for the file latticedb-0.5.0-py3-none-manylinux_2_17_aarch64.whl.
File metadata
- Download URL: latticedb-0.5.0-py3-none-manylinux_2_17_aarch64.whl
- Upload date:
- Size: 2.9 MB
- Tags: Python 3, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7fbc64bd2a16c9fdcbbf196dc55cdb4ed422cd6db865d808a9666353452c90c
|
|
| MD5 |
ca8dc031b1393cddc9915ae97229a353
|
|
| BLAKE2b-256 |
8509cafdea25a08af9de9a5df0985e9b69f371388bf467ed58a58fe24b9bd5aa
|
Provenance
The following attestation bundles were made for latticedb-0.5.0-py3-none-manylinux_2_17_aarch64.whl:
Publisher:
release.yml on jeffhajewski/latticedb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latticedb-0.5.0-py3-none-manylinux_2_17_aarch64.whl -
Subject digest:
b7fbc64bd2a16c9fdcbbf196dc55cdb4ed422cd6db865d808a9666353452c90c - Sigstore transparency entry: 1207940414
- Sigstore integration time:
-
Permalink:
jeffhajewski/latticedb@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/jeffhajewski
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Trigger Event:
push
-
Statement type:
File details
Details for the file latticedb-0.5.0-py3-none-macosx_11_0_arm64.whl.
File metadata
- Download URL: latticedb-0.5.0-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 711.2 kB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3214080056469fe54bf434129acb1b94ee7ead5d857944695bd772ea56d960f5
|
|
| MD5 |
9904eabecd7a8fd3689c881c59d686d3
|
|
| BLAKE2b-256 |
deb53fd9a39eb96b52fdf569ef31fa1b8e089e42177da91e7c1896214b2b7a3a
|
Provenance
The following attestation bundles were made for latticedb-0.5.0-py3-none-macosx_11_0_arm64.whl:
Publisher:
release.yml on jeffhajewski/latticedb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latticedb-0.5.0-py3-none-macosx_11_0_arm64.whl -
Subject digest:
3214080056469fe54bf434129acb1b94ee7ead5d857944695bd772ea56d960f5 - Sigstore transparency entry: 1207940589
- Sigstore integration time:
-
Permalink:
jeffhajewski/latticedb@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/jeffhajewski
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Trigger Event:
push
-
Statement type:
File details
Details for the file latticedb-0.5.0-py3-none-macosx_10_9_x86_64.whl.
File metadata
- Download URL: latticedb-0.5.0-py3-none-macosx_10_9_x86_64.whl
- Upload date:
- Size: 764.3 kB
- Tags: Python 3, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f1e7c3d416f23856b28289d0e69447c115d5d3ee5d3f7a26d4d147db948c390
|
|
| MD5 |
d546297640acceaa23de939acb0518c8
|
|
| BLAKE2b-256 |
016cad5e32842cee91de98b73a8be0d6f311c3b54bfdee3e4add3026682065d7
|
Provenance
The following attestation bundles were made for latticedb-0.5.0-py3-none-macosx_10_9_x86_64.whl:
Publisher:
release.yml on jeffhajewski/latticedb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latticedb-0.5.0-py3-none-macosx_10_9_x86_64.whl -
Subject digest:
2f1e7c3d416f23856b28289d0e69447c115d5d3ee5d3f7a26d4d147db948c390 - Sigstore transparency entry: 1207940542
- Sigstore integration time:
-
Permalink:
jeffhajewski/latticedb@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/jeffhajewski
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@981aaa6ddd0cab1bcafacbbeb386fc5a7eb55682 -
Trigger Event:
push
-
Statement type: