LangChain integration for CockroachDB with native vector support
Quick Start β’ Features β’ Documentation β’ Examples β’ Contributing
Build LLM applications with CockroachDB's distributed SQL database and native vector search capabilities. This integration provides:
- π― Native Vector Support - CockroachDB's
VECTORtype - π C-SPANN Indexes - Distributed vector indexes optimized for scale
- π Automatic Retries - Retries transient CockroachDB errors (40001, 40003, 08xxx, 57P01, 58C00, 58C01) on every operation
- β‘ Async & Sync APIs - Choose based on your use case
- ποΈ Distributed by Design - Built for CockroachDB's architecture
pip install langchain-cockroachdbimport asyncio
from langchain_cockroachdb import AsyncCockroachDBVectorStore, CockroachDBEngine
from langchain_openai import OpenAIEmbeddings
async def main():
# Initialize
engine = CockroachDBEngine.from_connection_string(
"cockroachdb://user:pass@host:26257/db"
)
await engine.ainit_vectorstore_table(
table_name="documents",
vector_dimension=1536,
)
vectorstore = AsyncCockroachDBVectorStore(
engine=engine,
embeddings=OpenAIEmbeddings(),
collection_name="documents",
)
# Add documents
await vectorstore.aadd_texts([
"CockroachDB is a distributed SQL database",
"LangChain makes building LLM apps easy",
])
# Search
results = await vectorstore.asimilarity_search(
"Tell me about databases",
k=2
)
for doc in results:
print(doc.page_content)
await engine.aclose()
asyncio.run(main())CockroachDBStore gives LangGraph agents memory that every thread can read, with semantic search and expiry:
from langchain_cockroachdb import CockroachDBStore
from langchain_openai import OpenAIEmbeddings
with CockroachDBStore.from_conn_string(
"cockroachdb://user:pass@host:26257/db",
pool_config={"max_size": 10},
index={"dims": 1536, "embed": OpenAIEmbeddings(model="text-embedding-3-small")},
ttl={"default_ttl": 60 * 24 * 30}, # minutes
) as store:
store.setup()
store.put(("user", "alice", "memories"), "seat", {"text": "Prefers window seats"})
results = store.search(("user", "alice"), query="Where does Alice like to sit?")
# Pass store=store to create_agent, StateGraph.compile or create_deep_agentSee the Long-Term Memory guide.
- Native
VECTORtype support with C-SPANN indexes - Advanced metadata filtering (
$and,$or,$gt,$in, etc.) - Hybrid search (full-text + vector similarity)
- Multi-tenancy with namespace-based isolation and C-SPANN prefix columns
- Persistent conversation storage in CockroachDB
- Session management by thread ID
- Drop-in replacement for other LangChain chat history implementations
- Short-term memory for multi-turn LangGraph agents
- Human-in-the-loop with interrupt/resume support
- Both
CockroachDBSaver(sync) andAsyncCockroachDBSaver - Compatible with LangGraph's
compile(checkpointer=...)interface
- Memory that every thread can read, for LangGraph agents and Deep Agents
- Both
CockroachDBStore(sync) andAsyncCockroachDBStore, with the API of LangGraph'sPostgresStore - Semantic search on a C-SPANN vector index, with metadata filters
- Expiry through CockroachDB row-level TTL, with no sweeper thread
delete_namespacedeletes a user's memories in one transaction- Keeping memories true:
reconcilesettles a new memory against the ones it may conflict with in one serializable transaction, orfind_conflictsandresolvelet an LLM or a person decide, with opt-in version history, provenance,confirmand conditional writes
- Retries transient CockroachDB errors (40001, 40003, 08xxx, 57P01, 58C00, 58C01, 53300) on every operation
- Configurable
RetryPolicywith exponential backoff, jitter and optional hang guards - Idempotent writes (UPSERTs, ON CONFLICT DO NOTHING) for safe retries
- Connection pooling with health checks and pre-ping
- Works with both SERIALIZABLE (default, recommended) and READ COMMITTED isolation
- See the Retries and Resilience guide
- Async-first design for high concurrency
- Sync wrapper for simple scripts
- Type-safe with full type hints
- Comprehensive test suite (177 tests)
LangChain Official Integration Docs:
Getting Started:
Guides:
- Vector Store
- Vector Indexes
- Hybrid Search
- Chat History
- Multi-Tenancy
- LangGraph Checkpointer
- LangGraph Long-Term Memory
- Async vs Sync
- Retries and Resilience
π§ Working Examples
quickstart.py- Get started in 5 minutessync_usage.py- Synchronous APIvector_indexes.py- Index optimizationhybrid_search.py- FTS + vector searchmetadata_filtering.py- Advanced querieschat_history.py- Persistent conversationscheckpointer.py- LangGraph checkpointerlong_term_memory.py- LangGraph long-term memory storememory_reconciliation.py- Keeping memories true: reconcile, history, confirm and conditional writesmulti_tenancy.py- Namespace-based multi-tenancyretry_configuration.py- Configuration patterns
# Clone repository
git clone https://github.com/cockroachdb/langchain-cockroachdb.git
cd langchain-cockroachdb
# Install dependencies
pip install -e ".[dev]"
# Start CockroachDB
docker-compose up -d
# Run tests
make test# Install docs dependencies
pip install -e ".[docs]"
# Serve documentation locally
mkdocs serve
# Open http://127.0.0.1:8000Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
- Distributed SQL - Scale horizontally across regions
- Native Vector Support - First-class
VECTORtype and C-SPANN indexes - Strong Consistency - SERIALIZABLE isolation by default, READ COMMITTED also supported
- Cloud Native - Deploy anywhere (IBM, AWS, GCP, Azure, on-prem)
- PostgreSQL Compatible - Familiar SQL with distributed superpowers
Apache License 2.0 - see LICENSE for details.
Built for the CockroachDB and LangChain communities.
- CockroachDB - Distributed SQL database
- LangChain - LLM application framework