Skip to main content

hallucination-gate

PyPI Python License: MIT CI

RAG evaluation + conservative release gate for RAG and fine-tuned LLMs.

  • Eval: RAGAS-class metrics (faithfulness, answer_relevancy, context_precision, context_recall) on claim↔chunk grounding — not whole-answer NLI only.
  • Gate: pass / rewrite / abstain so production only ships supported text.

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

evaler = RAGEval()  # neural; use_heuristic=True for CI
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.",
            # optional labeled retrieval:
            # "relevant_contexts": ["The Titan watch has a 2-year warranty covering defects."],
        }
    ]
)
print(report.aggregate)
# {'faithfulness': ..., 'answer_relevancy': ..., 'context_precision': ..., ...}
report.to_json("report.json")
hallucination-gate eval-dataset samples.jsonl --out report.json
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
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, 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()
result = gate.check(query, answer, context=retrieved_docs)
return result.text
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 gateuse_heuristic=True is for CI smoke, not calibrated faithfulness.
  • Ops surface — HF downloads, torch/sentence-transformers weight, Windows symlink quirks.
  • Not magic — still not a full substitute for domain-labeled regression + human review; it is a stronger grounding-first eval+gate stack than score-only RAGAS defaults.

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

hallucination_gate-0.7.0.tar.gz (60.2 kB view details)

Uploaded Source

Built Distribution

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

hallucination_gate-0.7.0-py3-none-any.whl (66.8 kB view details)

Uploaded Python 3

File details

Details for the file hallucination_gate-0.7.0.tar.gz.

File metadata

  • Download URL: hallucination_gate-0.7.0.tar.gz
  • Upload date:
  • Size: 60.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for hallucination_gate-0.7.0.tar.gz
Algorithm Hash digest
SHA256 d1b07a90212b0597b0f2d5093967cbb61adb0d0f1a8f3162da7199cda1af27f0
MD5 bcaa739c65669a34059c9629acea06e4
BLAKE2b-256 847b5030133dce3e21ca41bbf6991415b749f85e7444fbaf233e39355f815c40

See more details on using hashes here.

Provenance

The following attestation bundles were made for hallucination_gate-0.7.0.tar.gz:

Publisher: publish.yml on shrey315/hallucination-gate

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

File details

Details for the file hallucination_gate-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hallucination_gate-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0a18c25abbf6e918e901f2754c33fc78a0fc9c3d4b409fd060919918d44ae61
MD5 935c5065bc04827e8e8038371f38f485
BLAKE2b-256 12fe611d4ad3b7a13903af0cbd9fd340912536e540192331d32a5ff059a28b59

See more details on using hashes here.

Provenance

The following attestation bundles were made for hallucination_gate-0.7.0-py3-none-any.whl:

Publisher: publish.yml on shrey315/hallucination-gate

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

Release history Release notifications | RSS feed

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.0

2 files

This release

0.7.0 This release

2 files

0.6.5

2 files

0.6.4

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

Supported by

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