hallucination-gate
RAG quality system + conservative release gate for RAG and fine-tuned LLMs.
- Eval: claim-level faithfulness / relevancy / context metrics
- Retrieval: hit@k, recall@k, MRR, nDCG@k
- Regression: save baseline → diff → fail CI
- Latency budget: p50 / p95 / p99 / max ceilings
- Modes:
ci(heuristic smoke) vsquality(neural); policiesstrict/balanced - Gate: pass / rewrite / abstain for production
safe_answer
Author: Shreyas G.
Install
pip install -U hallucination-gate
pip install "hallucination-gate[ocr]" # optional OCR
RAG eval (RAGAS replacement path)
from hallucination_gate import RAGEval, LatencyBudget
evaler = RAGEval(
use_heuristic=True, # CI; omit for neural production eval
latency_budget=LatencyBudget(p95_ms=1500, max_ms=5000),
)
report = evaler.evaluate(
[
{
"query": "What is the warranty?",
"answer": "The Titan watch has a 2-year warranty.",
"contexts": [
"The Titan watch has a 2-year warranty covering defects.",
"Shipping takes 3-5 days.",
],
"ground_truth": "2-year warranty for manufacturing defects.",
"relevant_contexts": [
"The Titan watch has a 2-year warranty covering defects."
],
# or: "relevant_indices": [0],
}
],
save_baseline_path="baselines/titan.json",
# baseline_path="baselines/titan.json",
# fail_on_regression=True,
)
print(report.aggregate) # faithfulness, answer_relevancy, ...
print(report.retrieval) # hit_at_k, mrr, ndcg_at_k, ...
print(report.latency) # p50/p95/p99 + budget ok
report.raise_if_failed()
hallucination-gate eval-dataset samples.jsonl --out report.json \
--save-baseline baselines/titan.json --p95-ms 1500
# later in CI:
hallucination-gate eval-dataset samples.jsonl \
--baseline baselines/titan.json --fail-on-regression --p95-ms 1500
| Metric | How this package scores it |
|---|---|
| faithfulness | Fraction of answer claims supported by individual chunks (contradictions penalize) |
| answer_relevancy | Query↔answer embedding relevance |
| context_precision | Labeled relevant_contexts if provided; else claim-aligned chunk proxy |
| context_recall | Requires ground_truth — fraction of reference facts covered by contexts |
| hit@k / MRR / nDCG | Ranked retrieval vs relevant_contexts or relevant_indices |
| latency budget | p50/p95/p99/max vs LatencyBudget |
| regression | Diff aggregates/retrieval/latency vs saved baseline; fail CI on drops |
| groundedness / hallucination_risk / release_safety | BN posteriors from the same evidence stack |
Why this beats typical RAGAS setups for grounding: claim-level soft-OR against neighbors, retrieval+latency+regression in one report, false-release oriented gate, multimodal/OCR evidence, and a production safe_answer path — not only a mean score.
Production gate
from hallucination_gate import HallucinationGate, Evidence
gate = HallucinationGate(
quality_mode="quality", # or "ci" for heuristic smoke only
policy="balanced", # or "strict" for max false-release lock
warm=True, # preload models — cuts cold-start tails
)
result = gate.check(query, answer, context=retrieved_docs)
return result.text
Context chunks are aligned/filtered to the query+answer by default (generic overlap/embedding score — no domain lexicon). Metrics expose both context_precision_labeled and context_precision_aligned.
report = gate.evaluate(samples) # same backends as the gate
OCR
from hallucination_gate import Evidence, ocr_available
ev = Evidence.from_image(path="warranty_card.jpg")
ev = Evidence.from_ocr(path="scanned_policy.pdf")
Drawbacks (honest)
- Latency & cost — neural path adds inference time / GPU·CPU load per sample.
- Over-refusal — conservative gate can abstain on good extractive answers.
- Only as good as evidence — checks support, not world truth; bad retrieval still hurts.
- Hard cases — subtle math/code/reasoning can fool or over-block NLI.
- Heuristic ≠ quality gate —
use_heuristic=Trueis for CI smoke, not calibrated faithfulness. - Ops surface — HF downloads, torch/sentence-transformers weight, Windows symlink quirks.
- Not magic — still needs your domain labels (
relevant_contexts/ground_truth) and human review for hard cases; the stack now covers grounding + retrieval + latency SLOs + regression diffs.
Eval (gate safety)
pip install -e ".[dev]"
set RAG_EVAL_HEURISTIC=1
pytest -q -m "not neural"
hallucination-gate eval-heldout
License
MIT © Shreyas G
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hallucination_gate-0.9.0.tar.gz.
File metadata
- Download URL: hallucination_gate-0.9.0.tar.gz
- Upload date:
- Size: 69.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c918748ba391b840921a470166a6690b2b331658aea1c5eed1b38be7f7f37fe
|
|
| MD5 |
017c313dc4f515a87e2416e91509385c
|
|
| BLAKE2b-256 |
fdbc8c9f41bbd8b51b94e522bd04fa9ac127f1fca9d89b781e0d50c56eb5d133
|
Provenance
The following attestation bundles were made for hallucination_gate-0.9.0.tar.gz:
Publisher:
publish.yml on shrey315/hallucination-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hallucination_gate-0.9.0.tar.gz -
Subject digest:
1c918748ba391b840921a470166a6690b2b331658aea1c5eed1b38be7f7f37fe - Sigstore transparency entry: 2484344568
- Sigstore integration time:
-
Permalink:
shrey315/hallucination-gate@2bb18a60a49564d7c678795fad7bc4bc3112149e -
Branch / Tag:
refs/tags/v0.9.0 - Owner: https://github.com/shrey315
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2bb18a60a49564d7c678795fad7bc4bc3112149e -
Trigger Event:
release
-
Statement type:
File details
Details for the file hallucination_gate-0.9.0-py3-none-any.whl.
File metadata
- Download URL: hallucination_gate-0.9.0-py3-none-any.whl
- Upload date:
- Size: 77.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbf544c1f864783f8ad802aa9a8dfb1af2835f832ff24e3b347b55cc65024d3d
|
|
| MD5 |
0f862d1372e5b2a3639d4a630761a2bd
|
|
| BLAKE2b-256 |
8badd792ff5328ca5b0c5dd48d56b5eea0840cbb6b80489be3b96449428b5dcc
|
Provenance
The following attestation bundles were made for hallucination_gate-0.9.0-py3-none-any.whl:
Publisher:
publish.yml on shrey315/hallucination-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hallucination_gate-0.9.0-py3-none-any.whl -
Subject digest:
cbf544c1f864783f8ad802aa9a8dfb1af2835f832ff24e3b347b55cc65024d3d - Sigstore transparency entry: 2484344572
- Sigstore integration time:
-
Permalink:
shrey315/hallucination-gate@2bb18a60a49564d7c678795fad7bc4bc3112149e -
Branch / Tag:
refs/tags/v0.9.0 - Owner: https://github.com/shrey315
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2bb18a60a49564d7c678795fad7bc4bc3112149e -
Trigger Event:
release
-
Statement type: