Transformation Layer
The Transformation Layer maps raw database rows (from insert, update, or delete operations) into standard LangChain Document instances, manages unique document keys, and sanitizes input data.
Module Summary
Converts a DebeziumEventModel into a BuildResult (Document + ID + op). |
|
Abstract ID strategy — produce a stable document ID from a CDC event. |
|
Default strategy: |
|
SHA-256 hash of specified PK fields — safe for composite or binary keys. |
|
User-supplied callable ID strategy for full control. |
|
Top-level projection policy with optional per-table overrides. |
|
Field-level projection configuration for a single table. |
|
Coerce metadata values to types Chroma/PGVector/Milvus can store. |
API Reference
Document builder — converts CDC events into LangChain Documents.
This is the bridge between the ingestion/canonical layer and the synchronization/vector-store layer. Each CDC event produces a (Document, doc_id, op) BuildResult that the SyncManager consumes.
- class BuildResult(doc_id, op, document=None)[source]
Bases:
objectOutput of DocumentBuilder.build() for a single CDC event.
- Variables:
- Parameters:
- class DocumentBuilder(id_strategy=None, projection_policy=None, extra_metadata=None, sanitizer=None)[source]
Bases:
objectConverts a DebeziumEventModel into a BuildResult (Document + ID + op).
- Wires together:
IdStrategy → stable document ID
ProjectionPolicy → which fields → page_content / metadata
- Additional system metadata injected into every document:
_tabletable name_schemaschema/namespace_opCDC operation character_ts_mssource event timestamp (ms since epoch)_sourceoptional Debezium source block fields
Usage:
builder = DocumentBuilder() result = builder.build(event) # result.doc_id, result.op, result.document
- Parameters:
id_strategy (IdStrategy | None)
projection_policy (ProjectionPolicy | None)
sanitizer (Callable[[dict[str, Any]], dict[str, Any]] | None)
- build(event, allow_soft_delete=False)[source]
Build a BuildResult from a canonical CDC event.
- Parameters:
event (DebeziumEventModel) – The DebeziumEventModel to convert.
allow_soft_delete (bool) – If True, builds a Document with _is_deleted=True instead of returning None for delete operations.
- Return type:
Deterministic document ID strategies for vector store synchronization.
A stable, unique document ID per logical row is the foundation of correct CDC-to-vector-store synchronization. The same row must always produce the same ID across insert, update, and delete events so upsert/replace semantics work correctly.
- class IdStrategy[source]
Bases:
ABCAbstract ID strategy — produce a stable document ID from a CDC event.
- abstract generate(event)[source]
Return a stable, unique document ID for this event’s logical row.
- Parameters:
event (DebeziumEventModel)
- Return type:
- class TablePkIdStrategy(pk_fields=None)[source]
Bases:
IdStrategyDefault strategy:
{schema}.{table}:{primary_key}.Examples
public.orders:42public.customers:john@example.cominventory.products:SKU-001|warehouse-A(composite key)
Uses
event.extract_primary_key(pk_fields)which reads from the Debezium JSON key field first, then falls back to payload columns.- generate(event)[source]
Return a stable, unique document ID for this event’s logical row.
- Parameters:
event (DebeziumEventModel)
- Return type:
- class CompositeIdStrategy(pk_fields)[source]
Bases:
IdStrategySHA-256 hash of specified PK fields — safe for composite or binary keys.
Produces IDs like
public.orders:a3f2c1d4e5b6...(20-char hex prefix).- generate(event)[source]
Return a stable, unique document ID for this event’s logical row.
- Parameters:
event (DebeziumEventModel)
- Return type:
- class CustomIdStrategy(fn)[source]
Bases:
IdStrategyUser-supplied callable ID strategy for full control.
- Parameters:
fn (Callable[[DebeziumEventModel], str])
- generate(event)[source]
Return a stable, unique document ID for this event’s logical row.
- Parameters:
event (DebeziumEventModel)
- Return type:
- resolve_id_strategy(strategy, **kwargs)[source]
Resolve a strategy name string or an existing IdStrategy instance.
- Parameters:
strategy (str | IdStrategy) –
"table_pk","composite", or an IdStrategy instance.**kwargs (object) – Forwarded to the strategy constructor (e.g.
pk_fields).
- Raises:
ValueError – If the name is not recognised.
- Return type:
Field projection and content-template policies for document building.
- A ProjectionPolicy controls:
Which row fields become the
page_contentof a LangChain Document.Which fields appear in LangChain
metadata(for filtered retrieval).Optional pre-compiled
string.Templatecontent templates (e.g."$name — $description").Per-table overrides so different tables have different rendering rules.
- class TableProjectionPolicy(include_fields=None, exclude_fields=<factory>, metadata_fields=None, content_template=None)[source]
Bases:
objectField-level projection configuration for a single table.
- Variables:
include_fields (list[str] | None) – If set, only these fields enter the content block.
exclude_fields (list[str]) – Fields excluded (binary blobs, secrets, etc.). Applies only when include_fields is not specified.
metadata_fields (list[str] | None) – Fields copied into Document.metadata; None = all.
content_template (str | None) – Optional
string.Templatepattern. Uses$variableor${variable}syntax. Example:"$product_name — price: $price"
- Parameters:
- class ProjectionPolicy(default=None, overrides=None)[source]
Bases:
objectTop-level projection policy with optional per-table overrides.
Usage:
policy = ProjectionPolicy( default=TableProjectionPolicy( exclude_fields=["password_hash", "avatar_blob"], ), overrides={ "products": TableProjectionPolicy( content_template="$name — $description — $$${price}", metadata_fields=["id", "category", "price"], ) }, ) page_content, metadata = policy.project(event)
- Parameters:
default (TableProjectionPolicy | None)
overrides (dict[str, TableProjectionPolicy] | None)
- project(event)[source]
Project the current row of a CDC event into (page_content, metadata).
Returns empty content string for delete events (no after row).
Metadata sanitization utilities for vector database compatibility.