Skip to content

feat(vector-store): add IBM Db2 AI Vector Search as a vector store provider #6838

Description

@DhruvChaturvediIBM

Description

Adds IBM Db2 AI Vector Search as a fully supported vector store provider for Mem0 OSS. Db2 12.1.2+ ships a native VECTOR(n, FLOAT32) column type and VECTOR_DISTANCE function built into the engine — no extension, plugin, or sidecar process required. This means teams already running Db2 for their relational workloads can store and search memory embeddings in the same database, with no additional infrastructure.

Type of Change

  • New feature (non-breaking change that adds functionality)

Breaking Changes

N/A

What's included

Core implementation

mem0/vector_stores/db2.pyDb2VectorStore implementing the full VectorStoreBase interface:

Method What it does
insert Bulk inserts via executemany, hashes IDs to CHAR(16), stores vectors with VECTOR(CAST(? AS CLOB), dim, FLOAT32) and metadata as SYSTOOLS.JSON2BSON
search VECTOR_DISTANCE nearest-neighbour search with optional metadata filters and FETCH FIRST n ROWS ONLY
delete Accepts both raw string IDs and pre-hashed CHAR(16) IDs
update Can update embedding only, metadata only, or both in one call
get Single-row lookup by ID, returns None for missing records
list Full scan with optional metadata filters and top_k limit, returns the [[OutputData]] shape Mem0 expects
list_cols Queries SYSCAT.TABLES for all user tables in the current schema
delete_col DROP TABLE (no-ops safely when table is absent)
col_info Returns {schema, table_name, row_count} from SYSCAT.TABLES
reset Drop + recreate — table is fully usable immediately after

mem0/configs/vector_stores/db2.py — Pydantic Db2Config:

  • Accepts either a pre-built ibm_db_dbi.Connection via client or raw connection_params (database, host, port, username, password, optional security/ssl_cert for TLS)
  • Validates and normalises distance_strategy — supports EUCLIDEAN, COSINE, and DOT
  • Configurable column names (text_field, id_field, metadata_field, embedding_field) so it can be pointed at an existing table schema
  • Rejects unknown extra fields

Distance to similarity score conversion

Strategy Formula Range
EUCLIDEAN 1 / (1 + distance) (0, 1]
COSINE max(0, 1 - distance) [0, 1]
DOT returned as-is unbounded

Metadata filtering

Filters run against the SYSTOOLS.BSON2JSON metadata column via JSON_VALUE. Supported filter shapes:

  • Scalar equality{"user_id": "alice"}
  • List / IN{"category": ["work", "personal"]}
  • Wildcard existence{"run_id": "*"} (skipped in WHERE, matches any row)
  • Multi-field AND — top-level keys are combined with AND

Registration

  • mem0/vector_stores/configs.py"db2" added to the provider registry so VectorStoreConfig(provider="db2", config={...}) and Memory.from_config({"vector_store": {"provider": "db2", ...}}) both work
  • pyproject.tomlibm_db>=3.2.0 added to the vector-stores optional dependency group

Tests

tests/vector_stores/test_db2.py — 109 tests total:

  • 93 unit tests — fully mocked (ibm_db_dbi is stubbed via sys.modules when not installed), cover config validation, ID hashing, distance-to-score conversion, table creation, every CRUD method, NULL handling, filter SQL generation, and VectorStoreConfig provider registration
  • 15 live integration tests — run against a real Db2 instance, guarded by a @requires_db2_credentials skip marker, cover the full CRUD lifecycle plus test_live_documentation (end-to-end via Memory.from_config)
  • Credentials loaded automatically from examples/misc/.env via python-dotenv — no manual export needed

Documentation

  • docs/components/vectordbs/dbs/db2.mdx — new provider page with requirements, Memory.from_config usage example, full config table, score conversion explanation, and metadata filter examples
  • docs/docs.jsondb2 added to the Supported Vector Databases navigation group
  • docs/llms.txt — Db2 entry added so the docs-llms-txt-check CI check stays green

Example

examples/misc/db2_vector_store_example.py — a self-contained runnable demo (no LLM needed) that walks through all 10 operations against a live Db2 instance: connect, insert, similarity search, filtered search, list with filter, get by ID, update, delete, collection info, and reset.

Test Coverage

  • I added/updated unit tests
  • I added/updated integration tests
  • I tested manually — all 108 tests pass against a real Db2 12.1.2 instance; the example script runs end-to-end successfully

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation if needed

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions