Skip to main content

hallucination-gate

A conservative grounding gate for RAG and fine-tuned generators. It does not decide whether an answer is true in the world. It decides whether the answer is supported by the evidence you pass in, then passes, rewrites, or abstains.

False release is treated as the failure mode that matters. If a rewrite would no longer answer the question, the gate abstains.

from hallucination_gate import HallucinationGate, Evidence

gate = HallucinationGate()  # or mode="fine_tuned"

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

result = gate.check(query, answer, evidence=Evidence.from_image(path="photo.jpg", ocr="..."))
result = gate.check(query, answer, evidence=Evidence.from_pdf("policy.pdf"))
@gate.protect
def my_rag(query: str):
    docs = retriever(query)
    answer = llm(query, docs)
    return answer, docs

What this is (and is not)

It does It does not
Check claims against your retrieved chunks / KB / OCR / PDF text Know if the KB itself is wrong
Abstain on contradiction, invented entities, and number clashes Read fine-tune weights
Drop ungrounded sentences, then abstain if the remainder misses the query Replace an LLM-as-judge on subtle reasoning, code, or math proofs
Work with any stack that can give you query, answer, evidence Guarantee multilingual performance equal to English without swapping models

Default neural backends: multilingual MiniLM + DeBERTa NLI. Override with embed_model=, nli_model=, or RAG_EVAL_EMBED_MODEL / RAG_EVAL_NLI_MODEL.

Release is decided by claim grounding, not by the Bayesian network. BN scores are diagnostics only.

Set RAG_EVAL_HEURISTIC=1 for CI / offline (token coverage, no model download). Optional: HALLUCINATION_GATE_JUDGE=1 plus ANTHROPIC_API_KEY or OPENAI_API_KEY to escalate uncertain claims only.

Eval

Held-out domains (HR, API, vaccines, Redis, K8s) report false release and over-refusal:

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

Install

pip install hallucination-gate

From GitHub:

pip install git+https://github.com/shrey315/hallucination-gate.git

From this folder:

pip install -e ".[dev]"
pytest -q

HTTP API

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

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

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.0.tar.gz (48.6 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.0-py3-none-any.whl (55.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hallucination_gate-0.6.0.tar.gz
  • Upload date:
  • Size: 48.6 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.0.tar.gz
Algorithm Hash digest
SHA256 e4bc6b0a86f63503f2fc089d5bc04948c9af0d4a31bb923a94c82db95881b80f
MD5 2dacd35131cf3bdc871b993d4cad41fc
BLAKE2b-256 f2cf92e1cbbf92ae0a15808b134faa2f1219691b37ddc805d3eed3e004d70d4b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for hallucination_gate-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5ab02e2cf7369a487e6fa4d3f1ea306a2d8665428dff1e0181a5afcd47f6862
MD5 1e70dd56f32aa3685b7bc6c060476db9
BLAKE2b-256 ca6bb2a060433177e388fadab915683832f6fa473ad319297dd9f4e1075a3272

See more details on using hashes here.

Provenance

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

0.7.0

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

This release

0.6.0 This release

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