Skip to main content

Engine and Tables

SereneDBEngine owns the psycopg 3 connection pool and issues every DDL statement the integration needs. It is the first object you create and the last one you close; a vector store is always built on top of an engine.

MethodPurpose
from_connection_string()Build an engine from a libpq connection string.
from_pool()Wrap an AsyncConnectionPool you already own.
init_vectorstore_table()Create the table, and optionally its index, in one call.
drop_table()Drop the table.
close()Dispose of the connection pool.

Every method comes in a synchronous form and an a-prefixed asynchronous form; both are documented in one entry.

Connecting​

SereneDBEngine(...) cannot be called directly — it raises Exception("Only create class through 'from_connection_string' or 'from_pool' methods!"). Use one of the two factories below.

from_connection_string()​

python
SereneDBEngine.from_connection_string(conninfo, *, ignore_db_version=False, **kwargs)
ParameterTypeDefaultMeaning
conninfostr—libpq connection string, e.g. "host=127.0.0.1 port=7890 user=postgres dbname=postgres".
ignore_db_versionboolFalseSkip the server version check.
**kwargsAny—Forwarded to psycopg_pool.AsyncConnectionPool (min_size, max_size, timeout, …). A nested kwargs={...} entry is forwarded to each connection instead.

How it works. The first call creates a process-wide background event loop running on a daemon thread; every engine built this way shares it, which is what lets the synchronous API delegate to the async implementation. The pool is constructed with open=False and opened on that loop, because an AsyncConnectionPool has to be opened from inside the loop that will drive it. The factory then opens it straight away and runs the server version check, so an unreachable server, bad credentials or an unsupported version fail here rather than on the first query. With ignore_db_version=True there is nothing to check, and the pool is opened lazily before its first use instead.

Per-connection options go in the nested kwargs dict. row_factory is forced to psycopg's dict_row — the integration reads rows as dictionaries, so do not override it.

python
engine = SereneDBEngine.from_connection_string(
"host=127.0.0.1 port=7890 user=postgres dbname=postgres",
min_size=2,
max_size=10,
kwargs={"application_name": "my-rag-service"},
)

The bootstrap superuser postgres connects locally without a password; other roles authenticate with their role password, which belongs in the connection string.

from_pool()​

python
SereneDBEngine.from_pool(pool, loop=None, *, ignore_db_version=False)
ParameterTypeDefaultMeaning
poolAsyncConnectionPool—An already open psycopg pool. The engine will not open it.
loopOptional[asyncio.AbstractEventLoop]NoneThe loop the pool is driven by. Required if you intend to call any synchronous method.
ignore_db_versionboolFalseSkip the server version check.

Use this when your application already manages its own pool — a web framework's lifespan hook, for example.

from_pool() is a plain (non-async) method, so it can only wait for the version check when loop runs on another thread. When loop is None, or is the loop you are calling from, the check runs instead just before the engine's first query, and an unsupported server raises there.

Server version check​

The integration supports SereneDB 26.11.3 and later. Each engine reads the server's release from SELECT version() — which reports, for example, PostgreSQL 18.6 (SereneDB 26.11.3) — and raises UnsupportedServerVersionError when it is older, or when the reply names no SereneDB release at all (a plain PostgreSQL server, for instance):

Text
UnsupportedServerVersionError: version 26.10.0 is not supported. Minimum supported version is 26.11.3. To disable version check use ignore_db_version=True

The check runs once per engine: when the engine is created, or before its first query for an engine from from_pool() that cannot wait during creation. UnsupportedServerVersionError is a RuntimeError, importable from langchain_serenedb.

Pass ignore_db_version=True to either factory to skip the check — for instance, against a development build, which always reports patch level 0. You then take the risk that some of the SQL the integration generates is rejected or behaves differently on that server.

python
engine = SereneDBEngine.from_connection_string(
"host=127.0.0.1 port=7890 user=postgres dbname=postgres",
ignore_db_version=True,
)

Once the check has passed, engine.server_version holds the release as a tuple of integers, such as (26, 11, 2). It is None before the check runs, and always None on an engine created with ignore_db_version=True.

Creating the table​

init_vectorstore_table() / ainit_vectorstore_table()​

python
engine.init_vectorstore_table(table_name, vector_size, *, schema_name="public", ...)

Creates the table a vector store binds to, and optionally its inverted index.

