ChloeDB in LangChain, LangGraph, LlamaIndex and DB-API tools
Reference, rendered from docs/chloedb/frameworks.md.
These docs describe release 0.3.11. The docs of main describe the code as it is now.
On this page
The Python package plugs ChloeDB into the frameworks agents and retrieval pipelines are built with, and into anything that takes a Python DB-API connection:
| module | what it is | install |
|---|---|---|
chloedb.langchain |
ChloeDBVectorStore, a LangChain vector store (and, through as_retriever(), a retriever) |
pip install "chloedb[langchain]" |
chloedb.langgraph |
ChloeDBSaver, a LangGraph checkpointer, and ChloeDBStore, a LangGraph store for long-term memory |
pip install "chloedb[langgraph]" |
chloedb.llama_index |
ChloeDBVectorStore, a LlamaIndex vector store, and ChloeDBEmbedding |
pip install "chloedb[llama-index]" |
chloedb.dbapi |
a PEP 249 driver: connect(), cursors, pandas.read_sql |
nothing more |
Each of them takes where the data is in the same way: a database's path (opened in the process, and made at the first write when it does not exist), the http:// or https:// URL of a chloedb-server or a coordinator (with token=, or $CHLOEDB_TOKEN), or a chloedb.Database or chloedb.Client already open. Adapters given the same path in one process share one open database (a process opens a database once to write it), so a checkpointer and a store can both name "agent.chloedb"; a database opened by chloedb.open is passed as itself. A DB-API connection to that path shares it too, and closing the connection closes the database only when no adapter or other connection holds it.
Through a server, each adapter reads with a Session of its own, so that its reads see its own writes whichever copy answers: a LangGraph thread resumed a moment after its last step reads the checkpoint that step wrote, also through a coordinator and from a replica. Start the servers with --seq-no for that (reads that see a write); without it, a write answers no number and the reads are a plain client's. A chloedb.Client given to an adapter is used through its session too. The same code runs on a file on a laptop and on a cluster, and one database can hold an agent's vectors, checkpoints, memories and its other tables side by side, each in a table of its own.
LangChain#
ChloeDBVectorStore keeps each document as a row: its id, its text (a TEXT field, indexed for BM25), its metadata (a JSON field, so a filter on any key runs from the field's index) and its vector. A search ranks by vector, by words, or by both fused by reciprocal rank, so one store answers a question asked in other words and a query of exact names, codes and error messages, which vectors miss.
from chloedb.langchain import ChloeDBVectorStore
from langchain_openai import OpenAIEmbeddings
store = ChloeDBVectorStore("docs.chloedb", embedding=OpenAIEmbeddings())
store.add_texts(
["ChloeDB keeps text and vectors in one file", "LangGraph runs agents as graphs"],
metadatas=[{"topic": "db", "year": 2026}, {"topic": "agents", "year": 2025}],
ids=["a", "b"],
)
store.similarity_search("where do vectors live?", k=1)
store.similarity_search("ChloeDB", k=4, mode="hybrid", filter={"year": {"$gte": 2026}})
retriever = store.as_retriever(search_kwargs={"k": 4, "mode": "hybrid"})
Without an embedding model, ChloeDB embeds the texts itself, when they are written, and each query's text, with a model of a vector field: potion-retrieval-32M by default, any model a field takes otherwise (a hosted one too, such as openai/text-embedding-3-small). Nothing then embeds in Python:
store = ChloeDBVectorStore("docs.chloedb", model="potion-retrieval-32M")
| argument | default | what it does |
|---|---|---|
db |
"langchain.chloedb" |
where: a path, a server's URL, a Database or a Client |
table |
"langchain" |
the table, made when missing |
embedding |
None | a LangChain Embeddings; None for ChloeDB to embed with model |
model |
"potion-retrieval-32M" when embedding is None |
the model ChloeDB embeds with |
dim |
the first vector's length | the vectors' dimensions, to make the table before the first write |
distance |
"cosine" |
"cosine" makes vectors unit length, so the score is the cosine similarity; "dot" keeps them as the model writes them |
mode |
"vector" |
how a search ranks unless it says: "vector", "text" (BM25) or "hybrid" |
fusion |
None (reciprocal rank, k 60) | how a hybrid search fuses its two rankings unless it says: {"rrf": {"k": 10}} (and "depth", or "weights": {"text": 0.5}), a weighted sum of scores put on one scale, {"tmm": {"text": 0.1}} (words 0.1, meaning 0.9; "vector" names the other weight), {"minmax": ...}, {"zscore": ...}, or the name alone ("tmm": half and half) |
A search takes fusion= too, so a retriever can pass it: store.as_retriever(search_kwargs={"k": 4, "mode": "hybrid", "fusion": {"tmm": {"text": 0.1}}}). Reciprocal rank fusion needs no tuning; a weighted sum of tmm scores, its weight tuned on queries you graded (the command center's relevance lab tunes it, on half of them, and measures it on the other half), often ranks better. The fused ranking is the SQL of the same name: {"tmm": {"text": 0.1}} is ORDER BY 0.9 * tmm(similarity(...)) + 0.1 * tmm(bm25(...)).
The store has LangChain's methods: add_texts, add_documents (a document's id is kept), get_by_ids, delete(ids) or delete(filter=...), similarity_search, similarity_search_with_score (higher is closer: the cosine similarity, the BM25 score or the fused score), similarity_search_with_relevance_scores (by vector the cosine similarity, negative ones as 0, which is what Chroma, FAISS and PGVector report for cosine; hybrid, the fused score over the best it can be, a document first in both rankings (with tmm or minmax, the sum of the weights), so 1, and a zscore fusion, which has no best, raises NotImplementedError; by words it raises NotImplementedError, since BM25 has no scale from 0 to 1; a search's own mode= decides, else the store's), similarity_search_by_vector, max_marginal_relevance_search, from_texts, as_retriever and the async forms of each. A text written again under its id replaces the one before.
Filters#
Filters are written as MongoDB's, as LangChain's other stores take them, and each runs as a condition on the metadata's JSON field, from its index:
| filter | keeps the documents whose metadata |
|---|---|
{"topic": "db"} |
has topic equal to "db" (a list holds the value when one of its values is it) |
{"author": {"name": "Ann"}} |
has name equal to "Ann" in the object at author |
{"year": {"$gte": 2020, "$lt": 2026}} |
compares: $eq, $ne, $gt, $gte, $lt, $lte, $between ([lo, hi]) |
{"topic": {"$in": ["db", "ml"]}} |
is one of the values; $nin none of them |
{"score": {"$exists": True}} |
has a value there (False: has none) |
{"name": {"$like": "chl%"}} |
starts with the text before % |
{"tags": {"$contains": "x"}} |
holds the value in a list |
{"$or": [{...}, {...}]}, {"$and": [...]}, {"$not": {...}} |
combines filters |
$ne and $nin keep documents without the key, as MongoDB's do. A key is one level whatever it holds ({"a.b": 1} is the key a.b, not b inside a). Metadata is kept as JSON: a value that JSON cannot hold is kept as its text, and an object deeper than 16 levels, with more than 1,000 values or with a path longer than 256 bytes is refused, as a JSON field refuses it.
LangGraph#
The checkpointer#
ChloeDBSaver keeps a graph's checkpoints, and the writes pending on each, as rows of two tables (langgraph_checkpoints and langgraph_writes, made when missing, placed and clustered by thread_id, so that on a cluster a thread's rows sit on one shard, a thread is read from it alone, and a task's writes go in one transaction; tables made by 0.3.0 or earlier keep the placement by key they were made with), so that a thread goes on after a restart, from another process, or, through a server, from another machine. It has the methods LangGraph calls, synchronous and async alike (the async ones run the same calls on a worker thread): get_tuple, list (with filter on the metadata, before and limit), put, put_writes, delete_thread and copy_thread.
from chloedb.langgraph import ChloeDBSaver
graph = builder.compile(checkpointer=ChloeDBSaver("agent.chloedb"))
config = {"configurable": {"thread_id": "t1"}}
graph.invoke({"messages": [("user", "hi")]}, config)
graph.get_state(config)
list(graph.get_state_history(config)) # time travel
Interrupts, resumes (Command(resume=...)), forks from an old checkpoint and subgraphs work as with LangGraph's own savers. Checkpoints and writes are written by the graph's serializer (LangGraph's JsonPlusSerializer unless serde= says otherwise), as base64 text in fields that are stored and not indexed; the metadata is also kept in a JSON field, so that list(filter={"source": "input"}) runs from its index. Metadata past what a JSON field takes (16 levels, 1,000 values, paths of 256 bytes) is indexed in part: its top-level values first, then each object or list that fits whole; a filter on a part left out does not find the checkpoint.
The store#
ChloeDBStore is LangGraph's BaseStore, the long-term memory a graph's nodes read and write through store=: items by namespace and key, in one table (langgraph_store).
from chloedb.langgraph import ChloeDBStore
store = ChloeDBStore("agent.chloedb", index={"model": "potion-retrieval-32M", "fields": ["text"]})
store.put(("users", "u1", "memories"), "m1", {"text": "prefers dark roast coffee", "kind": "preference"})
store.search(("users", "u1"), query="what does the user drink?", limit=3)
store.search(("users", "u1"), filter={"kind": "preference"})
store.list_namespaces(prefix=("users",))
graph = builder.compile(checkpointer=ChloeDBSaver("agent.chloedb"), store=store)
- Filters on items' values run from a JSON field's index, with the operators above (LangGraph's own are
$eq,$ne,$gt,$gte,$ltand$lte). A value past what the field takes is kept whole and indexed in part, as a checkpoint's metadata is: an item holding a long transcript is still found by itskind. - Search by meaning:
index={"dims": 1536, "embed": OpenAIEmbeddings(), "fields": ["text"]}(or"embed": "openai:text-embedding-3-small"), as LangGraph's stores take it, embeds in Python;index={"model": "potion-retrieval-32M", "fields": [...]}has ChloeDB embed each item's text as it is written, and each query's.fieldsare the paths of the text in a value (["$"], the whole value, by default), andput(..., index=False)orindex=[paths]overrides them for one item. An item's texts are embedded together, as one text. - Time to live:
ttl={"default_ttl": 60, "refresh_on_read": True}(minutes), orput(..., ttl=...). The table expires each row at its time (expire_by), so an expired item is gone from every read at once, and a read moves an item's time on whenrefresh_on_readis on. - Without a query, a search returns the newest items first.
- Namespaces:
list_namespacesgroups the items by namespace in the server; past the most groups a query may make (--max-groups, 65,536), it reads the namespaces a page at a time in their order instead.
LlamaIndex#
ChloeDBVectorStore keeps nodes as the LangChain store keeps documents, with each node's document (ref_doc_id) and the rest of the node besides, so that delete(ref_doc_id) deletes a document's nodes and a query returns the nodes as they were written.
from llama_index.core import StorageContext, VectorStoreIndex
from chloedb.llama_index import ChloeDBVectorStore
store = ChloeDBVectorStore("docs.chloedb")
index = VectorStoreIndex.from_documents(documents, storage_context=StorageContext.from_defaults(vector_store=store))
index.as_retriever(similarity_top_k=4).retrieve("where do vectors live?")
index.as_query_engine(vector_store_query_mode="hybrid", similarity_top_k=4)
Queries rank in the modes default (by vector), text_search and sparse (by BM25) and hybrid (both, fused by reciprocal rank, or as the store's fusion= says, as the LangChain store's is written; a query's alpha, LlamaIndex's weight of the vectors from 0 to 1, makes it {"tmm": {"vector": alpha, "text": 1 - alpha}}, and vector_store_kwargs={"fusion": ...} on a retriever sets it for its queries), among the nodes that MetadataFilters keep (==, !=, >, >=, <, <=, in, nin, any, all, contains, is_empty, with and, or and not), doc_ids and node_ids. The store also has get_nodes, delete_nodes and clear. An empty list of ids names no node in get_nodes and delete_nodes, with filters or without, as in LlamaIndex's own stores, and delete_nodes with filters that hold no condition deletes nothing (clear() deletes every node); in a query an empty list limits nothing, as LlamaIndex's retrievers pass it.
With model="potion-retrieval-32M" the table embeds the nodes that come without a vector, and every query's text; store.embed_model is that model as LlamaIndex's, so the index needs no other:
store = ChloeDBVectorStore("docs.chloedb", model="potion-retrieval-32M")
index = VectorStoreIndex.from_documents(documents, storage_context=StorageContext.from_defaults(vector_store=store), embed_model=store.embed_model)
DB-API#
chloedb.dbapi is a PEP 249 driver, for code and tools written for any Python database: scripts written for sqlite3, pandas.read_sql, notebooks and report tools.
import chloedb.dbapi
conn = chloedb.dbapi.connect("books.chloedb") # or "http://127.0.0.1:7700", or a Database / Client
cur = conn.cursor()
cur.execute("SELECT id, title FROM books WHERE year >= :y ORDER BY bm25(title, :q) DESC LIMIT 5", {"y": 2000, "q": "dragon"})
cur.description # (name, type_code, ...) per column
cur.fetchall()
cur.execute("UPDATE books SET rating = :r WHERE id = :id", {"r": 4.5, "id": "b1"})
cur.rowcount # rows written
import pandas
pandas.read_sql("SELECT genre, COUNT(*) AS n FROM books GROUP BY genre", conn)
paramstyleis"named"::namein the SQL, bound from a mapping, with ChloeDB's own parameters too (a vector,{"embed": text}). Adatetimeordatebinds as ISO 8601 text (a naive one as UTC), and a NaN as NULL, in a file and through a server alike. ChloeDB has no binary type:Binary()and abytesparameter raiseNotSupportedError(keep bytes as base64 text).- A read (
SELECT,WITH,EXPLAIN) keeps its rows forfetchone,fetchmanyandfetchall. Through a server, aSELECTorWITHis read a page of 10,000 rows at a time as the rows are fetched (a scroll), so it reads past the server's result window as a file does;rowcountis how many rows it answered once that is known (they came in one page, or were all fetched), -1 until then. A write setsrowcountto the rows it wrote. - A ranked query without
LIMITanswers its best 100 rows: the cursor then warns (aUserWarning) and holds aWarningincursor.messages. - A
TIMESTAMPcolumn comes back as adatetimein UTC: a field, and an alias,MAX,MIN,date_trunc,date_bin,CAST(.. AS DATE)or+ INTERVALof one alike, since the answer gives each column's type (types of an answer's columns). - ChloeDB commits each statement as it runs:
commit()has nothing to do, androllback()raisesNotSupportedError.with conn:commits at its end and leaves the connection open, assqlite3and psycopg do;contextlib.closing(conn)closes it. description's type codes are the columns' types as the answer gives them ("text","int","float","bool","timestamp","vector","list"), whatever a column is named, which compare equal to the module'sSTRING,NUMBERandDATETIME; a text column named as a table's field keeps the field's own type ("keyword","json"), and a column of only NULLs takes the type of the field of its name. A connection reads the tables' types once, and again after aCREATE,ALTERorDROPit ran.- Errors are the module's PEP 249 classes (
ProgrammingErrorfor a bad statement,OperationalErrorfor a busy, overloaded or unreachable database,IntegrityErrorfor a conflict), each with the ChloeDB error'scodeand the original error as its__cause__. connect(path, read_only=True)opens a database another process writes, to read it.