Skip to main content

LlamaIndex vector store, reader, and retriever for CockroachDB native VECTOR + C-SPANN index

Project description

llama-index-cockroachdb

LlamaIndex integration for CockroachDB's native VECTOR column type and the C-SPANN approximate nearest neighbor index. Ships three classes:

  • CockroachDBVectorStore (llama_index.vector_stores.cockroachdb): drop-in BasePydanticVectorStore backed by CRDB.
  • CockroachDBReader (llama_index.readers.cockroachdb): load existing CRDB rows as LlamaIndex Document objects.
  • CockroachDBRetriever (llama_index.retrievers.cockroachdb): standalone retriever with C-SPANN beam-size tuning.

Requires CockroachDB v25.2+ and Python 3.10+.

Why not just use the pgvector store?

llama-index-vector-stores-postgres depends on the pgvector extension. CockroachDB ships its own native VECTOR(n) type and the C-SPANN distributed ANN index, so the pgvector store cannot be used as-is against a CRDB cluster: the DDL (CREATE EXTENSION vector, USING hnsw) and the per-session tuning vars (hnsw.ef_search, ivfflat.probes) don't exist there. This package targets CRDB's actual API surface.

Concern pgvector store this package
Column type pgvector.sqlalchemy.Vector native VECTOR(dim)
Index DDL CREATE INDEX ... USING hnsw (...) WITH (m, ef_construction) CREATE VECTOR INDEX ... WITH (min_partition_size, max_partition_size)
Search-time knob SET hnsw.ef_search SET vector_search_beam_size
Setup prereq CREATE EXTENSION vector SET CLUSTER SETTING feature.vector_index.enabled = true
Dialect postgresql+psycopg2 / +asyncpg cockroachdb+psycopg2 / cockroachdb+asyncpg (retry-aware)
HALFVEC yes not yet
Hybrid / sparse (tsvector) yes not yet (CRDB has no tsvector equivalent)

Install

pip install llama-index-cockroachdb

You also need a CRDB v25.2+ cluster with the vector feature enabled once at the cluster level:

SET CLUSTER SETTING feature.vector_index.enabled = true;

The store will attempt to set this on first initialization if enable_feature_setting=True (the default) and the connected user has permission.

Quick start

from llama_index.vector_stores.cockroachdb import CockroachDBVectorStore
from llama_index.core import VectorStoreIndex, StorageContext, Document

store = CockroachDBVectorStore.from_params(
    host="localhost",
    port=26257,
    user="root",
    password="",
    database="defaultdb",
    table_name="my_index",
    embed_dim=1536,
    distance_metric="cosine",                # or "l2", "inner_product"
    cspann_kwargs={
        "min_partition_size": 16,
        "max_partition_size": 128,
    },
    sslmode="disable",                       # local dev only
)

index = VectorStoreIndex.from_documents(
    [Document(text="...")],
    storage_context=StorageContext.from_defaults(vector_store=store),
)
print(index.as_query_engine().query("..."))

See examples/ for seven runnable scripts covering async, MMR, metadata filters, the standalone retriever, and the reader.

Tuning C-SPANN

Two levers, both optional:

  1. Build-time partitioning: pass cspann_kwargs={"min_partition_size": ..., "max_partition_size": ...} to from_params(). These map directly to the WITH clause on CREATE VECTOR INDEX.
  2. Query-time beam size: pass vector_search_beam_size=N to the store constructor (applies to every query) or as a kwarg on individual query() calls. Higher = better recall, slightly more latency. Issued as SET LOCAL vector_search_beam_size per session.
store.query(query, vector_search_beam_size=128)

Reader

from llama_index.readers.cockroachdb import CockroachDBReader

reader = CockroachDBReader.from_params(
    host="localhost", port=26257, database="defaultdb", user="root", sslmode="disable",
)
docs = reader.load_data(
    table="articles",
    text_column="body",
    metadata_columns=["id", "author", "tag"],
    id_column="id",
)
# or pass a query=... with :named params

Retriever

from llama_index.retrievers.cockroachdb import CockroachDBRetriever
from llama_index.embeddings.openai import OpenAIEmbedding

retriever = CockroachDBRetriever(
    vector_store=store,
    embed_model=OpenAIEmbedding(model="text-embedding-3-small"),
    similarity_top_k=5,
    vector_search_beam_size=128,
)
nodes_with_scores = retriever.retrieve("What is C-SPANN?")

Supported query modes

Mode Supported Notes
DEFAULT yes Vector ANN through C-SPANN
MMR yes Client-side rerank, configurable mmr_threshold, mmr_prefetch_factor, mmr_prefetch_k
HYBRID / SPARSE / TEXT_SEARCH no CRDB has no tsvector yet; raises NotImplementedError

Supported metadata filter operators

EQ, NE, GT, GTE, LT, LTE, IN, NIN, CONTAINS, TEXT_MATCH, TEXT_MATCH_INSENSITIVE, IS_EMPTY. Filters are AND/OR/NOT nestable via MetadataFilters.

For frequently filtered keys, declare indexed_metadata_keys={("category", "text"), ("year", "int")} on the store to get JSONB-extracted BTREE indices.

Development

uv sync --all-extras
uv run pre-commit install
uv run pytest         # spins up a CRDB testcontainer per session
uv run ruff check .

Tests can target an existing cluster instead of a container by exporting CRDB_TEST_URL=postgresql://user:pass@host:port/db?sslmode=disable.

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_cockroachdb-0.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llama_index_cockroachdb-0.1.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_cockroachdb-0.1.0.tar.gz.

File metadata

  • Download URL: llama_index_cockroachdb-0.1.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for llama_index_cockroachdb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a18f6964a73a19634068d79fd2ca9c5b62558222ec7435d96586ff4dded0043f
MD5 5d59083b96b80f32b85989cc7780b7b4
BLAKE2b-256 ca6275698bb88638772163ecfd007181d34b8717d1d9d5f1dc32f8e172355588

See more details on using hashes here.

File details

Details for the file llama_index_cockroachdb-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_cockroachdb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23d588120cde0e5c1ec3ffc124c2f06a333fec8998676aba7c1072803f939c8a
MD5 d24bcfcde133de0d27856c6c5f0af4f1
BLAKE2b-256 d74fac191f135d42640188b17c5fc6884d238ee9ebe58e0ff615f63e42e31a4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page