ParameterTypeDefaultMeaning
table_namestr—Table to create.
vector_sizeint—Embedding dimension N; the column is declared FLOAT[N].
schema_namestr"public"Schema for the table, its index and its text search dictionary.
content_columnstr"content"Column holding Document.page_content. Declared TEXT NOT NULL.
embedding_columnstr"embedding"Column holding the vector. Declared FLOAT[N] NOT NULL.
metadata_columnsOptional[list[Column | ColumnDict]]NoneTyped columns broken out of the metadata dict. See Metadata columns vs the JSON column.
metadata_json_columnstr"langchain_metadata"Column holding the remaining metadata as JSON.
id_columnstr | Column | ColumnDict"langchain_id"Primary key. A plain string means type UUID — see Choosing the id column type.
overwrite_existingboolFalseDROP TABLE IF EXISTS first, then recreate. Destroys existing data.
if_not_existsboolFalseEmit CREATE TABLE IF NOT EXISTS (and CREATE INDEX IF NOT EXISTS), making the call idempotent.
store_metadataboolTrueWhen False, no JSON metadata column is created.
vector_indexOptional[BaseIndex]NoneBuild the ANN index in the same call, e.g. IVFIndex().
hybrid_index_configOptional[HybridIndexConfig]NoneBuild the combined full-text + vector index (and its dictionary) in the same call. Build-time only — how queries fuse is set on the store, see Hybrid Search.
metadata_indexOptional[MetadataIndexConfig]NoneWhich metadata columns and JSON sub-fields join the index.

Only table_name and vector_size are positional; everything else is keyword-only.

python
from langchain_serenedb import Column, IVFIndex

engine.init_vectorstore_table(
"my_docs",
vector_size=768,
metadata_columns=[
Column("category", "TEXT", nullable=False),
Column("year", "INTEGER"),
],
vector_index=IVFIndex(),
)

Column​

A dataclass describing one typed column.

ParameterTypeDefaultMeaning
namestr—Column name. Raises ValueError("Column name must be type string") otherwise.
data_typestr—SQL type, emitted verbatim into the CREATE TABLE. Raises ValueError("Column data_type must be type string") otherwise.
nullableboolTrueFalse appends NOT NULL.

The data_type string is not validated against SereneDB's type list — an unknown type surfaces the database's error at CREATE TABLE time.

ColumnDict​

A TypedDict accepted anywhere a Column is, for callers that would rather pass a plain mapping. All three keys are required:

python
engine.init_vectorstore_table(
"my_docs",
768,
metadata_columns=[{"name": "category", "data_type": "TEXT", "nullable": False}],
)

Each key is type-checked at runtime and raises TypeError("The 'name' field must be a string."), TypeError("The 'data_type' field must be a string.") or TypeError("The 'nullable' field must be a boolean.").

Table shape​

The example above generates:

sql
CREATE TABLE "public"."my_docs"(
"langchain_id" UUID PRIMARY KEY,
"content" TEXT NOT NULL,
"embedding" FLOAT[768] NOT NULL,
"category" TEXT NOT NULL,
"year" INTEGER,
"langchain_metadata" JSON
);

On write, any metadata key matching a typed column is pulled out of the dict and bound to that column; whatever is left is serialized into the JSON column. A metadata key with no matching column and no JSON column is dropped. On read the JSON column is loaded first and the typed columns are layered on top, so Document.metadata comes back as one flat dict regardless of how the values were stored. Document.id is the id column rendered as a string.

Choosing the id column type​

Passing id_column as a plain string names the column and declares it UUID PRIMARY KEY. Generated ids are UUID4 strings, so the default works as long as you let the store assign them.

Metadata columns vs the JSON column​

Declaring a metadata key as a typed column buys you a real SQL type (so range comparisons behave like numbers or dates, not strings) and a column the inverted index can cover verbatim. Everything not declared still round-trips through the JSON column, and can still be filtered — see How a field resolves.

store_metadata=False omits the JSON column entirely. Then only declared metadata columns exist, metadata_index.json_fields raises ValueError, and a filter on an undeclared key is emitted as a bare column reference that the database rejects.

Creating indexes with the table​

An index is built in the same call when either vector_index or hybrid_index_config is given:

ArgumentsResult
NeitherTable only. Vector search falls back to an exact scan.
vector_index=IVFIndex(...)Table plus a vector-only inverted index on the embedding column.
hybrid_index_config=HybridIndexConfig()CREATE TEXT SEARCH DICTIONARY, then one combined inverted index over the content and embedding columns. The vector operator class defaults to IVFIndex().
BothThe combined index, using the vector_index you passed for the vector operator class.

To create the table without an index, simply omit both — and to remove an index later, call drop_vector_index() on the store.

The index is always named <table_name>langchainvectorindex and is not configurable; see One index per collection. metadata_index selects which metadata joins it, which is what makes metadata filters index-covered — see Metadata index configuration.

overwrite_existing=True drops and recreates the table. if_not_exists=True instead makes the call idempotent: the table and index are created only when absent, so re-running keeps existing data. The two are mutually exclusive and raise ValueError together.

Write visibility​

A write is visible to every search that starts after its call returns, full-text and vector alike: each write commits, and SereneDB shows a committed row through the table and through its index at once. See Visibility.

Dropping and closing​

drop_table() / adrop_table()​

python
engine.drop_table(table_name, *, schema_name="public")

Issues DROP TABLE IF EXISTS, which also removes the table's index. It does not drop the text search dictionary created for hybrid search.

close() / aclose()​

python
engine.close()

Disposes of the connection pool. The shared background loop and its thread are process-wide and are not shut down — they are daemon threads and do not keep the interpreter alive.

See also​