context-drift-detector-py
Detect topic drift between user intent, retrieved context, and AI answers. A fast, lexical Jaccard-overlap heuristic for "did the model wander off?" -- useful as a cheap first-pass guardrail before reaching for an embedding-based check. Zero runtime dependencies.
Python port of @mukundakatta/context-drift-detector.
Install
pip install context-drift-detector-py
Usage
from context_drift_detector import detect
intent = "What is the capital of France?"
context = ["Paris is the capital of France. It sits on the Seine."]
answer = "Paris is the capital of France."
report = detect(intent, context, answer)
report.drift # False
report.drift_score # 0.0 - 1.0 (higher = more drift)
report.signals # dict of jaccard overlaps
report.signals["answer_to_context"] # 0.0 - 1.0
When drift is real:
report = detect(
intent="What is the capital of France?",
context_chunks=["Paris is the capital of France."],
answer="Cats love tuna and naps.",
)
report.drift # True
report.drift_score # high (e.g. > 0.65)
API
detect(
intent: str,
context_chunks: str | Sequence[str],
answer: str,
*,
threshold: float = 0.65,
min_term_len: int = 3,
) -> DriftReport
DriftReport fields:
| Field | Meaning |
|---|---|
drift |
True iff drift_score > threshold. |
drift_score |
0.0-1.0; weighted blend of answer-to-context (60%) and answer-to-intent (40%) overlap, inverted. |
signals.intent_to_context |
Jaccard overlap between intent and retrieved context. |
signals.answer_to_context |
Jaccard overlap between answer and retrieved context. |
signals.answer_to_intent |
Jaccard overlap between answer and intent. |
intent_terms / context_terms / answer_terms |
Frozensets of the terms used. |
detect_context_drift(...) is exported as a JS-aligned alias.
How it works
Tokenizes each input into the lowercase set of min_term_len-char alphanumeric runs, then computes pairwise Jaccard overlaps. Empty inputs short-circuit to drift-free; this is intentional so a totally absent retrieval doesn't get flagged as drift on its own.
This is a cheap heuristic -- it doesn't catch paraphrases, synonyms, or semantically grounded contradictions. Use it as a fast first filter, then invest in an embedding/LLM-as-judge check for borderline cases.
See the JS sibling's README for the full design notes.
Metadata
Release files for context-drift-detector-py 0.1.0
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 |
|---|---|---|---|---|
| context_drift_detector_py-0.1.0.tar.gz | 6.1 kB | Without provenance | Details for context_drift_detector_py-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| context_drift_detector_py-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for context_drift_detector_py-0.1.0-py3-none-any.whl |
Total release size: 12.0 kB
Release files / context_drift_detector_py-0.1.0.tar.gz
| Download URL | context_drift_detector_py-0.1.0.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c60f75264a85c2cd63e5838f748724ff449bd6ca1506d558ab53873594ada457
|
|
BLAKE2b-256 checksum How to use checksums |
d3fd31af18eeae15cea83d0d694c97cdd7c04afe4f676839b66808668be51403
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / context_drift_detector_py-0.1.0-py3-none-any.whl
| Download URL | context_drift_detector_py-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b2dbfc71d5c88626ecfc5d5f2421208f895631d96dcf5b4abcdc5b599bfd284e
|
|
BLAKE2b-256 checksum How to use checksums |
6da640fd3f36e21f124edc6d44cf6f94c8ecb9b6be78315d110602f94c194dfe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Security for context-drift-detector-py 0.1.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.