embedding-drift-lib
Detect when your RAG vector index silently drifts from reality.
Pure Python library — no external services, no API keys, no database required.
3 dependencies: numpy, scikit-learn, scipy.
Install
pip install embedding-drift-lib
Quick Start
from embedding_drift_lib import DriftAnalyser, DriftConfig
import numpy as np
config = DriftConfig(index_name="my-rag")
analyser = DriftAnalyser(config)
# Pass your index vectors and recent query vectors
report = analyser.analyse(reference_vectors, current_vectors)
print(report.drift_type) # "none" | "mild" | "significant" | "severe"
print(report.mmd_score) # 0.0 = identical, > 0.20 = severe drift
print(report.primary_cause) # "model_drift" | "query_shift" | "data_staleness"
print(report.recommendation) # human-readable action to take
Wrap Your Embedding Function (Zero-Touch Monitoring)
from embedding_drift_lib import DriftMonitor, DriftConfig
from openai import AsyncOpenAI
client = AsyncOpenAI()
monitor = DriftMonitor(DriftConfig(index_name="my-rag"))
# Create baseline from your existing index (run once)
await monitor.create_snapshot(index_vectors, label="v1-baseline")
# Wrap your embed call — your RAG code does not change
embed = monitor.wrap(client.embeddings.create)
# Use normally — drift analysis runs in the background automatically
response = await embed(model="text-embedding-3-small", input=["user query"])
# Check anytime
report = monitor.last_report
What It Detects
| Drift Type | Signal | Example |
|---|---|---|
| Model drift | High MMD + concentrated Wasserstein | OpenAI silently updates model weights |
| Query shift | High coverage gap + low MMD | Users ask about a new topic your corpus never covered |
| Data staleness | High MMD + diffuse Wasserstein | Corpus ages relative to current user queries |
Configuration
config = DriftConfig(
index_name = "my-rag",
pca_components = 50, # reduce embedding dims before MMD
n_permutations = 200, # permutation test iterations
sample_rate = 0.05, # sample 5% of queries (default)
elevated_sample_rate = 0.30, # 30% when drift suspected
analysis_batch_size = 200, # analyse every N queries
mmd_mild_threshold = 0.02,
mmd_significant_threshold = 0.08,
mmd_severe_threshold = 0.20,
coverage_gap_alert_threshold = 0.15, # alert when 15%+ queries uncovered
snapshot_dir = "./snapshots",
)
How It Works
Your RAG App
|
| query -> embed() -> vector search -> LLM -> answer
v
DriftMonitor wrapper (transparent)
|
+-- Samples 5% of embedding calls (adaptive: 30% when drift suspected)
|
+-- Every 200 samples, runs DriftAnalyser:
| 1. PCA projection 1536-dim -> 50-dim
| 2. MMD^2 primary drift metric
| 3. Wasserstein which semantic directions shifted
| 4. Coverage gap query shift detection
| 5. Permutation test 200 shuffles, p < 0.05
|
+-- Classifies: none / mild / significant / severe
+-- Diagnoses: model_drift / query_shift / data_staleness
+-- Returns DriftReport
The Math
MMD (Maximum Mean Discrepancy) — non-parametric test measuring distance between two distributions without assuming any shape. Uses RBF kernel with median heuristic bandwidth.
Wasserstein distance — computed per PCA dimension. Concentration ratio (max/mean) distinguishes sudden model swap (high concentration) from gradual data staleness (diffuse).
Coverage gap — fraction of queries whose best cosine similarity to any index document falls below threshold. Catches query shift that MMD misses.
Permutation test — 200 label shuffles build a null distribution. Observed MMD must exceed the 95th percentile of the null to be reported as statistically significant.
Author
Sarthak Pande — GitHub
License
MIT #\x00 \x00e\x00m\x00b\x00e\x00d\x00d\x00i\x00n\x00g\x00-\x00d\x00r\x00i\x00f\x00t\x00-\x00l\x00i\x00b\x00 \x00 \x00
Metadata
Release files for embedding-drift-lib 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| embedding_drift_lib-0.1.1.tar.gz | 16.9 kB | With provenance | Details for embedding_drift_lib-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| embedding_drift_lib-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for embedding_drift_lib-0.1.1-py3-none-any.whl |
Total release size: 35.3 kB
Release files / embedding_drift_lib-0.1.1.tar.gz
| Download URL | embedding_drift_lib-0.1.1.tar.gz |
|---|---|
| Size | 16.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a7f99b383f37be39dd00dfbeb123dfdb3a3b9af1fc6448932323dc01b91f9b14
|
|
BLAKE2b-256 checksum How to use checksums |
ffc0346d10732d7124476fbd7b93b11459f18fc56b9f2f4140e1fda3f79fa09a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 31, 2026.
Transparency logRelease files / embedding_drift_lib-0.1.1-py3-none-any.whl
| Download URL | embedding_drift_lib-0.1.1-py3-none-any.whl |
|---|---|
| Size | 18.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8efd6b13d1f381d14ed8ee7ee834bd8fade5df4ed1092068cd9ba8af7ed00780
|
|
BLAKE2b-256 checksum How to use checksums |
c0177d693e451afbaeb961434f96b77661738d8f4609dcbf7219eab3b90740c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 31, 2026.
Transparency logSecurity for embedding-drift-lib 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for embedding_drift_lib-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 31, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.