API reference¶
Everything below is importable from lance_csr. The classes are implemented in Rust (PyO3); this page is
written by hand from their signatures and docstrings, and every behaviour it states is checked against the
code. The guides link here for exact signatures and link back for examples.
| Class | Purpose |
|---|---|
GraphConfig, GraphConfigBuilder |
Map tables to node labels and relationship types |
CypherEngine |
Run many Cypher queries over registered datasets |
CypherQuery |
Run one Cypher query |
SqlEngine, SqlQuery |
Run SQL through DataFusion |
VectorSearch, DistanceMetric |
Vector similarity search and reranking |
DirNamespace |
Resolve table names to Lance datasets in a directory |
ExecutionStrategy, SqlDialect |
Enums |
lance_csr.__version__ is the package version.
Datasets¶
Methods that take datasets take a dict[str, object] mapping each node label or relationship type (or SQL
table name) to one of:
lance.LanceDataset: read lazily and version-pinned byCypherEngine; read into memory by the other classes;pyarrow.Table,pyarrow.RecordBatch,pyarrow.RecordBatchReader, or any object exporting the Arrow C stream interface (__arrow_c_stream__).
Keys match labels case-insensitively.
Exceptions¶
| Exception | When |
|---|---|
ValueError |
Invalid query, unknown label, missing dataset or column, unsupported parameter type, empty datasets |
NotImplementedError |
A Cypher feature that parses but isn't supported |
RuntimeError |
Execution or I/O failures |
TypeError |
A dataset value of an unsupported type, such as a LanceDB Table |
GraphConfig¶
An immutable mapping from tables to a property graph. Create one with GraphConfig.builder().
GraphConfig.builder() -> GraphConfigBuilder(static)- A new, empty builder.
node_labels() -> list[str]- The configured node labels, lower-cased, in no particular order.
relationship_types() -> list[str]- The configured relationship types, lower-cased, in no particular order.
GraphConfigBuilder¶
GraphConfigBuilder()- Same as
GraphConfig.builder(). Every method returns a new builder. node_table(label: str, primary_key: str) -> GraphConfigBuilder- Nodes labelled
label, one per row of the dataset namedlabel, identified by theprimary_keycolumn. rel_table(rel_type: str, from_table: str, to_table: str, src: str, dst: str) -> GraphConfigBuilder- Edges of type
rel_type, one per row of the dataset namedrel_type, fromfrom_tablenodes toto_tablenodes.srcanddsthold the endpoints' primary keys. build() -> GraphConfig- Validates and returns the config. Raises
ValueErrorif a relationship's endpoint label has no node table. with_node_label(label: str, id_field: str)(deprecated)- Use
node_table. EmitsDeprecationWarning. with_relationship(rel_type: str, source_field: str, target_field: str, source_label: str | None = None, target_label: str | None = None)(deprecated)- Use
rel_table. EmitsDeprecationWarning.
See Graph schema.
CypherEngine¶
Registers datasets once and reuses the catalog, DataFusion session and (with use_csr=True) CSR index
across queries. Safe to share between threads.
| Parameter | Description |
|---|---|
config |
The graph mapping. |
datasets |
See Datasets. Lance datasets are pinned to the version passed in and not read here. |
use_csr |
Build the experimental CSR index for relationship types with integer endpoints, and node indexes for labels with unique integer keys. Default False. |
Raises ValueError if datasets is empty, a configured key or endpoint column is missing, or a Lance dataset
was opened through a namespace client and can't be reopened by URI. Raises RuntimeError if a dataset can't
be opened.
execute(query: str, params: dict | None = None) -> pyarrow.Table- Runs
queryand returns the whole result.paramsbinds$nameparameters:int,float,str,bool,None,datetime.datetimeanddatetime.date. execute_batches(query: str, params: dict | None = None) -> pyarrow.RecordBatchReader- Streams the result. Planning errors raise immediately; execution errors raise while reading. The reader
keeps its snapshot alive across
refresh(). explain(query: str, params: dict | None = None) -> str- The graph logical plan, DataFusion logical plan and physical plan.
refresh() -> None- Moves every Lance dataset to its latest version and updates the CSR, node indexes and statistics. All-or-nothing; running queries keep their snapshot. See Lance datasets.
dataset_versions() -> dict[str, int]- The pinned version of each Lance dataset. PyArrow inputs are not listed.
csr_stats() -> dict[str, dict]- Per relationship type with a CSR:
edges,delta_edges,tombstones,update,version,build_ms. Empty withoutuse_csr=True. See Inspecting the CSR. node_index_stats() -> dict[str, dict]- Per node label with a node index:
rows,version,build_ms. config() -> GraphConfig- The engine's config.
execute_with_vector_rerank(query: str, vector_search: VectorSearch) -> pyarrow.Table- Runs
query, then ranks its rows withvector_search. See Vector search.
CypherQuery¶
A parsed Cypher query. Raises ValueError if query_text doesn't parse. Builder methods return a new query.
with_config(config: GraphConfig) -> CypherQuery- Sets the graph config. Required before executing.
with_parameter(key: str, value: object) -> CypherQuery- Binds
$keytovalue. execute(datasets: dict, strategy: ExecutionStrategy | None = None) -> pyarrow.Table- Runs the query. Datasets are read into memory for this call.
execute_with_namespace(namespace: DirNamespace, strategy: ExecutionStrategy | None = None) -> pyarrow.Table- Runs the query, resolving each label to
<base_uri>/<label>.lance. explain(datasets: dict) -> str- The plans, as for
CypherEngine.explain. to_sql(datasets: dict, dialect: SqlDialect | None = None) -> str- The equivalent SQL, as joins. See From Cypher to SQL.
execute_with_vector_rerank(datasets: dict, vector_search: VectorSearch) -> pyarrow.Table- Runs the query, then reranks by vector similarity.
query_text() -> str,parameters() -> dict- The query text and the bound parameters.
variables() -> list[str],node_labels() -> list[str],relationship_types() -> list[str]- Names referenced by the query.
SqlEngine¶
Registers datasets (names lower-cased) in a DataFusion session for repeated SQL queries. Raises
ValueError if datasets is empty.
execute(sql: str) -> pyarrow.Table- Runs a SQL query.
SqlQuery¶
execute(datasets: dict) -> pyarrow.Table- Registers
datasetsand runs the query. explain(datasets: dict) -> str- DataFusion's logical and physical plans.
sql() -> str- The query text.
VectorSearch¶
An immutable builder for vector similarity search. column is the vector column; for reranking a Cypher
result, use alias.property ("d.embedding").
| Method | Default | Description |
|---|---|---|
query_vector(vector: list[float]) |
required | The query vector |
metric(metric: DistanceMetric) |
DistanceMetric.L2 |
Distance metric |
top_k(k: int) |
10 |
Number of rows returned |
include_distance(include: bool) |
True |
Add the distance column |
distance_column_name(name: str) |
"_distance" |
Name of the distance column |
use_lance_index(enabled: bool) |
False |
Query a Lance ANN index first, then run the Cypher query on the top-k rows; can change results when the query also filters |
search(table: pyarrow.Table) -> pyarrow.Table- Brute-force search, sorted by distance, at most
top_krows. RaisesValueErrorif no query vector is set or the column doesn't exist.
DistanceMetric¶
| Member | Distance |
|---|---|
DistanceMetric.L2 |
Euclidean |
DistanceMetric.Cosine |
1 - cosine similarity |
DistanceMetric.Dot |
Negative dot product |
DirNamespace¶
Resolves table names to Lance datasets at <base_uri>/<name>.lance, for
CypherQuery.execute_with_namespace.
base_uri -> str(property)- The directory.
ExecutionStrategy¶
| Member | Meaning |
|---|---|
ExecutionStrategy.DataFusion |
The default and only implemented strategy |
ExecutionStrategy.LanceNative |
Reserved; raises NotImplementedError |
SqlDialect¶
SqlDialect.Default (DataFusion), SqlDialect.Spark, SqlDialect.PostgreSql, SqlDialect.MySql,
SqlDialect.Sqlite. Used by CypherQuery.to_sql.