<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Devasish Banerjee</title>
    <description>The latest articles on DEV Community by Devasish Banerjee (@devasish_banerjee_b55ceb1).</description>
    <link>https://dev.to/devasish_banerjee_b55ceb1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3844329%2Fa0669568-893c-47f7-88f5-c32ea9d7e250.png</url>
      <title>DEV Community: Devasish Banerjee</title>
      <link>https://dev.to/devasish_banerjee_b55ceb1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devasish_banerjee_b55ceb1"/>
    <language>en</language>
    <item>
      <title>How I Built a Hallucination Detector for RAG Pipelines in Python</title>
      <dc:creator>Devasish Banerjee</dc:creator>
      <pubDate>Thu, 26 Mar 2026 08:20:26 +0000</pubDate>
      <link>https://dev.to/devasish_banerjee_b55ceb1/how-i-built-a-hallucination-detector-for-rag-pipelines-in-python-5ahc</link>
      <guid>https://dev.to/devasish_banerjee_b55ceb1/how-i-built-a-hallucination-detector-for-rag-pipelines-in-python-5ahc</guid>
      <description>&lt;h1&gt;
  
  
  How I Built a Hallucination Detector for RAG Pipelines in Python
&lt;/h1&gt;

&lt;p&gt;Every developer who has shipped a RAG application knows this moment.&lt;/p&gt;

&lt;p&gt;You retrieve the right documents. You pass them to the LLM. The response comes back confident, well-structured, and fluent. You ship it.&lt;/p&gt;

&lt;p&gt;Then a user reports that the LLM cited a statistic that wasn't in any of your documents. Or named a person who doesn't exist. Or described a process that contradicts your source material — while sounding completely authoritative.&lt;/p&gt;

&lt;p&gt;This is hallucination in a RAG pipeline. And it is surprisingly hard to catch systematically.&lt;/p&gt;

&lt;p&gt;I built &lt;strong&gt;HallucinationBench&lt;/strong&gt; to solve this in the simplest way possible.&lt;/p&gt;




&lt;h2&gt;
  
  
  The core idea
&lt;/h2&gt;

&lt;p&gt;The approach is straightforward: use GPT-4o-mini as a structured judge.&lt;/p&gt;

&lt;p&gt;Given a context (your retrieved documents) and a response (the LLM's output), the judge:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Breaks the response into individual factual claims&lt;/li&gt;
&lt;li&gt;Classifies each claim as &lt;strong&gt;grounded&lt;/strong&gt; (supported by context) or &lt;strong&gt;hallucinated&lt;/strong&gt; (absent or contradicted)&lt;/li&gt;
&lt;li&gt;Returns a faithfulness score and a verdict&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it. No embeddings, no vector databases, no infrastructure. Just one API call.&lt;/p&gt;




&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;hallucinationbench&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;

&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
The Eiffel Tower is located in Paris, France. It was constructed between
1887 and 1889 as the entrance arch for the 1889 World&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s Fair.
The tower is 330 metres tall.
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
The Eiffel Tower is in Paris. It was built in 1889 and stands 330 metres
tall. It was designed by Leonardo da Vinci and attracts over 7 million
visitors every year.
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Verdict          : FAIL
Faithfulness     : 0.40

Grounded claims  (2):
  ✓  The Eiffel Tower is in Paris.
  ✓  It stands 330 metres tall.

Hallucinated claims  (3):
  ✗  It was built in 1889.
  ✗  It was designed by Leonardo da Vinci.
  ✗  It attracts over 7 million visitors every year.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two genuine hallucinations caught cleanly — Leonardo da Vinci (it was Gustave Eiffel) and the 7 million visitor figure (not in the context).&lt;/p&gt;




&lt;h2&gt;
  
  
  The result object
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;faithfulness_score&lt;/span&gt;     &lt;span class="c1"&gt;# float 0.0 – 1.0
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grounded_claims&lt;/span&gt;        &lt;span class="c1"&gt;# list of supported statements
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hallucinated_claims&lt;/span&gt;    &lt;span class="c1"&gt;# list of fabricated statements
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;verdict&lt;/span&gt;                &lt;span class="c1"&gt;# "PASS" | "WARN" | "FAIL"
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;                  &lt;span class="c1"&gt;# judge model used
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Verdict&lt;/th&gt;
&lt;th&gt;Faithfulness Score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;✅ PASS&lt;/td&gt;
&lt;td&gt;&amp;gt;= 0.8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;⚠️ WARN&lt;/td&gt;
&lt;td&gt;&amp;gt;= 0.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;❌ FAIL&lt;/td&gt;
&lt;td&gt;&amp;lt; 0.5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  How the judge prompt works
&lt;/h2&gt;

&lt;p&gt;The system prompt instructs GPT-4o-mini to return &lt;strong&gt;raw JSON only&lt;/strong&gt; — no markdown, no explanation, no code fences. Just the structured object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"grounded_claims"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"claim 1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"claim 2"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"hallucinated_claims"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"claim A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"claim B"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"faithfulness_score"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0.75&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two design decisions that matter here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;response_format: json_object&lt;/code&gt;&lt;/strong&gt; — This is OpenAI's native JSON mode. It guarantees the output is valid JSON on every call, eliminating parsing failures almost entirely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;temperature: 0&lt;/code&gt;&lt;/strong&gt; — Hallucination detection should be deterministic. The same context and response should always produce the same verdict. Temperature 0 enforces this.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why GPT-4o-mini as the judge?
&lt;/h2&gt;

&lt;p&gt;Three reasons:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost.&lt;/strong&gt; Each evaluation costs approximately $0.001 — well under a tenth of a cent. You can run thousands of evaluations for a few dollars.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed.&lt;/strong&gt; GPT-4o-mini is fast. A typical evaluation completes in under 2 seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accuracy.&lt;/strong&gt; For claim-level classification against a provided context, GPT-4o-mini performs well. It is not doing open-domain knowledge retrieval — it is comparing claims against text you have already provided. That is a much simpler task.&lt;/p&gt;




&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;openai python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set your OpenAI API key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# .env&lt;/span&gt;
&lt;span class="nv"&gt;OPENAI_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your_key_here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clone and run the Streamlit demo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/bdeva1975/hallucinationbench.git
&lt;span class="nb"&gt;cd &lt;/span&gt;hallucinationbench
pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
streamlit run app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;The roadmap for v0.2.0:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Batch evaluation&lt;/strong&gt; — score multiple context/response pairs in one call&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CSV upload&lt;/strong&gt; in the Streamlit app&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom judge model&lt;/strong&gt; — bring your own OpenAI model&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LangChain and LlamaIndex integration hooks&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CI/CD example&lt;/strong&gt; — run hallucination checks as a GitHub Actions gate before deployment&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The bigger picture
&lt;/h2&gt;

&lt;p&gt;As RAG applications move from prototype to production, hallucination detection is becoming a non-negotiable quality gate — not an optional debugging tool.&lt;/p&gt;

&lt;p&gt;Gartner predicts that by 2028, 60% of software engineering teams will use AI evaluation and observability platforms to build user trust in AI applications, up from just 18% in 2025.&lt;/p&gt;

&lt;p&gt;HallucinationBench is the lightweight, zero-infrastructure entry point to that category. No account, no cloud, no dashboard required. Just pip install and two lines of code.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/bdeva1975/hallucinationbench" rel="noopener noreferrer"&gt;https://github.com/bdeva1975/hallucinationbench&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Star it if you find it useful. PRs and feedback welcome.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Built with Python, OpenAI GPT-4o-mini, and Streamlit.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>rag</category>
      <category>llm</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
