Skip to main content

hallucination-gate

PyPI Python License: MIT CI

Conservative grounding gate for RAG and fine-tuned LLMs.
Verify answers against your evidence → pass, rewrite, or abstain.

False release is the failure mode that matters. Neighbor chunks no longer veto a claim another chunk fully supports.

Install

pip install -U hallucination-gate
pip install "hallucination-gate[ocr]"   # Tesseract / EasyOCR / scanned PDFs

Quick start

from hallucination_gate import HallucinationGate, Evidence

gate = HallucinationGate()  # neural default (production)
# gate = HallucinationGate(use_heuristic=True)  # CI / offline smoke only

result = gate.check(
    query=user_query,
    answer=llm_answer,
    context=retrieved_docs,  # str | list[str] | LangChain Document | dict
)
return result.text  # show this to users
gate = HallucinationGate(mode="fine_tuned")
result = gate.check(query, answer, kb=your_knowledge_base)

# Images / PDFs / OCR
result = gate.check(query, answer, evidence=Evidence.from_image(path="warranty_card.jpg"))
result = gate.check(query, answer, evidence=Evidence.from_pdf("policy.pdf"))
result = gate.check(query, answer, evidence=Evidence.from_ocr(path="scanned.pdf"))
@gate.protect
def my_rag(query: str):
    docs = retriever(query)
    answer = llm(query, docs)
    return answer, docs

Inspect result.claims / result.diagnostics for claim↔chunk status, citations, and reasons.

OCR

pip install "hallucination-gate[ocr]"
# system: Tesseract binary (+ poppler for scanned PDFs)
from hallucination_gate import Evidence, ocr_available

print(ocr_available())  # pillow / tesseract / easyocr

ev = Evidence.from_image(path="card.jpg")          # auto-OCR + preprocess
ev = Evidence.from_ocr(path="scanned_policy.pdf")  # page OCR fallback

Upscale → contrast → denoise, then Tesseract and/or EasyOCR. Image-only PDFs OCR when text extract is empty.

Evidence patterns

Pattern When
Full retriever top-k Default. Per-chunk soft-OR handles mixed neighbors.
Reranked / answer-aligned top-k Best production default.
Citation-level chunks If the generator emits citations, pass only those.
Top-1 only Demos; too brittle for real retrieval.

Pass a list[str] (or documents)—do not concatenate top-k into one bag.

Heuristic vs neural

Mode How Use for
Neural (default) MiniLM + DeBERTa NLI Production
Heuristic Token / negation / number heuristics CI smoke (use_heuristic=True or RAG_EVAL_HEURISTIC=1)

Heuristic is a wiring check, not a calibrated quality gate.

Optional judge on uncertain claims only: HALLUCINATION_GATE_JUDGE=1 + ANTHROPIC_API_KEY or OPENAI_API_KEY.

What this is (and is not)

It does It does not
Ground claims in your chunks / KB / OCR / PDF Know if the KB itself is wrong
Abstain on contradiction, invented entities, number clashes Read fine-tune weights
Drop ungrounded sentences; abstain if the rest misses the query Replace LLM-as-judge on subtle code/math proofs

Release is decided by claim grounding. BN scores are diagnostics only.

Eval

pip install -e ".[dev]"
set RAG_EVAL_HEURISTIC=1
pytest -q -m "not neural"
hallucination-gate eval-heldout

HTTP API (optional)

Library users do not need a server.

uvicorn bayesian_rag_evaluator.api.main:app --reload --port 8000

POST /v1/answer{safe_answer, released, request_id, latency_ms} only.

License

MIT

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.6.4.tar.gz (56.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.6.4-py3-none-any.whl (61.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hallucination_gate-0.6.4.tar.gz
  • Upload date:
  • Size: 56.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.6.4.tar.gz
Algorithm Hash digest
SHA256 bf4c8c04a2fd9a3088c9d484ff0b062eb3bca26babf59e03ec8dda1bde90d23f
MD5 8e4ea5ff23ed860371b4e953eda1104e
BLAKE2b-256 f2a29fbdc49869ad26c60d56c7b17f723a4eec1d9e1419d9da2ae6a903b31121

See more details on using hashes here.

Provenance

The following attestation bundles were made for hallucination_gate-0.6.4.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.6.4-py3-none-any.whl.

File metadata

File hashes

Hashes for hallucination_gate-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 03cde8c52b6b8432cd8e79c00d69f900f18b67fd8c18abed4c7e617cbe106b97
MD5 c47f86dbed2b8001e8b28dd63f3be570
BLAKE2b-256 0e30266f89a04ab3d5388f628d3dca05878001d9a2cdff2fb2a0c6657f6ea707

See more details on using hashes here.

Provenance

The following attestation bundles were made for hallucination_gate-0.6.4-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

0.7.0

2 files

0.6.5

2 files

This release

0.6.4 This release

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