ChloeDB from Python
Reference, rendered from docs/chloedb/python.md.
These docs follow main, which can be ahead of the latest release, 0.3.11 (its docs): a change made after it says so where it is described.
On this page
The chloedb package runs a ChloeDB database inside a Python process, the way sqlite3 runs SQLite, or talks to a running chloedb-server. Both have the same methods, so a script written against a local file works against a server.
LangChain, LangGraph and LlamaIndex take ChloeDB as a vector store, a checkpointer and a memory store, and chloedb.dbapi is a DB-API 2.0 driver: see frameworks.
For dataframe work, db["table"] is a lazy frame written as Polars writes one, run where the data is, with the answer as Arrow: see DataFrames.
A first search#
pip install chloedb
import chloedb
db = chloedb.open("books.chloedb", schema={
"table": "books", "key": "id",
"fields": [
{"name": "id", "type": "keyword"},
{"name": "title", "type": "text"},
{"name": "year", "type": "int"},
],
})
db.upsert([
{"id": "b1", "title": "The river and the dragon", "year": 1999},
{"id": "b2", "title": "Winter garden", "year": 2005},
{"id": "b3", "title": "Dragons of the northern rivers", "year": 2011},
])
# Words, ranked by BM25, with a filter
for row in db.query("SELECT id, title FROM books WHERE MATCH(title, :q) AND year > :y "
"ORDER BY bm25(title, :q) DESC", q="dragon", y=2000):
print(row["id"], row["title"])
# A typo, forgiven
for row in db.query("SELECT id, title FROM books ORDER BY bm25(title, :q, fuzzy => 'auto') DESC LIMIT 2", q="dragn"):
print(row["id"], row["title"])
db.close()
b3 Dragons of the northern rivers b1 The river and the dragon b3 Dragons of the northern rivers
chloedb.open(path, schema)creates the database the first time and opens it every time after, so the script runs again as it is.upsertreplaces the rows whose keys it is given, so nothing is written twice; it replaces whole rows, and a field the new row leaves out is then empty. To change one field, useUPDATE:db.sql("UPDATE books SET year = 2000 WHERE id = 'b1'").- Values go in as parameters (
:q,:y), never into the SQL text. - The same methods work against a server:
chloedb.serve()starts a chloedb-server of its own and returns a client of it (withpip install "chloedb[server]"), andchloedb.connect("http://127.0.0.1:7700")is a client of one already running. A server's tables are made with SQL:
with chloedb.serve() as db: # a server on a free port, stopped when the block ends
db.sql("CREATE TABLE books (id KEYWORD PRIMARY KEY, title TEXT, year INT)")
db.upsert([{"id": "b1", "title": "The river and the dragon", "year": 1999}], table="books")
print(db.query("SELECT id, title FROM books ORDER BY bm25(title, :q, fuzzy => 'auto') DESC", q="dragn").rows)
- Rows like a given row ("more like this") rank by
similarityto its stored vector,{"vector_of": key}: similar rows. - When something fails, common errors has the messages people meet first and what each means.
The search guide goes through every kind of search on a small catalogue, and Build a search page puts them in a Python web page.
Install#
pip install chloedb # prebuilt for CPython 3.10 and later, on macOS 11+ and Linux with glibc 2.28+ pip install "chloedb[server]" # the same, and chloedb-server of the same release
pip picks the wheel for your machine, so nothing is compiled. since 0.3.7 The server extra also installs chloedb-server, the prebuilt server of the same release, which chloedb.serve() starts.
If import chloedb raises an ImportError that calls it a placeholder, pip installed chloedb 0.0.x, which only held the name before 0.3.0 was published: pip install --upgrade chloedb replaces it. pip also falls back to it when no release has a wheel for your machine (an older glibc, say): chloedb-server doctor says which you have.
The old name, import strata, still works for a release, with a DeprecationWarning; release 0.1.0 was published as strata-db and imports as strata only.
Each platform's wheel, the client alone with no build, and a build from source (which the BERT embedding models need) are at the end of this page.
A database in your process#
import chloedb
db = chloedb.open("books.chloedb", schema={
"table": "books",
"key": "id",
"fields": [
{"name": "id", "type": "keyword"},
{"name": "title", "type": "text"},
{"name": "description", "type": "text"},
{"name": "genres", "type": "keyword", "multi": True},
{"name": "year", "type": "int"},
{"name": "embedding", "type": "vector",
"embed": {"from": ["title", "description"], "model": "potion-retrieval-32M"}},
],
})
db.insert([
{"id": "b1", "title": "The river and the dragon", "description": "A fantasy of rivers.", "genres": ["fantasy"], "year": 1999},
{"id": "b2", "title": "Winter garden", "description": "A quiet mystery.", "genres": ["mystery"], "year": 2005},
])
for row in db.search("dragon", limit=5):
print(row["id"], row["title"])
db.query("SELECT year, COUNT(*) FROM books WHERE genres = :g GROUP BY year", g="fantasy").rows
db.close()
chloedb.open(path, schema) opens the database at path, or creates it with schema if it does not exist. The schema is a dict, JSON text or the path of a JSON file, in the form the server takes. Database.create(path, schema) and Database.open(path) do one or the other. models= says where embedding and ranking models, and the captioners of text made from media, are (default $CHLOEDB_MODELS, else ~/.local/share/chloedb/models (or ~/.local/share/strata/models, on a machine that has that folder from before the rename; before 0.3.2, always); chloedb-server fetch-model NAME downloads one there), and directory=True creates the database as a directory of segment files.
A database written by a version of ChloeDB from before field ids does not open: chloedb.upgrade(path) copies it into this version's format in its place, keeps the old one as <path>.before-field-ids, and returns (old_path, tables, rows), or None when the database needs no upgrade. Nothing may have it open meanwhile.
One process at a time can have a database open to write it. Others can open it with chloedb.open(path, read_only=True) (or Database.open(path, read_only=True)) to read it while it is written, the other workers of a web server under gunicorn or uwsgi, say, or the second process of Flask's debug reloader: each query sees the writer's commits so far, every write raises ChloeDBError, and refresh() looks for new commits at once (how readers follow the writer). Within a process, threads can query at once: every call releases the GIL while it runs. Threads can write at once too: a write waits while another holds the writer, and the writes that wait share the next commit (one disk flush for all of them), so none fails as busy. For writes from several processes, serve the database with chloedb-server and use the client below. A second open to write a file that is open to write already, in another process or in this one, raises BusyError (common errors). Segments are merged in the background as writes add them, one merge at a time, as a server merges them, so a table written to for weeks stays in a few segments without the rows its updates replaced. A with block, or close(), writes the rows still in memory into the file (so that the file alone holds the database, backups), merges the small segments that writes left, gives the file system back the space merges freed when it is an eighth of the file or more, and closes the file. optimize() goes further, for a table that is done changing: one segment, up to 2,000,000 rows.
A commit waits for the disk (fsync), so a write that returned is still there after a power failure. On a disk that is slow to sync, that caps one-row commits from one thread at a few hundred to a few thousand a second (writes from several threads share a sync). sync=False (to chloedb.open, Database.create, Database.open and connect) commits without waiting for the disk: a commit still reaches the operating system before it returns, so a crash of the program loses nothing, but a power failure or a crash of the machine may lose the transactions committed since the last flush, newest first, and the database stays consistent, as SQLite's synchronous=NORMAL in WAL mode does. Bulk loads, flush(), close() and merges sync either way. The file does not keep the choice: each open makes it.
Backing up, and damaged files#
A database is its file plus <path>-wal, the write-ahead log, which holds the rows written since the last flush; copying the file alone while the database is open loses them. To back up a database:
- while it is open and in use:
db.checkpoint("backup.chloedb")writes a consistent copy (the destination must not exist), checks every segment against its checksum first, so that a damaged database fails the checkpoint instead of being copied, checks the copy, and only then gives it its name. Restore by opening the copy, or renaming it into place while nothing has the database open; - after
close()(or awithblock):close()writes the rows still in memory into the file and leaves the log empty, so the file alone is then a complete copy;db.flush()does the same at any moment, until the next write.
Copying a database's files while it is written is not a backup, even with its -wal.
When the newest version of a database or its log is damaged so that opening it would lose acknowledged writes (bit rot, a copy cut short, a log deleted while it held transactions), chloedb.open raises ChloeDBError naming the file, what is damaged and how to recover, and changes nothing: restore a backup, or open with recover=True to accept the loss. That keeps copies of the damaged files first (<path>.damaged-<time>, <path>-wal.damaged), opens what is left, issues a RuntimeWarning, and db.damaged says what was lost (it is None for a database that opened whole).
Writing#
Rows are dicts of field name to value: str, int, float, None, bool for a bool field, a datetime or date (or ISO 8601 text) for a timestamp field, a list for a multi field, a list of floats or a numpy array for a vector, a dict of term weights for a sparse field, a URI or a dict for a media field (in-process, the whole value or {"uri": …, "data": <base64>}: nothing is fetched, see media fields). A naive datetime is taken as UTC. A field the schema does not have is an error. Results give a bool field's values as bool and a timestamp field's as ISO 8601 text in UTC ("2026-10-01T12:30:00Z"), in-process and from the client alike; datetime.fromisoformat reads them, and result.types says which columns hold them (an alias, MAX or date_trunc of a timestamp too), which to_pandas() and the DB-API driver turn into datetimes.
| method | what it does |
|---|---|
insert(rows) |
adds a row (a dict) or rows (any iterable of dicts) whose keys must be new; all or none are written. Fewer than 10,000 rows go through the write-ahead log into memory, which gathers the rows of many inserts into one segment once it holds 10,000; from 10,000 rows on, they are bulk-loaded straight into segments. A vector may be a numpy array of float32 or float64, which is read from its bytes rather than a Python float at a time. |
upsert(rows) |
adds rows, replacing the rows with their keys, in transactions of 10,000 |
delete(keys) |
deletes the rows with a key or keys; returns how many there were |
get(key), get_many(keys) |
the row with a primary key (a dict of the columns SELECT * gives), or None, read without SQL (rows by key); get_many answers a list in the keys' order. Each takes table= |
expire() |
deletes the rows whose expire_by time has passed, which reads leave out already; returns how many |
sql("UPDATE ...") |
INSERT, UPDATE and DELETE statements; returns the rows written. It also runs CREATE TABLE, ALTER TABLE and DROP TABLE (tables) and CREATE and DROP RANK PROFILE (rank profiles), which return 0, and writes any table |
add_column(name, type, **options), drop_column(name), rename_column(old, new) |
change a table's columns while it holds rows, as ALTER TABLE does: options are the field's, in the short form of a schema (db.add_column("price", "float", default=0.0), db.add_column("emb", "vector", embed=["title"], model="potion-retrieval-32M"), the same as embed={"from": ["title"], "model": ...}); rows written before a column was added read its default, or None. An embedded vector is computed for the rows already there before add_column returns (as embed_backfill does, taking its progress=), and sql("ALTER TABLE ... ADD COLUMN ...") does the same; when the field's model is not open here (embed_errors), it warns that those rows have no vector yet. Each takes table=. In-process only: through the client, send ALTER TABLE with sql, and the server fills the vectors in itself |
embed_backfill(field), missing_vectors(field) |
embeds the rows that have no vector in an embedded field (added since they were written, or written while its model was not open: set_embed_later), batch=256 rows a write, and returns how many it wrote; a fill longer than two seconds prints how far it is on stderr, progress= a function of (done, total) instead (False: nothing), and Ctrl-C stops it with the rows filled so far kept. missing_vectors counts the rows without one (those whose sources are all empty get none). Each takes table= |
import_file(path, format=None) |
loads a JSON Lines, JSON, CSV or TSV file (how files are read); returns {rows, bulk, skipped_fields} |
write(ops) |
applies write operations in one transaction, as the server's /v1/write takes them: insert, upsert and delete, set of a field ({"op": "set", "key": k, "field": "status", "value": "SOLD"}: a number in place, another by writing the row again with the value), and add to a number in place ({"op": "add", "key": k, "field": "n", "value": 1}; with a doc instead of a key, an add inserts the row when no row has its key); each names its table or writes the first; returns the rows written |
import_sqlite(path, tables=None, schemas=None, append=False) |
imports a SQLite database's tables, each into the table of its name, created when missing (SQLite in and out); a table without a key that has rows takes more only with append=True; returns what it did and what did not carry over |
export_sqlite(path, tables=None, fts=True, indexes=True) |
writes the tables to a new SQLite file, with indexes, an FTS5 index over the text and the ChloeDB schemas |
import_parquet(path, table=None, schema=None, key=None) |
imports a Parquet file or a directory of them (Spark's part files, Hive partitions) into one table, created from the columns when missing (Parquet and S3); returns what it did |
export_parquet(path, tables=None) |
writes the tables to Parquet: a .parquet file for one table, else a directory of them, each keeping its ChloeDB schema |
import_arrow(data, table=None) |
writes the rows of a pyarrow Table or RecordBatchReader, a polars DataFrame, a DuckDB relation or anything with __arrow_c_stream__, columns into the fields of their names; bulk-loaded into an empty table, else upserted |
chloedb.from_sqlite("shop.sqlite", "shop.chloedb") creates a database from a SQLite file's tables, and chloedb.plan_sqlite("shop.sqlite") says what it would make of them without writing; edit the plan and pass it to either as schemas. chloedb.from_parquet("events.parquet", "lake.chloedb") and chloedb.plan_parquet(...) do the same for Parquet. Any of these paths, and import_file's, may be an s3:// URL, read with the AWS_* variables (S3).
insert, upsert, delete and import_file write the table table= names (db.insert(rows, table="events")), or the only table there is; sql names the table it writes, and tables() lists every table's schema. A call that names no table where there are several raises ChloeDBError and names them, rather than write the first (db.insert names no table, and this server has 3: products, library_help, recipes. Pass table=, as in db.insert(..., table='recipes')), and so do get, get_many, search, table(), sid_next and the column calls. Changed: they read and wrote the first table before, so on a server of several tables db.search("lasagna") answered rows of another table. A client asks the server for its tables once, and again after a table is made, dropped or changed through it. Through a coordinator, delete on a table placed by a column takes that column's value as routing= (tables on shards). A key a row leaves out gets its field's default, and a field with not_null refuses a row without a value (schema options); a timestamp defaulting to "now" takes one time for all the rows of a call.
Vectors of embedded fields that a row leaves out are computed by the field's model when it is open, here or behind an API: a field naming openai/text-embedding-3-small, cohere/embed-v4.0, voyage/voyage-3.5, gemini/gemini-embedding-001, ollama/nomic-embed-text or another hosted model gets its vectors from the provider, with the key in the process's environment (OPENAI_API_KEY, ...), so inserting plain text is enough. A write whose vectors the provider will not give (after its retries) raises ChloeDBError and writes nothing. db.embed_errors lists the embedded fields whose model did not open (a hosted model without its key says which variable to set), whose vectors rows must then bring: a write that leaves one to a model ChloeDB could run but has not open fails, and says so, unless db.set_embed_later() lets it store the row without its vector, to be filled in once the model opens (a server does it itself; here, db.embed_backfill("emb")) (before, such a row was stored without a vector, and no error said so; a server still does that, and fills them in itself). A table records the fingerprint of its model's weights (the SHA-256 of its weights file) when it is created with the model in its models directory, and a model of the same name with other weights does not open for it: it is listed in db.embed_errors, with what to do. It also lists the semantic-ID fields whose codebook did not open.
Querying#
query(sql, params) runs a SELECT in ChloeDB's SQL. Parameters bind :name placeholders, from a dict, keyword arguments, or both; values by position, sqlite3's ? and psycopg's %s with a tuple, raise a TypeError that shows the call with names (db.query('SELECT id FROM recipes WHERE MATCH(name, :p1)', p1='soup')):
db.query("SELECT id, title FROM books WHERE year >= :y ORDER BY bm25(title, :q) DESC LIMIT 10", {"y": 2000}, q="dragon")
db.query("SELECT id FROM books ORDER BY similarity(embedding, :v) DESC LIMIT 10", v={"embed": "a quiet mystery"})
db.query("SELECT id FROM books ORDER BY similarity(embedding, :v) DESC LIMIT 10", v={"vector_of": "b1"})
A parameter is a number, a string, None, a list of numbers or a numpy array (a vector), a list, a dict of term weights, {"embed": text} (the text's vector by an embedded field's model; add "field" to pick one) or {"vector_of": key} (a stored row's vector, for more like this). Either reads the table the statement's FROM names since 0.3.2, unless it names another with "table" (before 0.3.2, the first table).
The answer is a Result:
for row in result, result[i] |
rows as dicts of column to value |
result.rows, result.columns |
rows as lists, in column order |
result.keys, result.scores |
each row's primary key and the score it was ranked by |
result.column("title"), result.scalar() |
one column; the first value (a count, a sum) |
result.stats |
what the engine did |
result.types |
each column's type as its values have it: "int", "float", "bool", "timestamp", "text", "vector" or "list", None for a column of only NULLs, whatever the column's name (MAX(ts), ts AS d and date_trunc('month', ts) are "timestamp" as ts is) since 0.3.9 |
result.to_pandas() |
a DataFrame (with pandas installed), each timestamp column of types as datetime64[us, UTC] |
query_arrow(sql, params) runs a SELECT as query does and answers with its columns as Arrow arrays, an ArrowResult, without making a Python object of each value; a query of one table that neither groups nor counts reads its columns from the database's segments straight into the arrays. pyarrow, polars, pandas and DuckDB read the arrays where the database wrote them, without copying them, through the Arrow PyCapsule interface:
r = db.query_arrow("SELECT id, year, rating FROM books WHERE year >= :y", y=1950)
r.to_pyarrow() # a pyarrow Table
r.to_polars() # a polars DataFrame (without pyarrow)
r.to_pandas() # a pandas DataFrame of the Arrow columns (pandas.ArrowDtype); arrow_dtypes=False copies them into numpy
pyarrow.table(r) # any library that reads Arrow streams reads r
duckdb.sql("SELECT avg(rating) FROM r")
| ChloeDB | Arrow |
|---|---|
int |
int64 |
float |
float64 |
keyword, text, json |
string (a JSON value as its text) |
bool |
bool |
timestamp |
timestamp[us, tz=UTC] |
vector |
list<float32> |
a multi field |
list<int64> or list<string> |
A computed column has the type of its values: a column of only NULLs (or of no rows) is null, and one of integers and floats float64. Values of other types mixed in one column (a CASE that answers a number in some rows and text in others) are not one Arrow array: query_arrow raises ChloeDBError naming the column, and query answers it. r.columns, len(r) and r.stats are as a Result's. query_arrow comes with the package's default feature datafusion. A Client's query_arrow asks the server for an Arrow answer ("format": "arrow") and returns a pyarrow Table with the same column types; it needs pyarrow, and a server too old for Arrow answers JSON, which it types from the values.
search(text, table=None, limit=10, where=None, columns="*", mode="auto", fusion="zscore") writes the query for the common case, on table since 0.3.2 (which reads that table alone: a client with a key limited to other tables than the first can search its own), or on the only table, since with several it names them and asks for one. It ranks by BM25 over the text fields (the generated ones if the table has any, such as a combined body, else every indexed text field, their scores added up) fused with the similarity of the first embedded vector field whose model is open, each side on its z-score over the candidates and weighted half and half (0.5 * zscore(...) + 0.5 * zscore(...)); fusion="rrf" fuses them by reciprocal rank instead, each text field's BM25 and the similarity apart (rrf(bm25(title, ...), bm25(body, ...), similarity(...))), and mode="text" or "vector" uses one side only. since 0.3.12 (unreleased) The rows that have every word of the text as written no longer come first: on twenty questions about a set of documents cut in 2,843 chunks (potion-retrieval-32M), that bonus brought the MRR@10 down from 0.787 to 0.685, and fusion="rrf" gives 0.818 there, while on SciFact's titles and abstracts it moved the nDCG@10 by less than 0.001 (0.7057 with it, 0.7049 without), all measured in a cloud container of 4 cores. A text whose words are rare still finds the row that holds them all first, as the recipe named "best ratatouille" for those words. since 0.3.12 (unreleased) It fused by reciprocal rank before, field by field, which on SciFact's titles and abstracts gave an nDCG@10 of 0.654 by words and meaning and 0.589 by words, against 0.706 and 0.669 now, and put the one recipe named "best ratatouille" 64th for those words. since 0.3.10 A word that is in no row is read as a typo of the words near it (fuzzy => 'auto'), so db.search("dragn") finds the dragon; typos=False matches words as written. A word that one of the text fields holds is no typo in the others: when every word of the text is in some row as written, the search reads none as a typo, so a recipe named "killer shrimp" comes before twenty whose steps say "fillet" (one short query per word checks it). where adds a condition, built in Python or in SQL with params for its placeholders:
db.search("dragon", where=col("year") >= 1990, columns=["id", "title"])
db.search("dragon", where="year >= :y", params={"y": 1990})
since 0.3.10 ask(question, table=None, k=8) answers a question from the rows search finds for it, citing them: {"answer", "sources", "model"}, the answer marking each claim with its row as [1], [2], and sources those rows in that order ({"n", "key", "score", "row"}). A language model writes it through llm(), where the query runs: model=, else $CHLOEDB_ASK_MODEL, else anthropic/claude-haiku-5-5 with ANTHROPIC_API_KEY. Without a key it says so, and search finds the same rows. A Client answers so when given table=, and without one asks the server's agent, which writes queries of its own (the agent):
r = db.ask("which book has a dragon in it?")
print(r["answer"]) # The river and the dragon is a fantasy of rivers [1].
print(r["sources"][0]["key"]) # b1
embed(text, field=None) returns the query vector of a text (embed(image=data, field="emb") of an image file's bytes, by a model of images, with the package built with --features vision, and embed(audio=data) of a sound file's, by a model of sound, built with --features audio), embed_tokens(text, field) the vectors of a query's tokens by the ColBERT model of a multi-vector field, as maxsim takes them ({"embed": text, "field": "tok"} as a parameter gives the same), and len(db) the number of rows.
plan(sql, params) returns a statement's plan as a dict without running it, and run(plan) runs a plan: a read's Result, or the number of rows a write wrote.
p = db.plan("SELECT id FROM books WHERE year >= :y LIMIT 3", y=2000)
db.run(p)
Similar rows (more like this): vector_of#
{"vector_of": key} as a parameter is the vector a stored row already has, so ranking by similarity to it lists the rows nearest that row in meaning: "more like this" under a product, "readers also liked" under a book. Leave the row itself out by its key, and put any other condition in the same WHERE:
db.query("SELECT id, title FROM books WHERE id <> :id AND genres = :g ORDER BY similarity(embedding, :v) DESC LIMIT 5",
id="b1", g="fantasy", v={"vector_of": "b1"})
The vector is the row's first VECTOR field's, embedded or given in its rows, from the table the statement's FROM names; {"vector_of": key, "field": "emb", "table": "books"} names another field or table. A key no row has is an error: query error: no row "b99". The search guide runs it on a catalogue.
Limits: memory, deadlines, interrupting#
A query runs in your process, so what it builds up (its groups, the rows it orders and returns, a percentile's values, a join's tables) is held by your program. The queries of every database a process opens share a memory budget, by default a quarter of the machine's memory (the cgroup's limit in a container, else RAM), or CHLOEDB_QUERY_MEMORY_MB MiB when that is set when the first database opens (0: no budget). A query that would hold more stops and raises ChloeDBError with code memory, naming the budget and how to raise it, and gives back what it held; the program and its other queries go on. SELECT * FROM big on a table larger than the budget, a GROUP BY over a column with a value per row or a percentile over every row fail this way rather than taking the machine's memory. The counts are estimates of what a query grows, not of every allocation, and the index a query reads is mapped from the file and not counted.
A query has no deadline unless it is given one. timeout_ms (to query, query_arrow, scroll, sql, search and run, as a Client's query takes it) stops it once it has run that long: it raises DeadlineError, naming the timeout_ms. memory= gives a query a budget of its own, in bytes (memory=0: none). A database's own limits, for every query that does not give its own, are set when it opens or later:
db = chloedb.open("books.chloedb", timeout_ms=5_000, memory=512 << 20) # 5 s, and 512 MiB its queries share
db.query("SELECT genres, COUNT(*) FROM books GROUP BY genres", timeout_ms=200)
db.query("SELECT * FROM books", memory=0) # this one counts nothing
db.set_limits(timeout_ms=None, memory=None) # back to the defaults
db.timeout_ms, db.memory # the limits now (None: none)
db.interrupt(), from any thread, stops the queries of the database running now, as sqlite3.Connection.interrupt does: each raises DeadlineError within a few thousand rows, and queries that start after it run. Calls release the GIL while they run, so another thread (a watchdog, a web server's request that was cancelled) can call it:
threading.Timer(2.0, db.interrupt).start()
try:
db.query("SELECT a.id, b.id FROM books a JOIN books b ON a.year = b.year")
except chloedb.DeadlineError:
...
A query checks its deadline and interrupt between segments and every 1,024 rows it groups, orders, walks or joins. A write is not cut short: sql's limits apply to a SELECT. Ctrl-C reaches Python only when a call returns, so a notebook stops a runaway query with timeout_ms or interrupt() from another thread.
Queries built in Python#
table() starts a query of the table built from Python values instead of SQL text, so nothing is quoted or escaped: fields are named as the schema names them (col("order"), col("doc no")), and values keep their types (a str is text whatever quotes it holds, a date or datetime a timestamp, UTC unless it says otherwise). Each method returns a new query; iterating one, or run(), runs it and gives its Result:
from chloedb import col, match, bm25, similarity, rrf, count, score
q = (db.table()
.select("id", "title", score())
.where((col("year") >= 2019) & match("body", "graph neural") & ~col("tags").isin(["draft"]))
.order_by(bm25("body", "graph neural"))
.limit(10))
for row in q:
print(row["id"], row["score()"])
db.table().select("genre", count().alias("n"), col("rating").avg().alias("avg")).group_by("genre").having(count() > 10).order_by(col("genre"))
db.table().order_by(rrf(bm25("title", text), similarity("emb", db.embed(text)))).limit(10)
# each video's best clip, the 10 best videos
db.table().select("video", "id", score()).distinct_on("video").order_by(bm25("transcript", text)).limit(10)
col(name), lit(value) |
a field; a value where an expression is wanted |
==, !=, <, <=, >, >= |
comparisons with a value or another expression |
&, \|, ~ |
and, or, not (Python's and, or, not and chained comparisons such as 1 < col("x") < 5 raise TypeError) |
.isin(values), .between(lo, hi), .is_null(), .is_not_null() |
as SQL's IN, BETWEEN (a bound of None is open), IS NULL |
match(field, query, min=None, min_rows=None, max_rows=None) |
the full-text condition, in MATCH's syntax; min=2 or min="75%" asks for that many of its items, and with max_rows (or min_rows) for as many as fit |
at_least(n, *conds, min_rows=None, max_rows=None), count_true(*conds) |
at least n of the conditions hold (n a count, or a percentage such as "75%"), as SQL's AT_LEAST, or with max_rows (or min_rows) as many as fit; how many hold, a number to rank by |
bm25(field, text, syntax=None, min=None, min_rows=None, max_rows=None), similarity(field, vector), rrf(*scores, k=60, depth=100), ranked(keys) |
scores; bm25 reads weights, required and excluded words ("+mrna vaccine^2 -animal") unless syntax="plain", and in order_by with max_rows ranks the rows with as many of its words as fit; similarity takes a vector (embed(text) gives a text's), or {term: weight} for a SPARSE field; ranked is a ranking from elsewhere, primary keys best first |
+, -, *, /, %, **, abs(), round(), ln, log, exp, sqrt, sigmoid, clamp, least, greatest, coalesce, tmm(x, lo=None, hi=None), minmax, zscore, rank, case((cond, value), ..., otherwise=None) |
a ranking expression of scores, fields and numbers: bm25("body", q) * (1 + 0.2 * coalesce(col("rating"), 0)). + adds raw values; 0.3 * tmm(bm25(...)) + 0.7 * tmm(similarity(...)) is weighted fusion |
gbdt(model, *features) |
a ranking model's score of a row from its features: gbdt("docs_ltr", bm25("body", q), col("rating")), the LightGBM or XGBoost model <models>/docs_ltr/gbdt.json |
reachable(field, seeds, hops=1, ...), path(field, seeds, to, ...), ppr(field, seeds, ...) |
graph conditions and PageRank as a score, with the SQL functions' options as keyword arguments |
score(), hops() |
the score a row was ranked by; its hops from a traversal's seeds |
features() |
in select, a column for each input of the ranking (features), named as Python writes it; any expression of scores that scores a row by itself is a column too |
count(), col(f).count(distinct=True), .sum(), .avg(), .min(), .max() |
aggregates, for select, having and order_by over groups |
.alias(name), .asc(), .desc(nulls_first=False) |
a column's name; an order_by direction |
select(...) names the columns (without it, the stored fields but vectors, as SELECT *), where(...) keeps rows, order_by(...) ranks by a score best first or orders by fields ascending, rerank(expr, count) ranks its top count rows again (rerank phases), group_by(...) and having(...) group, distinct_on(...) keeps the first row of each group in the query's order, max_per(k, field) keeps at most k rows of each value, and limit(n) and offset(n) page (groups, after distinct_on). A query is a plan, the same JSON that plan(sql) gives: q.plan() shows it, and a Client runs it on a server the same way.
A server from Python#
chloedb.connect(url, token=None, timeout=30, retry_for=10) returns a Client of a running chloedb-server, using only the standard library. A method given table=, and table(name), read that table alone, never the first one, so a key limited to tables works with them since 0.3.2. It has schema, tables, query, sql, search, insert, upsert, delete, import_file (an upload to /v1/import, up to the server's --max-body-mb, 256 MiB unless set), import_sqlite(path, tables=None, append=False) and plan_sqlite(path) (a SQLite file's tables, uploaded the same way; SQLite in and out), export_sqlite(path, tables=None) (the server's tables downloaded as a SQLite file), import_parquet(path, table=None, key=None), plan_parquet(path) and export_parquet(path, table=None) (Parquet over HTTP) and len(), as above, plus get(key), get_many(keys) and put(key, row, if_seq_no=None) for rows by key, write(ops) for raw write operations, explain, run(plan) (a plan from explain(...)["ir"] or a table() query, run by /v1/plan), table() (queries built in Python), embed(text, field=None) (a text's vector by the server's model, from /v1/embed, or embed(image=data) an image's and embed(audio=data) a sound's), embed_tokens(text, field) (a query's vectors a token, for a multi-vector field), query_logged and export_rank_log (the rank log), health, stats and ask(question) for the agent.
client = chloedb.connect("http://127.0.0.1:7700") # token= or $CHLOEDB_TOKEN for a server with --token
client.upsert({"id": "b3", "title": "Star letters", "year": 2024})
client.search("letters", limit=3).dicts()
A server answers at most --max-result-window rows a query (10,000 by default). scroll(sql, params, page_size=10_000, arrow=False, keep_alive_ms=None) reads past it: it yields the answer a page at a time, Results or with arrow=True pyarrow Tables, each page at most page_size rows and at most the window, every page from the version of the database the first read (scrolls). Leaving the loop early ends the scroll on the server. A Database has scroll too, whose pages are slices of one answer, since a database in your process has no window.
for page in client.scroll("SELECT id, title FROM books WHERE year >= :y", y=1950, page_size=5000):
handle(page.dicts())
The server embeds {"embed": text} parameters and the rows it is sent with its own models. Errors the server reports raise chloedb.ChloeDBError (or a subclass of it, below), with the HTTP status as .status, the JSON answer as .answer and a stable .code; a server that cannot be reached raises urllib.error.URLError. Each thread keeps one connection to the server open between its requests (a new one after 4 seconds idle, or when the server closed it), so a checkpointer's steps over https do not pay a TLS handshake each; through a proxy ($HTTPS_PROXY, $HTTP_PROXY) each request goes by urllib as before. A float that is not a number (NaN, infinity) is sent as null, as a database in your process stores it. A request the server refuses for its load (429: behind on flushing its writes, its write queue full, too many queries running) did nothing, so the client sends it again after the Retry-After the server gives, for up to retry_for seconds (10; 0 sends each request once), and raises the 429 after that. A write that failed partway on a cluster (an error with "partial": true) is not sent again, since the shards that took it keep it, unless it has an idempotency key.
write(ops, idempotency_key=k) and sql(statement, idempotency_key=k) send a write with an idempotency key, so that sending it again does not apply it twice. A write that failed partway through a coordinator, after some shards took their part, says so in its error's answer and names its key:
try:
client.write(ops)
except chloedb.ChloeDBError as e:
if not (e.answer or {}).get("partial"):
raise
client.write(ops, idempotency_key=e.answer["idempotency_key"]) # the rest, once
A server started from Python#
since 0.3.7 chloedb.serve(db=None, listen=None, token=None, args=(), binary=None, wait=60) starts a chloedb-server on this machine and returns a Client of it, which stops the server when it is closed or its with block ends (and this Python process stops any it started when it ends). It is for a script, a notebook or a test that wants the server's API (the command center at the client's url, several processes on one database, a cluster's API before there is a cluster) without starting the program by hand:
import chloedb
with chloedb.serve("books.chloedb") as db: # the database is made if it does not exist
db.sql("CREATE TABLE books (id KEYWORD PRIMARY KEY, title TEXT, year INT)")
db.insert([{"id": "b1", "title": "The river and the dragon", "year": 1999}], table="books")
print(db.url, db.search("dragon", table="books").dicts())
Without db it serves a new database in a temporary folder, removed when the server stops. It listens on 127.0.0.1 at a free port (listen="host:port" picks the address; beyond loopback the server needs a token, which it is given in its environment and the client sends), with the gRPC API off and no apps started; args are more of the server's flags, such as ["--demo", "20000"], and win over those. wait is how many seconds it may take to answer /livez; one that stops first raises ChloeDBError with what it printed. The client's server is the Server: its url, db, process (the subprocess.Popen), running, output() (what it printed), and stop(timeout=10), which sends SIGTERM, then SIGKILL after timeout seconds. A copy of the client (pickled, or session()) talks to the same server and leaves it running when closed.
The program is binary when given, else $CHLOEDB_SERVER_BIN, else the one the chloedb-server package installed (pip install chloedb-server, or the server extra above), else chloedb-server on the PATH; chloedb.find_server() says which, and raises FileNotFoundError saying what to install when there is none. Like the client, it needs only the standard library.
From asyncio#
since 0.3.2 chloedb.AsyncClient(url, token=None, timeout=30, retry_for=10) is the client for asyncio (FastAPI, aiohttp): the same methods, each a coroutine, whose request runs in a thread of the event loop's default executor, so the package still needs nothing beyond the standard library and the loop is never blocked. What reads the server and is a property or len() on Client is a coroutine here: await db.schema(), await db.count(). scroll is an async iterator, session() gives an AsyncSession, and client is the Client underneath.
async with chloedb.AsyncClient("http://127.0.0.1:7700", token=key) as db:
hits = await db.search("letters", table="books", limit=3)
async for page in db.scroll("SELECT id FROM books"):
handle(page.dicts())
q = (await db.table("books")).where(col("year") >= 2000).limit(5)
rows = await db.run(q) # q.run() would block the loop
Reading your own writes#
A read from a replica, or through a coordinator whose shards have replicas, can be answered by a copy that has not applied a write yet. The servers fix that with sequence numbers (reads that see a write), when they number their writes (--seq-no):
- A write's count is a
chloedb.Written: anint(it compares, adds, prints and serialises as one, so code that used the number goes on working) with the write'sseq_nofrom a server, orseq_nos, one per shard, from a coordinator; both are None from a server without--seq-no.write,insert,upsert,delete,put, a write bysqland a write plan byrunanswer one;insertand the others that send several requests answer the latest numbers of all of them. query,query_arrow,search,get,get_many,scroll, a read bysql, a read plan byrunandsid_nexttakemin_seq_no=andmin_seq_nos=: the copy that answers holds those writes. A replica that has not applied them waits up to a second, then answers 503, and a coordinator passes the read to the shard's primary; a server refuses a number past its last write (409).client.session()(orchloedb.Session(url, ...)) is a client that does this itself: it keeps the latest numbers its own writes answered and sends them with each of its reads, so reading your own writes through a coordinator or from a replica is one line. A session is safe to share between threads;session.observe(written)adds a write made by another client. Numbers given to a read explicitly are sent with the session's, the later of each.
s = chloedb.connect("http://coordinator:7700").session()
s.upsert({"id": "u1", "name": "Ada"})
s.get("u1") # sees the upsert, whichever copy answers
w = client.upsert({"id": "u2", "name": "Grace"}) # or explicitly, by its numbers
client.get("u2", min_seq_no=w.seq_no, min_seq_nos=w.seq_nos)
The numbers go with reads only: a server refuses them on a write (a write sees every write before it), so a session leaves them off an INSERT, UPDATE or DELETE by sql, judging a statement by its first word. A Database takes min_seq_no and min_seq_nos and ignores them: a database in your process reads every write it committed.
Errors#
Every error ChloeDB raises is a chloedb.ChloeDBError (except chloedb.ChloeDBError catches them all) with a stable .code, the same in your process and from a server: the server's JSON code when it gives one, else the kind the engine's message starts with (busy: ...), else the HTTP status. The common codes are raised as subclasses:
| code | class | when |
|---|---|---|
bad_request |
BadRequestError |
a bad schema, query, value or request (HTTP 400, 413, 422); sending it again fails again |
unauthorized |
UnauthorizedError |
no token, or one without the rights the request needs (401, 403) |
not_found |
NotFoundError |
no such route, table or cursor (404); get of a missing key answers None instead |
conflict |
ConflictError |
refused by the state it met (409): an if_seq_no that no longer holds, an idempotency key still running, a min_seq_no past the server's last write |
conflict |
DuplicateKeyError |
since 0.3.2 an INSERT of a key the table already holds; a ConflictError and a BadRequestError both (it was a BadRequestError alone before), with .table, .field (the key field), .key and .row (the row's place in the write, from 1) where the message names them |
partial_write |
PartialWriteError |
a coordinator wrote some shards and not others (answer["partial"]): send it again with the same idempotency_key |
busy |
BusyError |
the database is open to write already, in another process or in this one (the message says which, and what to do instead), or another writer holds it (common errors) |
overloaded |
OverloadedError |
refused for the server's load before any work (429), after the client's retries |
timeout |
DeadlineError |
a deadline passed: a query's timeout_ms, a coordinator's wait for a shard (504), the engine's own |
unavailable |
UnavailableError |
no copy can serve it now (503): a replica behind, a primary replaced, a hosted model down |
A write through a coordinator that may or may not be applied (its shard's primary stopped answering, or was replaced while the write was being acknowledged) keeps its status's class, UnavailableError (503) or ConflictError (409), and says so: .in_doubt is true and .idempotency_key is the key to send it again with, which applies it once (the coordinator's own when the write had none; the server's Failover). Do not send it again as it was: without the key it could be applied twice.
chloedb.__version__ is the package's version since 0.3.2, and the package ships py.typed, so type checkers read its annotations.
Other codes have no subclass: memory (a query past its memory budget, limits), corrupt, io, internal (another 5xx) and error. chloedb.error_code(message, status, answer) gives the code of any message. A server that cannot be reached still raises urllib.error.URLError, and a client's own timeout TimeoutError, as before.
Database and Client: what differs#
A script moves between chloedb.open and chloedb.connect unchanged as long as it keeps to what both have. timeout_ms means the same on both: a query's deadline (limits). Options that only a server can honour raise TypeError on a Database rather than being taken as query parameters: idempotency_key (to sql, write), keep_alive_ms (to scroll), and timeout_ms to a method that runs no query (plan); a query parameter of one of those names goes in the params dict. The rest of the differences:
Database (in your process) |
Client (a server) |
|
|---|---|---|
| a write's count | int |
Written, an int with seq_no / seq_nos |
min_seq_no, min_seq_nos |
taken and ignored | sent: the answering copy holds those writes |
timeout_ms |
the query's deadline, none by default | sent: the query's deadline, the server's --timeout-ms (5 s) by default |
memory, interrupt() |
a query's budget; stops the running queries | the server's --query-memory-mb |
idempotency_key, keep_alive_ms |
TypeError |
sent |
scroll |
slices of one answer | pages of a server-side scroll, past --max-result-window |
export_sqlite |
returns a dict | returns the tables' names |
export_parquet |
tables=, returns a dict |
table=, returns the table's name |
import_sqlite, import_parquet |
take schemas= / schema= |
the server's schemas |
| a plan | plan(sql) |
explain(sql)["ir"] |
| only here | optimize, vacuum, expire, refresh, add_column, drop_column, rename_column, import_arrow, embed_errors |
put, stream, source, sources, plan_sqlite, plan_parquet, health, stats, ask, session |
| errors | ChloeDBError subclasses, status None |
the same classes and codes, with status and answer |
| unreachable | urllib.error.URLError |
Streams and Kafka#
client.stream(events) writes an iterable of stream lines through /v1/stream as it is read, each an operation or one event's operations as {"ops": [...], "source": {"name", "offset", "ts"}}, and returns the stream's last answer (lines, written, skipped). client.source(name) is the offset of the last event a source wrote (None for none), and client.sources() lists them all. A server skips the operations of an offset it has reached, so a stream resumed from source(name) + 1, or from anywhere before it, writes each event once.
chloedb.ingest.kafka is a Kafka (or Redpanda) consumer built on it (for one that needs no process of yours, the server reads topics itself with a Kafka connector), which needs confluent-kafka (pip install confluent-kafka, or pip install "./chloedb-py[kafka]"). Each partition is a source named <topic>/<partition> (after prefix=), and a partition given to the consumer is read from after the offset the server has, not from what the consumer group committed to Kafka, so each event is written once across consumer restarts, server restarts and rebalances. Within a partition the events are written in order.
import json
import chloedb
from chloedb import ingest
def click(e): # an ingest.Event: topic, partition, offset, timestamp, key, value
c = json.loads(e.value)
return [
{"op": "insert", "table": "events", "doc": {"id": c["id"], "usr": c["usr"], "item": c["item"]}},
{"op": "add", "table": "users", "key": c["usr"], "field": "n_events", "value": 1},
]
ingest.kafka(chloedb.connect("http://127.0.0.1:7700"), "clicks", click, brokers="127.0.0.1:9092", group="chloedb")
The function gives an event's operations, which commit together with its offset; None writes the offset alone (for events to leave out). Without one, each value is JSON: an operation, or {"ops": [...]}. The consumer runs until interrupted, or until max_events= events were read or none came for idle= seconds, and returns counts (events, written, skipped, streams, retries). It keeps a stream open for seconds_per_stream= (1 by default), then commits the stream's offsets to Kafka for Kafka's own lag tools and opens the next. When the server cannot be reached, or answers 429 or 5xx, it waits and reads every partition again from the server's offsets, for up to retry_for= seconds (60). An event the function fails on stops it after the events before it are written, so a restart begins with that event. config= adds librdkafka settings, and consumers of one group share the partitions. ingest.consume(client, log, to_ops) runs the same loop over another log, through the small ingest.Log interface.
Common errors#
The errors people meet first, each as the last line of its traceback, with what it means and what to do. The troubleshooting page has the server's and the installer's too.
BusyError: busy: the database is open elsewhere#
chloedb._native.BusyError: busy: the database is open elsewhere: process 3541 (python app.py), the parent of this process, has it open for writing. A reloader (Flask's debug mode, Django's runserver, uvicorn --reload) runs the program again in a child while its first process keeps what it opened at start: ... In Python: open it in a function the routes call (functools.cache keeps one), app.run(debug=True, use_reloader=False), or chloedb.connect('http://127.0.0.1:7700') to chloedb-server --db shop.chloedb
chloedb.open of a file that is open to write already: in another process (a second script, a chloedb-server serving it, the process that Flask's debug reloader starts beside yours, a second gunicorn worker), or in this one, by an earlier chloedb.open of the same path that is still open (a module imported under two names, a notebook cell run again before the old db was closed, a test that never closed its database). since 0.3.8 The message goes on to say which, from the process id the writer keeps beside the file: this process has it open for writing already, process N (command), the parent of this process (a reloader, as above), or process N (command) has it open for writing for any other process (the command on Linux), with what to do in each case, in Python's words at the end. Before 0.3.8 it stopped at the database is open elsewhere.
- In one process, open a database once and share the object: a
dbat module level that every request and thread uses (threads may query and write it at once).db.close(), or awithblock, closes it, and the nextopenthen succeeds. - Under a reloader (Flask's
debug=True, Django's runserver,uvicorn --reload), open the database in a function the routes call, withfunctools.cacheon it so that one is kept, rather than at import: the reloader's first process imports the module and never serves, so only the process that serves opens it. Or run without the reloader:app.run(debug=True, use_reloader=False). - To read it from other processes while one writes it, open it there with
chloedb.open(path, read_only=True): each query sees the writer's commits, and writes raiseChloeDBError. - To write from several processes (a web app's workers, a reloader), serve the file with
chloedb-server --db pathand connect each process withchloedb.connect(url), as Build a search page does. chloedb-server --db pathon a file a script has open stops withERROR chloedb_server: open path: busy: the database is open elsewhere: stop the script, or let it use the server.
URLError: <urlopen error [Errno 111] Connection refused>#
urllib.error.URLError: <urlopen error [Errno 111] Connection refused>
A client (chloedb.connect, chloedb.AsyncClient) found nothing listening at its URL: the server is not running, or it listens on another address or port (its log's HTTP and UI on ... line says which). curl -s http://127.0.0.1:7700/livez answers when one is up. The client tries again only when a server refused a request for its load (429), so it fails at once when none is there. URLError is not a ChloeDBError, so except chloedb.ChloeDBError lets it through; it is an OSError, as the client's own TimeoutError is:
try:
hits = db.search(q, table="books").dicts()
except chloedb.ChloeDBError as e: # the server answered with an error: e.code, e.status, e.answer
...
except OSError: # no server answered (urllib.error.URLError), or none in time
...
BadRequestError: ... bad value for minutes: "five"#
chloedb._native.BadRequestError: schema error: row 1: schema error: bad value for minutes: "five"
A value of another type than its field's: here text for an INT field, in the first row of the write (rows count from 1). An insert writes all of its rows or none. Form fields and query strings arrive as text, so convert them before writing (int(form["minutes"])); db.tables() lists each field's type. The same mistake in SQL says query error: minutes is an INT field: give it a number, not text 'five', and a field the table does not have says schema error: row 1: schema error: no field minuts.
ChloeDBError: books.chloedb does not exist: pass a schema to create it#
chloedb.open(path) without a schema, where no database is: a misspelt path, or a script started from another folder (a relative path is read from the current directory). Give the right path, or the schema to create the database. Its code is error.
ChloeDBError: db.search names no table, and this server has 3: ...#
_chloedb.ChloeDBError: db.search names no table, and this server has 3: products, library_help, recipes. Pass table=, as in db.search(..., table='recipes')
A call that reads or writes one table (search, insert, upsert, delete, get, import_file) without table=, on a database or server of several tables. Name the table, as the message shows; a database of one table needs no name.
TypeError: query parameters are named in ChloeDB's SQL (:name) ...#
TypeError: query parameters are named in ChloeDB's SQL (:name) and given by keyword or in a dict, not by position as sqlite3's ? and psycopg's %s take them: db.query('SELECT id FROM recipes WHERE MATCH(name, :p1)', p1='soup')
Values by position, a tuple or list with ? or %s in the SQL, as sqlite3 and psycopg take them. Write :q where the value goes and pass q=..., or the dict {"q": ...}. A ? or $1 sent another way (over HTTP, from the command center's console) gets the same advice from the server.
ChloeDBError: corrupt database: no valid meta slot#
_chloedb.ChloeDBError: corrupt database: no valid meta slot
Most often the file is not a ChloeDB database at all: a JSON Lines file, a CSV or a SQLite database opened by mistake. ChloeDB does not change that file, but leaves an empty <path>-readers folder beside it, which you can delete. Rows from such a file go into a database with db.import_file(path) (JSON Lines, JSON, CSV, TSV), db.import_sqlite(path) or db.import_parquet(path). A database that is really damaged names what is damaged and how to recover (damaged files). Its code is corrupt.
A vector field whose model is not open#
chloedb._native.BadRequestError: schema error: notes.emb is embedded by model potion-retrieval-32M, which is not open: the row would be written without its vector, which similarity() never finds. Open the model (...), give the vector in the write, or ...
A write that leaves a vector to a model the database could not open. db.embed_errors says why, for each such field:
>>> db.embed_errors
{'emb': 'schema error: model potion-retrieval-32M: no /home/me/.local/share/chloedb/models/potion-retrieval-32M/model.safetensors (fetch it first)'}
Fetch the model once with chloedb-server fetch-model potion-retrieval-32M (into ~/.local/share/chloedb/models, or $CHLOEDB_MODELS), or open the database with models= the folder that has it, then open the database again. Until then, a query with {"embed": text} fails with query error: no embedded field has its model open. The BERT models (bge-small-en-v1.5 and others) do not run in the prebuilt wheels: embed_errors then says model bge-small-en-v1.5 is a BERT model: build with thebertfeature, and troubleshooting says what to use instead.
The rank log#
query_logged(sql, params, context=..., variant=..., candidates=...) runs a ranked query and writes what it served to a rank log: a row per row answered, with its position, its score and the values its ranking read, as they were computed. candidates logs that many rows from the query's OFFSET, those past its LIMIT as not shown, and ts (a datetime or microseconds) says when it was served, now by default. explore (a dict, {"policy": "softmax", "tau": 0.5, "top": 20}) shows the top rows in an order drawn by a logging policy and logs each row's chance of its place. The Result is the query's, with result.request, the id its log rows carry, for labelling them later, and result.propensities, the shown rows' chances when it explored. export_rank_log(...) reads the log back as a list of dicts for training, each with its context's history and its label as of the row's time, from a table of events:
r = db.query_logged("SELECT id, title FROM books ORDER BY bm25(title, :q) DESC LIMIT 10",
{"q": "dragon"}, context="u42", variant="b", candidates=50)
db.sql("UPDATE rank_log SET label = 3 WHERE request = :r AND row_key = :k", {"r": r.request, "k": r.keys[0]})
rows = db.export_rank_log(events={"table": "events", "user": "usr", "item": "item", "time": "ts", "action": "action"},
history=20, window="30 minutes", grades={"click": 1, "buy": 3})
rows[0]["features"], rows[0]["label"], rows[0]["history_items"]
Semantic-ID codebooks#
A semantic-ID field gives each row a code from its vector by a codebook, which chloedb.train.codebook trains and saves in the models directory before a table names it:
h = chloedb.train.codebook(db, "items-rq", field="emb") # a table's vectors
h = chloedb.train.codebook(vectors, "items-rq") # a list of lists, or a 2-D numpy array
items = chloedb.open("items.chloedb", schema={
"table": "items", "key": "id",
"fields": [
{"name": "id", "type": "keyword"},
{"name": "emb", "type": "vector", "dim": 384},
{"name": "sid", "type": "keyword", "multi": True,
"semantic_id": {"from": "emb", "codebook": "items-rq"}},
],
})
items.upsert(rows) # each row gets its sid
items.query("SELECT id FROM items WHERE sid = '12.7'")
It returns the SHA-256 of the file, which the table records. The options are levels (1 to 4, default 3), k centroids a level (up to 256), iterations of k-means a level (25), sample, the vectors it trains on (100 a centroid by default, drawn at random), seed (the same vectors and options give the same codebook), balanced, table (a database's first by default) and models (the default models directory, as for Database.create). Pass the same models= to Database.create and Database.open: a codebook that does not open is listed in db.embed_errors, and writes that need a code then fail.
chloedb.train.codebook_report(db, "items-rq", field="emb") (or of vectors, as above) says what a codebook makes of a set of vectors, before a field names it for good:
r = chloedb.train.codebook_report(vectors, "items-rq")
r["rows"], r["codes"], r["shared_rows"], r["largest_group"] # 12101, 10126, 3288, 12
r["levels"][2] # {"used": 256, "fullest": 630, "perplexity": 56.7, "residual": 0.135}
shared_rows are the rows whose code another row has too: only the tie number tells them apart, so a decoder that writes codes cannot. A level's perplexity is how many of its numbers the rows are spread over when weighed by use (k when every number has as many rows as any other), and residual the share of the vectors' squared length the code so far leaves unsaid.
Plain k-means uses the later levels' numbers unevenly, as above: what a level leaves of the vectors points every way alike, and there most centroids end on one far vector each while a few near the middle take the rest. balanced=True trains each level with every centroid given the same number of the training vectors. On the 12,101 items of Amazon Beauty (text vectors of 384 dimensions, 3 levels of 256) the third level's perplexity goes from 57 to 251 and the rows sharing a code from 3,288 to 2,601; training takes 17 s, not 6 (results). It holds a distance for every training vector and centroid (8 bytes each: 52 MB at the default sample). Rows are coded by their nearest centroid either way.
Next items#
chloedb.ngrams keeps counts of what came after the last one or two items in a table, and chloedb.next_items(db, history, k=10) reads them with backoff (Next items):
from chloedb import ngrams
db = chloedb.open("next.chloedb", ngrams.schema())
db.write(ngrams.ops(["a", "b"], "c")) # after each event: "c" after [..., "a", "b"]
chloedb.next_items(db, ["a", "b"], k=10) # [("c", 1.0), ...]
Sequence models#
encode('model', history) turns a user's events into a vector of an item field, by a model that chloedb.train.sequence trains with PyTorch (pip install torch) and saves in the models directory:
import chloedb.train
db = chloedb.Database.open("shop.chloedb", models="models")
out = chloedb.train.sequence(db, "next", table="items", field="emb",
events={"table": "events", "user": "usr", "item": "item", "action": "action", "time": "ts"},
learn_items=True, negatives="all", holdout=True, models="models")
chloedb.train.write_vectors(db, "next", models="models") # the learned item vectors, into items.emb
db.query("""SELECT id FROM items ORDER BY similarity(emb, encode('next',
(SELECT item, action, ts FROM events WHERE usr = :u ORDER BY ts DESC LIMIT 200))) DESC LIMIT 10""", {"u": "u42"})
- The histories come from one of
events(a table of the database: its columns of users, items, and optionally actions, times, andorderfor events with equal times),sequences(a dict from user to a list of(item, action, time in seconds), oldest first) orrecords(the rank log's export withhistory: each record's history is a training example, and its row the target when its label is positive or missing). The item vectors are the field's, orvectors(a dict from key to vector). - What it learns: every event is the target of the events before it. Each item is scored as
similarityscores it, the dot product of the model's unit vector with the item's vector, overtemperature(0.05), and the loss is cross-entropy against the other targets of the batch (negatives="in_batch", the default) or every item ("all", for catalogs that fit). In a batch, an item that is often a target is a negative as often, which teaches the model to rank what is popular low:logq=Truetakes from each negative's score the log of the chance that the batch holds it, worked out from how often each item is a target (the target of its own row keeps its score), anduniform_negatives=naddsnitems drawn evenly from all of them to every batch, so items that are seldom or never targets are negatives too. Use both for a catalog too large for"all": on Yambda-50M's 629,298 tracks they took Recall@10 on held-out events from 0.0229 to 0.0334 after four epochs (results). Withlearn_items=Truethe item vectors are trained too, starting from the field's, and returned normalized inout["vectors"]; without it the field keeps its vectors and the model learns to map histories into their space, so a new item is found as soon as it has a vector. - The item vectors go with the model: it is saved with the vectors it was trained with (
items.safetensors, part of its hash), andencoderefuses a history whose items have other vectors in the field.chloedb.train.write_vectors(db, name, models=None)writes them into the field the model's config names, by anUPDATEof that field alone for each item (aDatabaseor aClient); returns the rows written.chloedb._sequence.read_items(path)reads them back. - Options:
width(64),layers(2),heads(1),ffn(4 × width),max_len(200 events),gap_buckets(32),actions(the actions seen by default; others count as one more),epochs(50),batch(128),lr(1e-3),dropout(0.2),seed. Withholdout=Trueeach user's last event is kept out of training, and training stops when NDCG@10 on those has not risen forpatienceepochs (5), keeping the best epoch.max_secondsstops it after the first epoch that ends past that many seconds.log=printprints each epoch's loss and metrics, whichout["history"]also holds. - It returns
{"path", "history", "vectors"}. Pass the samemodels=toDatabase.open, or start the server with--models: a coordinator needs the model, its shards don't.
Search on the device#
chloedb.nearby(url, table, dir) holds a slice of a chloedb-server's table near a user: the rows of the tiles around them in a database in dir, searched with the server's statistics. move_to(lat, lon) fetches the tiles it lacks, and search(q, k=10, d0=1000) answers on the device when it can prove the answer is the server's, and asks the server otherwise; the result's where is "device" or "server" and why the reason. For a vector slice, focus(taste, rows=20_000) holds the clusters nearest a taste vector and similar(vector, k=10) answers a similarity search the same way, fetching the few clusters its codes name when that proves the answer.
near = chloedb.nearby("http://127.0.0.1:7700", "places", "./slice")
near.move_to(40.7484, -73.9857)
r = near.search("pizza", k=10)
r.where, r.why # ("device", "proved")
Wheels, platforms and building from source#
The package is in chloedb-py/: a Rust extension built with maturin, for CPython 3.10 and later. pip install chloedb (install) is all most machines need; this is what it installs, and the other ways.
Prebuilt#
Each chloedb-py-v<version> release on GitHub has a wheel per platform, for Python 3.10 and later, that installs with no Rust and no compiler. The same wheels are on PyPI, so pip picks the one for your machine:
pip install chloedb
| platform | wheel |
|---|---|
| macOS 11+ on Apple silicon | chloedb-<version>-cp310-abi3-macosx_11_0_arm64.whl |
| macOS 11+ on Intel | chloedb-<version>-cp310-abi3-macosx_11_0_x86_64.whl |
| Linux x86-64, glibc 2.28+ (RHEL 8, Amazon Linux 2023, Debian 10, Ubuntu 20.04 and later) | chloedb-<version>-cp310-abi3-manylinux_2_28_x86_64.whl |
| Linux aarch64, glibc 2.28+ | chloedb-<version>-cp310-abi3-manylinux_2_28_aarch64.whl |
| Linux x86-64 and aarch64, glibc 2.35+ (Ubuntu 22.04, Debian 12 and later), release 0.2.0 | chloedb-0.2.0-cp310-abi3-manylinux_2_35_x86_64.whl, ..._aarch64.whl |
The workflow py-release.yml builds them on a tag chloedb-py-v<version> (or strata-py-v<version>; the version in chloedb-py/pyproject.toml), installs each one into an empty virtualenv and runs the embedded tests against it, then attaches them to the release. The Linux wheels are built in the manylinux_2_28 container (AlmaLinux 8). The same tag publishes them to PyPI, from 0.3.0 on; pip install chloedb -f https://github.com/chloetech-ai/chloedb-releases/releases/expanded_assets/chloedb-py-v<version> installs a release from GitHub instead. Windows and Alpine (musl) have no wheel: build from source there (Windows through WSL 2).
Only the client, with no build#
chloedb.connect, the client of a running chloedb-server, needs only Python's standard library. Without the extension, from a checkout of the repository, put chloedb-py/python on the path and import it:
PYTHONPATH=chloedb-py/python python3 -c 'import chloedb; print(chloedb.connect("http://127.0.0.1:7700").health())'
The package imports without the extension, and chloedb.Client, chloedb.connect, chloedb.serve, chloedb.ChloeDBError, Result and the query builder work. A database in the process (chloedb.open, chloedb.Database, from_sqlite, the codebook training) raises ImportError saying what to install.
From source#
It needs Rust (stable) and a C compiler; the build takes about 12 minutes on 4 cores.
pip install ./chloedb-py # from the repository's root: builds and installs a wheel
or, while working on it:
cd chloedb-py python3 -m venv .venv && . .venv/bin/activate pip install maturin pytest maturin develop --release # builds the extension into the virtualenv pytest tests # the client tests also need chloedb-server built
The package includes DataFusion, which runs joins, groups and sorts over many joined rows (--no-default-features leaves it out). Model2Vec embedding models need nothing more. For the BERT models, build with --features bert (--features accelerate on a Mac): maturin develop --release --features bert, or MATURIN_PEP517_ARGS="--features bert" pip install ./chloedb-py. The extension runs on any CPU of its architecture: ChloeDB picks its SIMD kernels (AVX2, AVX-512, the aarch64 dot product instructions) at run time. RUSTFLAGS="-C target-cpu=native" builds one for the building machine only.