Skip to main content

LangChain VectorStore for MySQL 9's native VECTOR type — works with ShannonBase, self-hosted MySQL, and MySQL HeatWave.

Project description

langchain-shannonbase

PyPI CI Python License: MIT

A LangChain VectorStore backed by MySQL 9's native VECTOR type. If your data already lives in MySQL, you can do retrieval without bolting a separate vector database onto your stack.

It works against three things that share the same VECTOR / STRING_TO_VECTOR / DISTANCE surface:

Backend What it is Good for
ShannonBase open-source "MySQL for AI" local dev and self-hosting, no subscription
MySQL 9 vanilla self-hosted MySQL you already run MySQL
MySQL HeatWave Oracle's managed MySQL production on OCI

Why it exists

Before this, if your data was in MySQL your LangChain options were thin. The one MySQL vector store in the ecosystem is locked to Google Cloud SQL, and ShannonBase's LangChain integration was on their wishlist but nobody had built it. This is the plain, self-hostable version: no cloud lock-in, no extra service to run.

It passes LangChain's standard vector-store integration suite, so it behaves like any other store you'd drop into a chain.

When to use this (and when not to)

ShannonBase can also do the whole retrieve-and-generate loop in one SQL call with sys.ML_RAG. If you're all-in on ShannonBase and happy in SQL, that's the simpler path and you probably don't need this.

Reach for this package when you're already building in LangChain: you want orchestration in Python, your own embeddings (OpenAI, a local model, whatever), or to plug MySQL into a chain or agent you've already got. Same engine underneath, different front door.

Install

pip install "langchain-shannonbase[mysql]"

The [mysql] extra pulls in the database driver. Leave it off if you only want the offline in-memory backend for tests.

Quickstart

from langchain_openai import OpenAIEmbeddings
from langchain_shannonbase import ShannonBaseVectorStore

store = ShannonBaseVectorStore(
    embedding=OpenAIEmbeddings(model="text-embedding-3-small"),
    table="documents",
    host="127.0.0.1", port=3306, user="root", password="", database="rag",
)

store.add_texts(
    ["Refunds are accepted within 30 days.", "Free shipping over $50."],
    metadatas=[{"topic": "refunds"}, {"topic": "shipping"}],
    ids=["1", "2"],
)

store.similarity_search("what's the return policy?", k=2)

The table is created on the first write, with an embedding VECTOR(n) column sized to your embedding model.

For a full doc-in to grounded-answer example, see examples/rag.py.

Filtering, MMR, scores, retriever

# restrict a search to matching metadata
store.similarity_search("policy?", k=2, filter={"topic": "refunds"})

# maximal marginal relevance, for hits that aren't near-duplicates of each other
store.max_marginal_relevance_search("policy?", k=3, fetch_k=20, lambda_mult=0.5)

# cosine similarity, or a normalized [0,1] relevance score, with each hit
store.similarity_search_with_score("return policy?", k=2)
store.similarity_search_with_relevance_scores("return policy?", k=2)

# search with an embedding you already have
store.similarity_search_by_vector(my_vector, k=2)

# fetch or delete specific rows by id
store.get_by_ids(["1"])
store.delete(ids=["2"])

# use it as a retriever in any chain
retriever = store.as_retriever(search_kwargs={"k": 3})

How it works

No extensions, just MySQL 9's built-in vector support:

CREATE TABLE documents (
  id VARCHAR(36) PRIMARY KEY,
  content TEXT,
  metadata JSON,
  embedding VECTOR(1536)
);
-- inserts go through STRING_TO_VECTOR('[...]')
-- search:  ORDER BY DISTANCE(embedding, STRING_TO_VECTOR('[...]'), 'COSINE') LIMIT k

Search returns the nearest rows as LangChain Documents, each with a score of 1 - distance. Cosine is the default; pass metric="dot" or metric="euclidean" if you'd rather.

Performance and scale

Search is exact: MySQL 9 (and ShannonBase) run a full DISTANCE scan and return the true nearest neighbours, so recall is always 100%. The tradeoff is that latency grows with the row count, since there's no approximate (HNSW-style) vector index in MySQL 9 or ShannonBase yet. In practice this is fine for thousands to low millions of vectors; past that you'd want an ANN index, which I'll add if and when the backends support one.

Connections are pooled (pool_size defaults to 5, override it in the constructor), so repeated queries reuse connections instead of reconnecting each time.

There's a latency benchmark in bench/benchmark.py if you want numbers for your own instance.

API

Method What it does
add_texts(texts, metadatas, ids) embed and upsert, returns the ids
similarity_search(query, k, filter=...) top-k Documents, optional metadata filter
similarity_search_with_score(query, k) same, with similarity scores
similarity_search_with_relevance_scores(query, k) with normalized [0,1] scores (cosine)
max_marginal_relevance_search(query, k, fetch_k, lambda_mult) diverse results
similarity_search_by_vector(embedding, k) search with a raw vector
get_by_ids(ids) fetch documents by id
delete(ids) remove by id
from_texts(texts, embedding, ...) build a populated store in one call

Metrics: cosine (default), dot, euclidean.

Testing

The logic is unit-tested offline against an in-memory backend, so you don't need a database to run the suite. That's also how the LangChain standard tests run in CI:

pip install -e ".[dev]"
pytest

There's a live round-trip test too, which runs against a real instance when you give it connection details:

export SB_HOST=127.0.0.1 SB_USER=root SB_PASSWORD=... SB_DATABASE=test
pytest tests/test_integration.py

For local development, ShannonBase gives you the MySQL 9 vector features without a HeatWave subscription.

Roadmap

Next on my list:

  • Native async via an async MySQL driver (async already works through LangChain's executor fallback)
  • Relevance scores for the dot and euclidean metrics (cosine is done)
  • Optional vector indexing where the backend supports it
  • Range and comparison operators in filters (only equality today)

Issues and PRs welcome.

Requirements

  • Python 3.9+
  • A MySQL-9-compatible database with the VECTOR type (ShannonBase, MySQL 9, or HeatWave)
  • mysql-connector-python (via the [mysql] extra)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_shannonbase-0.3.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_shannonbase-0.3.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file langchain_shannonbase-0.3.0.tar.gz.

File metadata

  • Download URL: langchain_shannonbase-0.3.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for langchain_shannonbase-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2544cc9b569c2a6fa205ff3d67f826f3ad9fcf6b511c90be5f0c75f24010b81b
MD5 72631823b4b09e4995e3a6af13156120
BLAKE2b-256 2a8dcc9e29707c6cc48a7f1bf23e842a245e1b9d283147b16d88e8a349163f1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_shannonbase-0.3.0.tar.gz:

Publisher: publish.yml on apoorva-01/langchain-shannonbase

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_shannonbase-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_shannonbase-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe3312b6262f59bfe81d9fb5a48b04d8b95e7c411ad99da6e6e7c3d2354284fa
MD5 017297a0ef44ecfda85fed0b46eb7486
BLAKE2b-256 59ae11f9ad73601ed424bd125392ea784319b99516afd2e9ab1c63b2005e6f60

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_shannonbase-0.3.0-py3-none-any.whl:

Publisher: publish.yml on apoorva-01/langchain-shannonbase

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page