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

A LangChain VectorStore for MySQL 9's native VECTOR type — so you can do RAG on a database you already run.

Works with ShannonBase (the open-source MySQL-for-AI), self-hosted MySQL 9, and MySQL HeatWave — they all share the same VECTOR / STRING_TO_VECTOR / DISTANCE surface.

Why this exists

If your data already lives in MySQL, your options for LangChain vector search were thin: the only MySQL VectorStore is locked to Google Cloud SQL, and ShannonBase's LangChain integration was on its wishlist but unbuilt. This fills that gap — a plain, self-hostable adapter that plugs MySQL 9 into the LangChain ecosystem, no separate vector database required.

Install

pip install "langchain-shannonbase[mysql]"

Use

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"}],
)

# It's a normal LangChain vector store — use it directly or as a retriever:
docs = store.similarity_search("return policy?", k=2)
retriever = store.as_retriever(search_kwargs={"k": 3})

Because it implements LangChain's VectorStore interface, it drops into any LangChain chain, retriever, or RAG pipeline unchanged.

How it works

Under the hood it uses MySQL 9's native vector features — no extensions:

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')

Similarity search reads back the nearest rows by cosine distance and returns them as LangChain Documents with a score (1 - distance).

API

Method Does
add_texts(texts, metadatas, ids) embed + upsert, returns ids
similarity_search(query, k) top-k Documents
similarity_search_with_score(query, k) with cosine similarity scores
similarity_search_by_vector(embedding, k) search with a raw vector
delete(ids) remove by id
from_texts(texts, embedding, ...) build a store in one call
metric= "cosine" (default), "dot", "euclidean"

Testing

The core logic is unit-tested offline via an in-memory backend (no database needed — pytest). A live round-trip test runs against a real instance when you set the connection env vars:

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

Local dev tip: run ShannonBase to get MySQL-9 vector features without a HeatWave subscription.

Requirements

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

License

MIT © Apoorva Verma

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.1.0.tar.gz (9.4 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.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langchain_shannonbase-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 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.1.0.tar.gz
Algorithm Hash digest
SHA256 10252b69d7d0116f1187593607918c57a20a36f4c522c897dffce7c85603e0bf
MD5 77262122dda5b8ce637244b9a6cf7a0a
BLAKE2b-256 ff556372a3e3e087028e9712c0088af5f75abf4f6b13879a4ddd3ca7ddb1d17b

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_shannonbase-0.1.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.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_shannonbase-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3cceb42fa3bcc44db910d84677ff9e55b919602dfc699dbbbc4115e72bb3e2da
MD5 13212ecd768798f4c4f9e420cbe7e549
BLAKE2b-256 84bda94497849494de9f0721026fa3533b8a343cc6f075efaf5399e011e666e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_shannonbase-0.1.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