<?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: Muhammad Ikram UL Mustafa</title>
    <description>The latest articles on DEV Community by Muhammad Ikram UL Mustafa (@ikramulmustafa).</description>
    <link>https://dev.to/ikramulmustafa</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4172591%2F9419e530-fb69-406b-bfaf-9581347fe0ed.png</url>
      <title>DEV Community: Muhammad Ikram UL Mustafa</title>
      <link>https://dev.to/ikramulmustafa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ikramulmustafa"/>
    <language>en</language>
    <item>
      <title>Your Linter Passes. The Exception Handler Is Lying.</title>
      <dc:creator>Muhammad Ikram UL Mustafa</dc:creator>
      <pubDate>Thu, 19 Nov 2026 20:25:00 +0000</pubDate>
      <link>https://dev.to/ikramulmustafa/your-linter-passes-the-exception-handler-is-lying-4j8f</link>
      <guid>https://dev.to/ikramulmustafa/your-linter-passes-the-exception-handler-is-lying-4j8f</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;verify_password&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;hash_password&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password_hash&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the handler again. If anything inside the &lt;code&gt;try&lt;/code&gt; raises, the function returns &lt;code&gt;True&lt;/code&gt;. A database timeout. A &lt;code&gt;None&lt;/code&gt; where a user object should be. A bug in the hashing library after an upgrade. Every one of those lets the user in.&lt;/p&gt;

&lt;p&gt;Nobody writes this on purpose. It gets written while handling something else, it passes review because it looks like error handling, and it sits there until the day the hashing call starts failing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nothing in the toolchain objects
&lt;/h2&gt;

&lt;p&gt;Run the tests. They pass. The tests check that the right password returns &lt;code&gt;True&lt;/code&gt; and the wrong one returns &lt;code&gt;False&lt;/code&gt;, and neither path raises, so the handler never runs.&lt;/p&gt;

&lt;p&gt;Run the linter. The default rule sets in flake8 and Ruff say nothing. Pylint flags the broad &lt;code&gt;except Exception&lt;/code&gt;, but it flags it the same way whether the handler returns &lt;code&gt;True&lt;/code&gt; or logs and carries on, so the warning tells you nothing about which handler is dangerous. A warning that fires on every broad handler is one people learn to ignore.&lt;/p&gt;

&lt;p&gt;Run the type checker. The function is declared to return &lt;code&gt;bool&lt;/code&gt;. It returns &lt;code&gt;bool&lt;/code&gt;. That is a pass.&lt;/p&gt;

&lt;p&gt;This is the problem with swallowed exceptions. Static analysis is good at things that are visibly wrong. A swallowed exception is not visibly wrong. The code runs, returns a plausible value, and logs nothing. You find out weeks later, from a number that does not reconcile or a login that should not have worked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it matters more now
&lt;/h2&gt;

&lt;p&gt;This bug has always existed. What changed is the rate at which code arrives.&lt;/p&gt;

&lt;p&gt;A lot of code is now generated faster than anyone reads it. Ask a model to "handle the error" in a function and you will reliably get a handler that makes the error go away, not one that surfaces it. It catches &lt;code&gt;Exception&lt;/code&gt;, returns a sensible default, and the function now "never crashes". That reads as an improvement in a diff. It is the opposite.&lt;/p&gt;

&lt;p&gt;Review does not scale to catch this, because the pattern looks fine at a glance. So I wanted the codebase itself to refuse it. That is &lt;a href="https://github.com/ikramulmustafa/silentcatch" rel="noopener noreferrer"&gt;silentcatch&lt;/a&gt;: a small static check that fails the build when a pull request adds an exception handler that swallows the failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it decides a handler is swallowed
&lt;/h2&gt;

&lt;p&gt;The question it asks of every handler is narrow: is there any evidence in the body that this failure was recorded? A &lt;code&gt;raise&lt;/code&gt; counts. A logging call counts. A call to an error reporter like Sentry counts. If none of those is present, the exception is being swallowed.&lt;/p&gt;

&lt;p&gt;It parses each file with Python's &lt;code&gt;ast&lt;/code&gt; module, walks every &lt;code&gt;ExceptHandler&lt;/code&gt; node, and runs a small visitor over the handler body. This is the core of it, from &lt;code&gt;detector.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;_HandlerInspector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NodeVisitor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Walks a handler body looking for evidence the failure was not swallowed."""&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reraises&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;raises_new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns_or_assigns_fallback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;has_other_statements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;visit_Raise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Raise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reraises&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;raises_new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generic_visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;visit_Call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;
        &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Attribute&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;LOG_METHODS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;receiver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_root_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;receiver&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;receiver&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;LOG_RECEIVERS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;REPORTING_CALLS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;REPORTING_CALLS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generic_visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;visit_Return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Return&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns_or_assigns_fallback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generic_visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;LOG_METHODS&lt;/code&gt; is the usual set: &lt;code&gt;debug&lt;/code&gt;, &lt;code&gt;info&lt;/code&gt;, &lt;code&gt;warning&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt;, &lt;code&gt;exception&lt;/code&gt;, &lt;code&gt;critical&lt;/code&gt; and a few more. &lt;code&gt;REPORTING_CALLS&lt;/code&gt; covers things like &lt;code&gt;capture_exception&lt;/code&gt;, &lt;code&gt;report&lt;/code&gt;, &lt;code&gt;notify&lt;/code&gt; and &lt;code&gt;print_exc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The receiver check matters more than it looks. Matching on the method name alone would treat &lt;code&gt;response.error()&lt;/code&gt; as logging. So a call only counts as a log if the object it is called on looks like a logger: &lt;code&gt;logger&lt;/code&gt;, &lt;code&gt;log&lt;/code&gt;, &lt;code&gt;logging&lt;/code&gt;, &lt;code&gt;self&lt;/code&gt;, &lt;code&gt;app&lt;/code&gt; and similar. There is a test for exactly that case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_non_logger_error_method_does_not_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nf"&gt;verdicts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"""
try:
    send()
except Exception:
    response.error()
"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Verdict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SILENT&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the visitor has run, the verdict is a short chain of decisions (trimmed slightly):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;inspector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reraises&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;inspector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;raises_new&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;

&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_exception_label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;broad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_is_bare_or_broad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;inspector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Logged but not re-raised. Only worth flagging on broad handlers,&lt;/span&gt;
    &lt;span class="c1"&gt;# where the code continues past an error it does not understand.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;broad&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;findings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Finding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verdict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;Verdict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;LOGGED_ONLY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;_body_is_only_pass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;detail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"the handler does nothing at all"&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;inspector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns_or_assigns_fallback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;detail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"the handler returns a value instead of surfacing the failure"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;detail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"nothing in the handler records or re-raises the error"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gives three outcomes. A handler that re-raises, bare or chained, is fine. A handler that records the error is fine, unless it is also broad, in which case it is a warning. Everything else is a failure, with a message that says which kind of swallow it is.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;verify_password&lt;/code&gt; above fails. These do not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bad input"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# narrow and logged: a considered decision&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;risky&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"failed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;   &lt;span class="c1"&gt;# chained re-raise&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;capture_exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# reported (broad, so this one is a warning)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running it on the demo file in the repo, which has two swallowed handlers, one logged-only handler, one chained re-raise and one suppressed handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ silentcatch demo/payments.py
FAIL  demo/payments.py:19  `Exception` is swallowed: the handler returns a value instead of surfacing the failure.
FAIL  demo/payments.py:27  `Exception` is swallowed: the handler returns a value instead of surfacing the failure.
WARN  demo/payments.py:35  `Exception` is logged but not re-raised. Execution continues past an error the code did not specifically anticipate.

silentcatch: 2 swallowed, 1 logged-only.
Add `# silentcatch: allow` above a handler that is deliberate.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exit code 1. The re-raise and the suppressed handler do not appear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design decisions and what they cost
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A broad handler that logs is a warning, not a failure
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;except Exception&lt;/code&gt; followed by &lt;code&gt;logger.error(...)&lt;/code&gt; is sometimes exactly right. A request boundary in a web app should catch everything, log it, and return a 500 rather than kill the worker. A task runner should log a failed task and move to the next one.&lt;/p&gt;

&lt;p&gt;It is also sometimes wrong, when the code keeps going past an error it did not anticipate and later steps run on bad state. The tool cannot tell those two apart from the syntax. If I made it a failure, every web app would fail on its error middleware on day one. So it is a warning by default, and &lt;code&gt;--strict&lt;/code&gt; turns warnings into failures for teams that want that.&lt;/p&gt;

&lt;p&gt;The cost: a broad handler that logs and then returns a wrong value passes the default check. The log line is there, so someone could find the failure. Nobody is forced to.&lt;/p&gt;

&lt;h3&gt;
  
  
  Diff mode, or the check gets turned off
&lt;/h3&gt;

&lt;p&gt;Run any new check on an existing codebase and you get the backlog. On a few years of Python that can be hundreds of handlers. Nobody fixes hundreds of handlers in the PR that adds a linter. What happens instead is the check gets marked as non-blocking, then ignored, then removed. Nothing improves.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--base origin/main&lt;/code&gt; reports only findings on lines the pull request touched. It asks git which Python files changed, then runs &lt;code&gt;git diff -U0&lt;/code&gt; per file and reads the hunk headers to build the set of changed line numbers. A finding is kept only if its &lt;code&gt;except&lt;/code&gt; line is in that set.&lt;/p&gt;

&lt;p&gt;That holds new code to the standard and leaves the backlog alone. The old handlers are still there. But the number stops growing, which is more than most cleanup efforts manage.&lt;/p&gt;

&lt;p&gt;Two costs. It keys on the &lt;code&gt;except&lt;/code&gt; line, so if a PR edits the body of an old handler without touching the &lt;code&gt;except&lt;/code&gt; line, it is not reported. And if git is not available it prints a notice and falls back to scanning everything, which is the noisy behaviour diff mode exists to avoid. In CI you need &lt;code&gt;fetch-depth: 0&lt;/code&gt; on checkout so the base ref exists.&lt;/p&gt;

&lt;h3&gt;
  
  
  Suppression is a comment, not a config file
&lt;/h3&gt;

&lt;p&gt;Some handlers genuinely should swallow. Optional telemetry. Best-effort cache warming. Cleanup that must not mask the original error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;telemetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# silentcatch: allow&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comment goes on the &lt;code&gt;except&lt;/code&gt; line or the line above it. I chose that over an ignore list in &lt;code&gt;pyproject.toml&lt;/code&gt; for three reasons.&lt;/p&gt;

&lt;p&gt;The decision sits next to the code it is about. Anyone reading the handler sees that it was deliberate.&lt;/p&gt;

&lt;p&gt;It shows up in the diff. A reviewer approving a new &lt;code&gt;# silentcatch: allow&lt;/code&gt; is approving that specific handler, not a path glob that will quietly cover future code too.&lt;/p&gt;

&lt;p&gt;It moves with the code. An ignore list keyed by file and line breaks as soon as the file is edited above the handler. A comment does not.&lt;/p&gt;

&lt;p&gt;The cost is that it is easy to type. A comment that says "allow" with no reason is not much better than no check. I put a short reason after it in my own code, and I would ask for one in review.&lt;/p&gt;

&lt;h2&gt;
  
  
  It flagged its own source
&lt;/h2&gt;

&lt;p&gt;The first time I ran silentcatch on itself, it reported five handlers. All five were swallowing by design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;analyse_source&lt;/code&gt; catches &lt;code&gt;SyntaxError&lt;/code&gt; and returns an empty list. A file that does not parse is not this tool's problem. The interpreter will complain about it soon enough.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;analyse_file&lt;/code&gt; catches &lt;code&gt;OSError&lt;/code&gt; and &lt;code&gt;UnicodeDecodeError&lt;/code&gt; and returns an empty list. An unreadable file is skipped.&lt;/li&gt;
&lt;li&gt;Two functions in &lt;code&gt;cli.py&lt;/code&gt; catch the errors from running &lt;code&gt;git&lt;/code&gt; and return &lt;code&gt;None&lt;/code&gt;. Git being missing is an expected condition. The caller checks for &lt;code&gt;None&lt;/code&gt; and falls back to a full scan.&lt;/li&gt;
&lt;li&gt;The hunk header parser catches &lt;code&gt;ValueError&lt;/code&gt; and &lt;code&gt;IndexError&lt;/code&gt; on a malformed &lt;code&gt;@@&lt;/code&gt; line and skips that hunk.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every one of them returns a fallback without logging. By the tool's own rule, every one of them is a swallow. The rule was right about what the code does. It just could not know that I meant it.&lt;/p&gt;

&lt;p&gt;I had two options. Loosen the rule, for example by treating &lt;code&gt;return None&lt;/code&gt; or &lt;code&gt;return []&lt;/code&gt; as acceptable. Or keep the rule and mark the five handlers. I marked them, each with a short reason after the marker: "unparseable files are not our concern", "unreadable files are skipped by design", "git absence is an expected condition, caller handles None", "malformed hunk header, skip this hunk".&lt;/p&gt;

&lt;p&gt;Loosening the rule would have exempted the exact pattern that causes real damage. A lookup that fails and returns &lt;code&gt;None&lt;/code&gt; is the classic version of this bug. The repo's CI now runs silentcatch on its own source on every push, so a sixth unmarked handler would fail the build.&lt;/p&gt;

&lt;p&gt;The honest reading of that first run: on a codebase of about 360 lines, written by someone who knew the rule, the false positive rate was five out of five. Most handlers in a small, careful tool are deliberate. On a larger codebase with a more typical mix you will see real catches alongside them, but you will see false positives. The suppression comment is how you pay for them, once per handler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;p&gt;Worth knowing before you adopt it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Python only.&lt;/strong&gt; The approach ports to other languages. This implementation does not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logger detection is a heuristic.&lt;/strong&gt; It matches method names against plausible receivers. A custom wrapper like &lt;code&gt;audit.record_failure(e)&lt;/code&gt; is not recognised, and you get a false positive. It also cuts the other way: &lt;code&gt;self.error(...)&lt;/code&gt; counts as logging because &lt;code&gt;self&lt;/code&gt; is an accepted receiver, even if that method does not log.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It cannot infer intent.&lt;/strong&gt; A handler that returns a safe default because that really is correct looks identical to one hiding a bug. That is why the suppression comment exists instead of a cleverer rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single-file analysis.&lt;/strong&gt; If your handler calls a helper that logs, that is not seen. Neither is anything that crosses a module boundary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The thing to watch is the false positive rate. A check that fires on correct code gets disabled, and a disabled check is worth less than no check. If it flags a handler that it should not have, that is the most useful issue you can open.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install and use
&lt;/h2&gt;

&lt;p&gt;Python 3.10 or newer. No dependencies outside the standard library. MIT licensed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install silentcatch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# whole project&lt;/span&gt;
silentcatch .

&lt;span class="c"&gt;# only lines changed against main&lt;/span&gt;
silentcatch --base origin/main

&lt;span class="c"&gt;# also fail on broad handlers that log but do not re-raise&lt;/span&gt;
silentcatch --strict
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exit code is 1 if anything failed, 0 otherwise.&lt;/p&gt;

&lt;p&gt;As a GitHub Action, with findings shown as inline annotations on the pull request diff:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;silentcatch&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;pull_request&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;silentcatch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;fetch-depth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;          &lt;span class="c1"&gt;# needed for --base
&lt;/span&gt;      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ikramulmustafa/silentcatch@v1&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;base&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;origin/${{ github.base_ref }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/ikramulmustafa/silentcatch" rel="noopener noreferrer"&gt;https://github.com/ikramulmustafa/silentcatch&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PyPI: &lt;a href="https://pypi.org/project/silentcatch/" rel="noopener noreferrer"&gt;https://pypi.org/project/silentcatch/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>devops</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Laravel RAG Regression Testing in CI</title>
      <dc:creator>Muhammad Ikram UL Mustafa</dc:creator>
      <pubDate>Thu, 19 Nov 2026 15:55:44 +0000</pubDate>
      <link>https://dev.to/ikramulmustafa/laravel-rag-regression-testing-in-ci-1bkm</link>
      <guid>https://dev.to/ikramulmustafa/laravel-rag-regression-testing-in-ci-1bkm</guid>
      <description>&lt;p&gt;A broken queue job throws. A bad migration stops the deploy. A RAG feature that gets worse does neither: it keeps answering confidently and keeps citing sources. They are just the wrong sources, and you find out when a user does.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://github.com/ikramulmustafa/laravel-rag" rel="noopener noreferrer"&gt;laravel-rag&lt;/a&gt;, an open-source Composer package, so that kind of regression fails a build instead. Its &lt;code&gt;rag:eval&lt;/code&gt; command runs golden questions against your corpus, scores recall and MRR, and with &lt;code&gt;--ci&lt;/code&gt; exits non-zero when a score drops below a floor you set. Version 0.3.0 rebuilt the package on the official Laravel AI SDK, and this tutorial is written for it. The &lt;a href="https://ikramulmustafa.com/projects/laravel-rag-retrieval-with-an-eval-harness" rel="noopener noreferrer"&gt;laravel-rag case study&lt;/a&gt; covers why it exists; this post goes from install to a red GitHub Actions check.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why retrieval regressions never throw an error
&lt;/h2&gt;

&lt;p&gt;Three ordinary changes cause most of them: a different chunk size, a cheaper embedding model, or a few hundred new documents crowding the old ones out of the top results. None raise an exception, and unit tests keep passing because they never check what the retriever returns for a real question.&lt;/p&gt;

&lt;p&gt;So test retrieval directly. Scoring final answers needs an LLM in the loop, which is slow, costs money and varies between runs, so it belongs in a nightly job. Scoring retrieval costs one embedding call per question, and it is where most regressions start: if the right chunk never reaches the prompt, no prompt change recovers it. The wider retrieval fixes are in &lt;a href="https://ikramulmustafa.com/blog/rag-that-doesnt-hallucinate-retrieval-patterns-that-scale" rel="noopener noreferrer"&gt;RAG that doesn't hallucinate&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install laravel-rag on the Laravel AI SDK
&lt;/h2&gt;

&lt;p&gt;The package is &lt;a href="https://packagist.org/packages/ikramulmustafa/laravel-rag" rel="noopener noreferrer"&gt;ikramulmustafa/laravel-rag on Packagist&lt;/a&gt;. Version 0.3.0 needs PHP 8.3+ with Laravel 12.62+ or 13.15+, plus the official Laravel AI SDK (&lt;code&gt;laravel/ai&lt;/code&gt; ^1.4), which makes the embedding calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require laravel/ai ikramulmustafa/laravel-rag
php artisan vendor:publish --tag=ai-config
php artisan vendor:publish --tag=rag-config

&lt;span class="c"&gt;# then, in .env, before migrating:&lt;/span&gt;
&lt;span class="c"&gt;#   DB_CONNECTION=pgsql&lt;/span&gt;
&lt;span class="c"&gt;#   OPENAI_API_KEY=sk-...&lt;/span&gt;
&lt;span class="c"&gt;#   RAG_EMBEDDING_PROVIDER=openai&lt;/span&gt;
&lt;span class="c"&gt;#   RAG_EMBEDDING_MODEL=text-embedding-3-small&lt;/span&gt;
&lt;span class="c"&gt;#   RAG_EMBEDDING_DIMENSIONS=1536&lt;/span&gt;

php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Credentials and endpoints live in &lt;code&gt;config/ai.php&lt;/code&gt;, which is the SDK's file. The package only says which provider and model to use. &lt;code&gt;RAG_EMBEDDING_PROVIDER&lt;/code&gt; names a provider from &lt;code&gt;config/ai.php&lt;/code&gt;; left empty, it falls back to the SDK's &lt;code&gt;ai.default_for_embeddings&lt;/code&gt;. It has to be a single name, not a failover list, because a fallback provider would store another model's vectors under this model's id. Any SDK provider that supports embeddings works. A local model through Ollama needs no 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="nv"&gt;RAG_EMBEDDING_PROVIDER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ollama
&lt;span class="nv"&gt;RAG_EMBEDDING_MODEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nomic-embed-text
&lt;span class="nv"&gt;RAG_EMBEDDING_DIMENSIONS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;768
&lt;span class="nv"&gt;RAG_EMBEDDING_SEND_DIMENSIONS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last line is "native" mode: no &lt;code&gt;dimensions&lt;/code&gt; parameter is sent, for models that reject one (it works on the &lt;code&gt;ollama&lt;/code&gt; and &lt;code&gt;openai-compatible&lt;/code&gt; providers, and only while that provider has no &lt;code&gt;models.embeddings.dimensions&lt;/code&gt; in &lt;code&gt;config/ai.php&lt;/code&gt;, because the SDK then sends that value; the SDK's &lt;code&gt;openai&lt;/code&gt; provider always sends a width). The width is still checked on every vector that comes back.&lt;/p&gt;

&lt;p&gt;On PostgreSQL the migration runs &lt;code&gt;CREATE EXTENSION IF NOT EXISTS vector&lt;/code&gt; (the database role needs permission), then creates &lt;code&gt;rag_documents&lt;/code&gt;, which in 0.3 also keeps each document's text, &lt;code&gt;rag_chunks&lt;/code&gt; with a &lt;code&gt;vector&lt;/code&gt; column and an HNSW cosine index (for widths up to 2,000), and &lt;code&gt;rag_eval_runs&lt;/code&gt; for eval history. Set &lt;code&gt;RAG_EMBEDDING_DIMENSIONS&lt;/code&gt; first: the migration writes it into the column type. Changing it later on Postgres means &lt;code&gt;php artisan rag:reindex --resize&lt;/code&gt;, which swaps the column for one of the new width without rewriting the table and then re-embeds everything.&lt;/p&gt;

&lt;p&gt;SQLite and MySQL work too, computing cosine similarity in PHP. That loads every candidate row into memory, so it suits tests and small corpora, which is exactly what an eval job holds. Coming from 0.2? Read &lt;a href="https://github.com/ikramulmustafa/laravel-rag/blob/main/UPGRADING.md" rel="noopener noreferrer"&gt;UPGRADING.md&lt;/a&gt; first, and save a baseline under 0.2 before you upgrade, as described further down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure the embedder, chunking and floors
&lt;/h2&gt;

&lt;p&gt;Settings live in &lt;code&gt;config/rag.php&lt;/code&gt;. The embedding driver defaults to &lt;code&gt;sdk&lt;/code&gt;. The &lt;code&gt;fake&lt;/code&gt; driver is a deterministic, network-free embedder for tests; it builds vectors as wide as &lt;code&gt;RAG_EMBEDDING_DIMENSIONS&lt;/code&gt; (default 1536), so the package's own tests and CI set 64 explicitly. The old &lt;code&gt;openai&lt;/code&gt; driver name still works as a deprecated alias for &lt;code&gt;sdk&lt;/code&gt;, and &lt;code&gt;Rag::extend()&lt;/code&gt; registers a driver of your own. &lt;code&gt;chunking.size&lt;/code&gt; and &lt;code&gt;chunking.overlap&lt;/code&gt; are in characters (1000 and 200), and &lt;code&gt;retrieval.min_score&lt;/code&gt; starts at 0.0, which drops nothing. The eval section, trimmed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'eval'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'suite_path'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'RAG_EVAL_SUITE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;base_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'tests/rag/suite.json'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'k'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'RAG_EVAL_K'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'thresholds'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'pass_rate'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;// RAG_EVAL_MIN_PASS_RATE&lt;/span&gt;
        &lt;span class="s1"&gt;'recall_at_k'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// RAG_EVAL_MIN_RECALL_AT_K&lt;/span&gt;
        &lt;span class="s1"&gt;'mrr'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;// RAG_EVAL_MIN_MRR&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'tolerances'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'pass_rate'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'recall_at_k'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'mrr'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'mean_top_score'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thresholds are absolute floors. Each one can be overridden per environment with the variable in its comment, and an empty value is ignored rather than read as 0.0. That is how a real embedding model gets higher floors than the hashed fake one. Tolerances apply only against a saved baseline and set how far a metric may drop before it counts as a regression.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ingest a corpus with rag:ingest
&lt;/h2&gt;

&lt;p&gt;Commit a small, representative set of real documents next to the suite for CI to rebuild on every run, say under &lt;code&gt;tests/rag/corpus&lt;/code&gt;. Cover the topics users ask about most, plus documents that are easy to confuse.&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;# every *.md file under the directory (the default pattern)&lt;/span&gt;
php artisan rag:ingest tests/rag/corpus

&lt;span class="c"&gt;# other file types, and no confirmation prompt (needed in CI)&lt;/span&gt;
php artisan rag:ingest tests/rag/corpus --pattern=&lt;span class="s1"&gt;'*.mdx'&lt;/span&gt; --force
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sources are exact strings.&lt;/strong&gt; Each file is stored under the path the command found it at, with forward slashes on every platform, so this records &lt;code&gt;tests/rag/corpus/refunds.md&lt;/code&gt;, and the suite must name exactly that. Ingesting from the database instead? Whatever you pass as &lt;code&gt;source&lt;/code&gt; to &lt;code&gt;Rag::ingest()&lt;/code&gt; is what the suite matches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unchanged content is skipped.&lt;/strong&gt; Content is hashed, so re-running does not pay to embed it again.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Big documents ask first.&lt;/strong&gt; If one document is estimated above 100,000 tokens (&lt;code&gt;ingestion.token_warning_threshold&lt;/code&gt;), the command asks before embedding it. &lt;code&gt;--force&lt;/code&gt; skips the prompt, which CI needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Write the eval suite as JSON
&lt;/h2&gt;

&lt;p&gt;Say the corpus holds refund, shipping and annual-leave policies. A suite is a JSON object keyed by case ID. Save it as &lt;code&gt;tests/rag/suite.json&lt;/code&gt;, the default &lt;code&gt;suite_path&lt;/code&gt;, and &lt;code&gt;rag:eval&lt;/code&gt; finds it without a flag:&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="nl"&gt;"refund-window"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"query"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"How long do customers have to request a refund?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"expected_sources"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"tests/rag/corpus/refunds.md"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"must_contain"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"30 days"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"non-refundable-items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"query"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Which items cannot be refunded?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"expected_sources"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"tests/rag/corpus/refunds.md"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"must_contain"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"gift cards"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"shipping-time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"query"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"How long does standard shipping take?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"expected_sources"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"tests/rag/corpus/shipping.md"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"leave-not-shipping"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"query"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"What is the annual leave accrual rate?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"expected_sources"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"tests/rag/corpus/leave.md"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"must_not_contain"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"PO boxes"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"not-in-corpus"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"query"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"What is the CEO's home address?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"expect_no_answer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;expected_sources&lt;/code&gt;: must appear in the top k. Recall and MRR come from these.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;must_contain&lt;/code&gt; and &lt;code&gt;must_not_contain&lt;/code&gt;: case-insensitive text checks on the retrieved chunks. The second catches a neighbouring document leaking in, like shipping rules turning up for a leave question.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expect_no_answer&lt;/code&gt;: passes only if retrieval returns nothing. Combining it with &lt;code&gt;expected_sources&lt;/code&gt; is rejected when the suite loads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No-answer cases are the only test of whether your system can say "I don't know", so include several. At the default &lt;code&gt;min_score&lt;/code&gt; of 0.0 they fail against any non-empty corpus, and in a five-case suite that one failure drops &lt;code&gt;pass_rate&lt;/code&gt; to 0.800, under the floor. The failure message prints the top score each one got; compare it with what real questions score (&lt;code&gt;Rag::retrieve($question)-&amp;gt;topScore()&lt;/code&gt; in Tinker) and set &lt;code&gt;RAG_RETRIEVAL_MIN_SCORE&lt;/code&gt; between the two. Production retrieval uses the same floor, which is the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run rag:eval and read the report
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;php artisan rag:eval&lt;/code&gt; prints a report. &lt;code&gt;--k&lt;/code&gt; sets how many chunks each case retrieves (default 5), &lt;code&gt;--suite&lt;/code&gt; picks another file, and &lt;code&gt;--json&lt;/code&gt; prints only JSON on stdout. Here is a real ingest and eval of the package's own fixture suite on v0.3.0: three short policies and five cases much like the ones above, with the embedder, chunking and &lt;code&gt;k&lt;/code&gt; its CI eval job uses (the fake embedder at 64 dimensions, 200-character chunks with an overlap of 40, &lt;code&gt;k=2&lt;/code&gt;), run locally on a SQLite file database instead of Postgres. Inside a package, &lt;code&gt;vendor/bin/testbench&lt;/code&gt; stands in for &lt;code&gt;php artisan&lt;/code&gt;.&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="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;RAG_EMBEDDING_DRIVER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;fake &lt;span class="nv"&gt;RAG_EMBEDDING_DIMENSIONS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;64 \
    &lt;span class="nv"&gt;RAG_CHUNK_SIZE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;200 &lt;span class="nv"&gt;RAG_CHUNK_OVERLAP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;40 \
    &lt;span class="nv"&gt;DB_CONNECTION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;sqlite &lt;span class="nv"&gt;DB_DATABASE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/tmp/rag.sqlite
$ touch /tmp/rag.sqlite &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; vendor/bin/testbench migrate --force
$ vendor/bin/testbench rag:ingest tests/Fixtures/corpus --force
 0/3 &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;---------------------------&lt;span class="o"&gt;]&lt;/span&gt;   0%
 3/3 &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="o"&gt;============================&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; 100%

   INFO  3 ingested &lt;span class="o"&gt;(&lt;/span&gt;7 chunks&lt;span class="o"&gt;)&lt;/span&gt;, 0 unchanged and skipped.

  Next: &lt;span class="sb"&gt;`php artisan rag:eval`&lt;/span&gt; to check retrieval still holds up.
$ vendor/bin/testbench rag:eval --suite=tests/Fixtures/suite.json --k=2 --save=.rag-baseline.json

   INFO  Suite &lt;span class="s1"&gt;'suite'&lt;/span&gt; — 5 cases at &lt;span class="nv"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2.

+----------------+-------+-------+------+
&lt;span class="o"&gt;|&lt;/span&gt; metric         &lt;span class="o"&gt;|&lt;/span&gt; score &lt;span class="o"&gt;|&lt;/span&gt; floor &lt;span class="o"&gt;|&lt;/span&gt;      &lt;span class="o"&gt;|&lt;/span&gt;
+----------------+-------+-------+------+
&lt;span class="o"&gt;|&lt;/span&gt; pass_rate      &lt;span class="o"&gt;|&lt;/span&gt; 1.000 &lt;span class="o"&gt;|&lt;/span&gt; 0.900 &lt;span class="o"&gt;|&lt;/span&gt; pass &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; recall_at_k    &lt;span class="o"&gt;|&lt;/span&gt; 1.000 &lt;span class="o"&gt;|&lt;/span&gt; 0.800 &lt;span class="o"&gt;|&lt;/span&gt; pass &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; mrr            &lt;span class="o"&gt;|&lt;/span&gt; 1.000 &lt;span class="o"&gt;|&lt;/span&gt; 0.600 &lt;span class="o"&gt;|&lt;/span&gt; pass &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; mean_top_score &lt;span class="o"&gt;|&lt;/span&gt; 0.550 &lt;span class="o"&gt;|&lt;/span&gt; —     &lt;span class="o"&gt;|&lt;/span&gt;      &lt;span class="o"&gt;|&lt;/span&gt;
+----------------+-------+-------+------+

   INFO  All cases passed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;recall_at_k&lt;/strong&gt;: the share of each case's expected sources found in the top k, averaged.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mrr&lt;/strong&gt;: mean reciprocal rank, 1 divided by the rank of the first chunk from an expected source (0 if none). It rewards putting the right material first, since a prompt only fits a few chunks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pass_rate&lt;/strong&gt;: the share of cases that passed every check.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mean_top_score&lt;/strong&gt;: average similarity of each case's best match. No floor; a steady slide usually means corpus dilution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No-answer cases count toward &lt;code&gt;pass_rate&lt;/code&gt; only. Keep &lt;code&gt;k&lt;/code&gt; well below the corpus size. The fixture corpus is seven chunks, hence &lt;code&gt;--k=2&lt;/code&gt;. At &lt;code&gt;--k=4&lt;/code&gt; the same corpus drops &lt;code&gt;pass_rate&lt;/code&gt; to 0.800 while recall and MRR stay at 1.000: the window now holds the shipping chunk that mentions PO boxes, so the &lt;code&gt;must_not_contain&lt;/code&gt; case fails whatever the ranking is. A window holding most of the corpus measures nothing. The eval searches every collection, so keep the eval database to your suite's corpus.&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;--ci&lt;/code&gt;, a completed run exits 0 whatever the scores. With &lt;code&gt;--ci&lt;/code&gt;, it exits 1 when any metric under &lt;code&gt;thresholds&lt;/code&gt; is below its floor. A missing suite file exits 1 either way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gate pull requests in GitHub Actions
&lt;/h2&gt;

&lt;p&gt;The package gates its own pull requests with an eval job on Postgres and pgvector. Adapted for an application that embeds with OpenAI through the SDK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Retrieval eval&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;postgres&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pgvector/pgvector:pg16&lt;/span&gt;
        &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
          &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rag_test&lt;/span&gt;
        &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'5432:5432'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;
          &lt;span class="s"&gt;--health-cmd pg_isready&lt;/span&gt;
          &lt;span class="s"&gt;--health-interval 10s&lt;/span&gt;
          &lt;span class="s"&gt;--health-timeout 5s&lt;/span&gt;
          &lt;span class="s"&gt;--health-retries 5&lt;/span&gt;

    &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;DB_CONNECTION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pgsql&lt;/span&gt;
      &lt;span class="na"&gt;DB_HOST&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
      &lt;span class="na"&gt;DB_PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;
      &lt;span class="na"&gt;DB_DATABASE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rag_test&lt;/span&gt;
      &lt;span class="na"&gt;DB_USERNAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
      &lt;span class="na"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
      &lt;span class="na"&gt;OPENAI_API_KEY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.OPENAI_API_KEY }}&lt;/span&gt;
      &lt;span class="na"&gt;RAG_EMBEDDING_PROVIDER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;openai&lt;/span&gt;
      &lt;span class="na"&gt;RAG_EMBEDDING_MODEL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;text-embedding-3-small&lt;/span&gt;
      &lt;span class="na"&gt;RAG_EMBEDDING_DIMENSIONS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1536&lt;/span&gt;
      &lt;span class="na"&gt;RAG_RETRIEVAL_MIN_SCORE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.RAG_RETRIEVAL_MIN_SCORE }}&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v5&lt;/span&gt;

      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;shivammathur/setup-php@v2&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;php-version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'8.3'&lt;/span&gt;
          &lt;span class="na"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mbstring, pdo, pdo_pgsql&lt;/span&gt;
          &lt;span class="na"&gt;coverage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;none&lt;/span&gt;

      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;composer install --prefer-dist --no-interaction --no-progress&lt;/span&gt;

      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Ingest the eval corpus&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;cp .env.example .env&lt;/span&gt;
          &lt;span class="s"&gt;php artisan key:generate&lt;/span&gt;
          &lt;span class="s"&gt;php artisan migrate --force&lt;/span&gt;
          &lt;span class="s"&gt;php artisan rag:ingest tests/rag/corpus --force&lt;/span&gt;

      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Fail the build if retrieval regressed&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;php artisan rag:eval --ci&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Job-level &lt;code&gt;env&lt;/code&gt; values take precedence over the copied &lt;code&gt;.env&lt;/code&gt;, and &lt;code&gt;config/ai.php&lt;/code&gt; reads &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; from the repository secret. &lt;code&gt;RAG_RETRIEVAL_MIN_SCORE&lt;/code&gt; comes from a repository variable: set it to the score floor you chose from your no-answer cases above. Until it is set, retrieval keeps the default of 0.0, the &lt;code&gt;not-in-corpus&lt;/code&gt; case fails, &lt;code&gt;pass_rate&lt;/code&gt; lands at 0.800 and the job stays red. The container starts empty, so every run embeds the whole CI corpus plus one call per question; keep it small.&lt;/p&gt;

&lt;p&gt;For a job that checks the wiring without any key, replace the four embedding lines with &lt;code&gt;RAG_EMBEDDING_DRIVER: fake&lt;/code&gt; and &lt;code&gt;RAG_EMBEDDING_DIMENSIONS: 64&lt;/code&gt;. That is what the package's own eval job does, adding &lt;code&gt;RAG_CHUNK_SIZE: 200&lt;/code&gt; and &lt;code&gt;RAG_CHUNK_OVERLAP: 40&lt;/code&gt; so its three fixture documents become seven chunks, and running &lt;code&gt;rag:eval&lt;/code&gt; with &lt;code&gt;--k=2 --ci&lt;/code&gt;. The fake embedder hashes words into buckets. It is free, offline and deterministic, but not semantic, so its scores say nothing about how your real questions rank. Gate on the model you ship.&lt;/p&gt;

&lt;p&gt;A real model does not have to mean a paid key. The package also runs a weekly workflow, &lt;code&gt;eval-real.yml&lt;/code&gt;, that installs Ollama in the runner, pulls &lt;code&gt;nomic-embed-text&lt;/code&gt; (768 dimensions, native mode) and runs the same fixture suite against a SQLite file database. Its first run, on 2026-11-19, passed all 5 cases at &lt;code&gt;k=2&lt;/code&gt; with 200/40 chunking: recall@k 1.000, MRR 1.000 and a mean top score of 0.769. Its floors are 0.95, set through the three &lt;code&gt;RAG_EVAL_MIN_*&lt;/code&gt; variables, and it runs &lt;code&gt;rag:eval&lt;/code&gt; with &lt;code&gt;--ci --json&lt;/code&gt;. One detail worth copying: a final step checks that the saved report names &lt;code&gt;ollama:nomic-embed-text:768&lt;/code&gt; as its embedding model. The fixture suite also passes on the fake embedder, so a job that quietly fell back to it would look green.&lt;/p&gt;

&lt;p&gt;Each run is also recorded in &lt;code&gt;rag_eval_runs&lt;/code&gt; with its scores, failure reasons, embedding model, the settings it ran with (&lt;code&gt;k&lt;/code&gt;, chunking, &lt;code&gt;min_score&lt;/code&gt;, reranking) and the commit SHA when it can be found (on GitHub Actions, or from &lt;code&gt;.git/HEAD&lt;/code&gt;). The history pays off where the database persists, such as staging, not in a throwaway CI container. A failed write only warns on stderr, so &lt;code&gt;--json&lt;/code&gt; output stays parseable, and &lt;code&gt;--no-record&lt;/code&gt; skips it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Catch regressions the floors miss with a baseline
&lt;/h2&gt;

&lt;p&gt;Floors answer "is this bad?". As a suite grows, "did this get worse?" matters more. Run &lt;code&gt;php artisan rag:eval --save=.rag-baseline.json&lt;/code&gt; on main, commit the file, and gate pull requests with &lt;code&gt;php artisan rag:eval --baseline=.rag-baseline.json --ci&lt;/code&gt;. Here is the fixture suite re-chunked from 200 characters to 80 (overlap 40 to 10, via &lt;code&gt;RAG_CHUNK_SIZE&lt;/code&gt; and &lt;code&gt;RAG_CHUNK_OVERLAP&lt;/code&gt;), reindexed, and compared with a baseline saved from the run above:&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="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;RAG_CHUNK_SIZE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;80 &lt;span class="nv"&gt;RAG_CHUNK_OVERLAP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;10
$ vendor/bin/testbench rag:reindex --force

   WARN  Re-embedding 3 document&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; with fake:deterministic-hash:64. This calls the embedding API &lt;span class="k"&gt;for&lt;/span&gt; every chunk.

 0/3 &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;---------------------------&lt;span class="o"&gt;]&lt;/span&gt;   0%
 3/3 &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="o"&gt;============================&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; 100%

   INFO  Reindex complete.

$ vendor/bin/testbench rag:eval --suite=tests/Fixtures/suite.json --k=2 \
    --baseline=.rag-baseline.json --ci

   INFO  Suite &lt;span class="s1"&gt;'suite'&lt;/span&gt; — 5 cases at &lt;span class="nv"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2.

+----------------+-------+-------+------+
&lt;span class="o"&gt;|&lt;/span&gt; metric         &lt;span class="o"&gt;|&lt;/span&gt; score &lt;span class="o"&gt;|&lt;/span&gt; floor &lt;span class="o"&gt;|&lt;/span&gt;      &lt;span class="o"&gt;|&lt;/span&gt;
+----------------+-------+-------+------+
&lt;span class="o"&gt;|&lt;/span&gt; pass_rate      &lt;span class="o"&gt;|&lt;/span&gt; 1.000 &lt;span class="o"&gt;|&lt;/span&gt; 0.900 &lt;span class="o"&gt;|&lt;/span&gt; pass &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; recall_at_k    &lt;span class="o"&gt;|&lt;/span&gt; 1.000 &lt;span class="o"&gt;|&lt;/span&gt; 0.800 &lt;span class="o"&gt;|&lt;/span&gt; pass &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; mrr            &lt;span class="o"&gt;|&lt;/span&gt; 0.900 &lt;span class="o"&gt;|&lt;/span&gt; 0.600 &lt;span class="o"&gt;|&lt;/span&gt; pass &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; mean_top_score &lt;span class="o"&gt;|&lt;/span&gt; 0.538 &lt;span class="o"&gt;|&lt;/span&gt; —     &lt;span class="o"&gt;|&lt;/span&gt;      &lt;span class="o"&gt;|&lt;/span&gt;
+----------------+-------+-------+------+

   INFO  All cases passed.


   INFO  Against baseline:

  pass_rate        1.000 → 1.000  +0.000
  recall_at_k      1.000 → 1.000  +0.000
  mrr              1.000 → 0.900  -0.100
  mean_top_score   0.550 → 0.537  -0.012

   WARN  Baseline was run with different settings: chunk_size, chunk_overlap.

   WARN  1 metric&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; regressed:

  ↓ mrr dropped 0.1000 &lt;span class="o"&gt;(&lt;/span&gt;1.0000 -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 0.9000&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On floors alone, this change ships. Every case passes, every metric is above its floor, and the report says "All cases passed." The baseline shows what the floors cannot: MRR fell from 1.000 to 0.900, which across five cases means one question now gets its right document second instead of first. With a tight context budget, second can mean cut. Nothing threw, and the command exits 1. &lt;code&gt;mean_top_score&lt;/code&gt; also slipped, by 0.012 (the comparison rounds to four places before printing, hence 0.537 against the table's 0.538), but that is inside its 0.02 tolerance, so it is reported and not counted.&lt;/p&gt;

&lt;p&gt;A smaller chunk size can also hide a regression behind better-looking scores. In this post's 0.2 version, cutting the same corpus from 1,000-character chunks to 80 at &lt;code&gt;k=1&lt;/code&gt; raised &lt;code&gt;mean_top_score&lt;/code&gt; from 0.311 to 0.436 and kept recall and MRR at 1.000, while the chunk now ranked first for "Which items cannot be refunded?" no longer said "gift cards". Only the &lt;code&gt;must_contain&lt;/code&gt; check caught it.&lt;/p&gt;

&lt;p&gt;The settings warning is new in 0.3. Each run records its &lt;code&gt;k&lt;/code&gt;, chunking, &lt;code&gt;min_score&lt;/code&gt;, reranking and embedding model, and a comparison across different settings says so, because a metric that moved after a chunking change is not a like-for-like comparison. It is a warning only; changing &lt;code&gt;k&lt;/code&gt; or chunk size on purpose is a normal reason to re-save a baseline, not a build failure in itself.&lt;/p&gt;

&lt;p&gt;With both flags, a build fails on a floor breach or any drop beyond a metric's tolerance. A missing or unreadable baseline file warns and skips the comparison, so a first run is not punished for having nothing to compare with. A baseline from a different embedding model gets its own warning that the scores are not comparable and should be re-saved.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do when the eval fails
&lt;/h2&gt;

&lt;p&gt;Read the failure reasons first: each names the case, the query and the problem, such as an expected source missing from the top k, along with what came back instead. Then match the metric to a cause:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;recall_at_k dropped:&lt;/strong&gt; the right document left the window. Check chunking, the embedding model, or new documents outranking old ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mrr dropped, recall held:&lt;/strong&gt; the right document ranks lower, where a tight context budget may cut it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only pass_rate dropped:&lt;/strong&gt; usually a text check or no-answer case. A fact was split out of the top chunk, or something irrelevant is getting in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mean_top_score sliding:&lt;/strong&gt; the corpus is diluting. Check what was added.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After a chunking change, run &lt;code&gt;php artisan rag:reindex&lt;/code&gt;, not &lt;code&gt;rag:ingest&lt;/code&gt;. A chunk-size edit changes no content hash, so ingest skips every document; on the fixture corpus it reports &lt;code&gt;0 ingested (0 chunks), 3 unchanged and skipped&lt;/code&gt;, and the eval goes on scoring the old chunks. &lt;code&gt;rag:reindex --stale&lt;/code&gt; does not help here either: it selects documents embedded with another model, and the model has not changed, so it reports "No stale documents to reindex." A plain &lt;code&gt;rag:reindex&lt;/code&gt; re-embeds every stored document from the text saved at ingest time, so the working directory no longer matters, except for documents ingested before 0.3, which have no stored text and are re-read from their source path.&lt;/p&gt;

&lt;p&gt;After a model swap, the retriever throws &lt;code&gt;MixedEmbeddingModels&lt;/code&gt; on the first search, naming both model ids, until you reindex or restore the old model. Run &lt;code&gt;php artisan rag:reindex --stale&lt;/code&gt;: it re-embeds only documents that still have chunks from another model or chunks without a vector. On Postgres, if the new model has a different width, run &lt;code&gt;rag:reindex --resize&lt;/code&gt; instead: it changes the column, then reindexes as &lt;code&gt;--stale&lt;/code&gt;. A document that fails is listed as &lt;code&gt;collection/source&lt;/code&gt;, the command exits non-zero, and running &lt;code&gt;--stale&lt;/code&gt; again resumes. Then re-save the baseline, because scores from different models are not comparable.&lt;/p&gt;

&lt;p&gt;If a drop is a trade-off you accept, re-save the baseline in the same pull request so the reviewer sees the new numbers. Lowering a floor to get a green build is deleting a failing test with extra steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the package stops
&lt;/h2&gt;

&lt;p&gt;laravel-rag still keeps to retrieval. Since 0.3 it has an optional second stage: SDK reranking, off by default, which fetches more candidates by vector similarity, asks a reranking model to pick the best, keeps both scores on each citation, and falls back to vector order with a logged warning if the reranker fails. &lt;code&gt;Rag::tool()&lt;/code&gt; gives an SDK agent a search tool that returns scored citations as JSON, with an optional fixed collection, an allow-list of filters the model may use, and an output budget. What it still does not do: no BM25 blending, so exact identifiers like product codes remain the weak spot of pure vector search, and no answer-level or LLM-judge evals. Those still belong in a nightly job you build on top. If you are still choosing sources and chunking, start with my &lt;a href="https://ikramulmustafa.com/blog/implementing-rag-a-comprehensive-guide" rel="noopener noreferrer"&gt;guide to implementing RAG&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To get retrieval gated like this in your own product, see my &lt;a href="https://ikramulmustafa.com/rag-ai" rel="noopener noreferrer"&gt;RAG development&lt;/a&gt; and &lt;a href="https://ikramulmustafa.com/laravel-development" rel="noopener noreferrer"&gt;Laravel development&lt;/a&gt; services.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>rag</category>
      <category>ai</category>
    </item>
    <item>
      <title>Context Rot: Why Your RAG Pipeline Degrades Without Anyone Deploying Anything</title>
      <dc:creator>Muhammad Ikram UL Mustafa</dc:creator>
      <pubDate>Mon, 02 Nov 2026 02:19:34 +0000</pubDate>
      <link>https://dev.to/ikramulmustafa/context-rot-why-your-rag-pipeline-degrades-without-anyone-deploying-anything-1278</link>
      <guid>https://dev.to/ikramulmustafa/context-rot-why-your-rag-pipeline-degrades-without-anyone-deploying-anything-1278</guid>
      <description>&lt;p&gt;Nobody changed the model. Nobody touched the prompt. Accuracy dropped anyway.&lt;/p&gt;

&lt;p&gt;This is the most common production RAG failure I see, and it is the hardest to notice, because there is no deploy to correlate it with. The system that passed every test in March is quietly wrong in September.&lt;/p&gt;

&lt;p&gt;The cause is almost always the same: your index drifted away from your source of truth, and nothing told you.&lt;/p&gt;

&lt;h2&gt;
  
  
  The failure nobody instruments for
&lt;/h2&gt;

&lt;p&gt;A RAG pipeline has two copies of your knowledge. The source documents, and the embedded chunks derived from them. Those are updated at different times, by different code paths, and often by different people.&lt;/p&gt;

&lt;p&gt;Every gap between them is a window where retrieval returns something that is no longer true.&lt;/p&gt;

&lt;p&gt;Here is what actually happens over six months:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Documents get updated, chunks do not.&lt;/strong&gt; Someone edits a policy document. The text store updates. The embedding job is queued behind something else, or failed silently three weeks ago, or was never wired up for edits because it was built for initial ingestion. Retrieval now returns the old content, confidently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Documents get deleted, chunks survive.&lt;/strong&gt; A product is discontinued and its documentation is removed. Nobody deletes the chunks, because deletion was never part of the ingestion path. The model is still happily answering questions about it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;New documents change the neighbourhood.&lt;/strong&gt; You add two hundred new documents. Nothing about the old ones changed, but they are no longer in the top five for the queries they used to answer, because something more semantically similar now outranks them. Your accuracy on existing questions drops and nobody deployed anything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The embedding model changes under you.&lt;/strong&gt; Provider updates the model behind the same API name. New queries are embedded with the new version, your stored chunks with the old one. Now you are comparing vectors from two different spaces. This one is particularly nasty because similarity scores still come back, they are just meaningless.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chunking configuration changes.&lt;/strong&gt; You tune chunk size or overlap because retrieval quality was poor. Everything ingested after that uses the new strategy, everything before it does not. One index, two chunking schemes, and the older documents quietly retrieve worse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query distribution shifts.&lt;/strong&gt; Your users start asking about things the corpus never covered well. Retrieval still returns the closest match, because that is what it does. The closest match to a question you have no answer for is simply the wrong answer with high confidence.&lt;/p&gt;

&lt;p&gt;None of these throw an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why your tests did not catch it
&lt;/h2&gt;

&lt;p&gt;Most teams test retrieval once, at build time, with a set of questions written by the person who built it. That tells you the pipeline works. It tells you nothing about whether it still works.&lt;/p&gt;

&lt;p&gt;The second problem is what gets measured. Teams evaluate the final answer, because that is what the user sees. But the answer is downstream of retrieval, so a retrieval failure and a generation failure look identical from the output. You cannot tell whether the model reasoned badly or whether it reasoned correctly over the wrong chunks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instrument the gap, not the output
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Fingerprint the ingestion, not just the content
&lt;/h3&gt;

&lt;p&gt;The instinct is to hash the source document and skip re-embedding when it has not changed. That is right as far as it goes, but it misses two thirds of what determines whether your stored vectors are still valid.&lt;/p&gt;

&lt;p&gt;A chunk is a product of three things: the source text, the embedding model, and the chunking configuration. If any of the three changes, the stored vector is stale. So the fingerprint has to cover all three.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="n"&gt;EMBEDDING_MODEL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;text-embedding-3-small&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;CHUNK_CONFIG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;size&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;overlap&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;strategy&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;structural&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ingestion_fingerprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk_config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;|&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;|&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk_config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sort_keys&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;hexdigest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ingest_document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;fingerprint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ingestion_fingerprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EMBEDDING_MODEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CHUNK_CONFIG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;existing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT ingestion_fingerprint FROM doc_chunks &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;WHERE document_id = $1 LIMIT 1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;existing&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fingerprint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="c1"&gt;# content, model and chunking all unchanged
&lt;/span&gt;
    &lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;chunk_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;CHUNK_CONFIG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;embed_batch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DELETE FROM doc_chunks WHERE document_id = $1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO doc_chunks &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(document_id, tenant_id, content, embedding, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; ingestion_fingerprint, embedding_model, indexed_at) &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;VALUES ($1, $2, $3, $4, $5, $6, now())&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;fingerprint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EMBEDDING_MODEL&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why this matters more than it looks. If you fingerprint only the text, a model migration is silently blocked. Every unchanged document returns early and never gets re-embedded, while your drift monitoring keeps flagging a mismatch that the ingestion path refuses to fix. Detection with no route to repair, which is arguably worse than no detection at all.&lt;/p&gt;

&lt;p&gt;Worth writing a test for the case specifically: same text, bumped model version, assert that re-embedding happens.&lt;/p&gt;

&lt;p&gt;Two other things in that code. The delete and insert are in one transaction, so a query cannot land in a window where the old chunks are gone and the new ones are not written yet. And &lt;code&gt;embedding_model&lt;/code&gt; is stored per row as well as inside the fingerprint, because you want to query it directly in the next section.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Find the drift
&lt;/h3&gt;

&lt;p&gt;Once the fingerprint and model version are stored, staleness becomes a query rather than a mystery.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- chunks whose ingestion inputs no longer match current config&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexed_at&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;doc_chunks&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ingestion_fingerprint&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;-- recomputed per document in app code&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexed_at&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- orphaned chunks: source document is gone&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;doc_chunks&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- chunks embedded with a different model than you are querying with&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;embedding_model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;doc_chunks&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;embedding_model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That last one should return exactly one row. If it returns two, you have vectors from two different embedding spaces in the same index and your similarity scores are not comparable.&lt;/p&gt;

&lt;p&gt;Run these on a schedule and alert on them.&lt;/p&gt;

&lt;p&gt;One thing worth adding beyond a schedule: a freshness threshold per document, with an alert when anything exceeds it. A scheduled job running on time and the data actually being current are different claims, and only the second one matters. If the re-indexing job failed three weeks ago, a cron that fires successfully every night will never tell you.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Evaluate retrieval separately from generation
&lt;/h3&gt;

&lt;p&gt;This is the change that matters most, and it is the one teams skip.&lt;/p&gt;

&lt;p&gt;Build a set of questions where you know which document should be retrieved. Not which answer is correct, which &lt;em&gt;source&lt;/em&gt; is correct. Then measure whether it still comes back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;RETRIEVAL_CASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;what is the refund window for annual plans&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;must_retrieve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;billing-policy-v4&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;how do I rotate an API key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;must_retrieve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;api-authentication&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_retrieval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;failures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;RETRIEVAL_CASES&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;retrieved_docs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;document_slug&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;must_retrieve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;retrieved_docs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;failures&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;expected&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;must_retrieve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;got&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retrieved_docs&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;failures&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run this nightly. When it starts failing, you know the problem is retrieval and not the model, which is half the debugging done before you have opened anything.&lt;/p&gt;

&lt;p&gt;The useful property of this test is that it fails for reasons that have nothing to do with your code. New documents outranking old ones, an embedding model change, a chunking tweak that seemed harmless. Those are exactly the changes that have no deploy attached.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Treat an empty or weak retrieval as a real state
&lt;/h3&gt;

&lt;p&gt;If nothing crosses your similarity threshold, that is information. Most pipelines throw it away and send whatever came back anyway.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;MIN_SIMILARITY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.35&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;relevant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;similarity&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;MIN_SIMILARITY&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;relevant&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;log_retrieval_miss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I don&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;t have information about that.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&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;relevant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those logged misses are the most valuable dataset you will build. They are a direct record of what your users want and your corpus does not cover, and the rate of them climbing over time is drift showing up in your metrics before it shows up in complaints.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape of the problem
&lt;/h2&gt;

&lt;p&gt;A RAG system is not a feature you ship. It is two stores that have to agree, updated on different schedules by different code, with a model in front making whatever they return sound reasonable.&lt;/p&gt;

&lt;p&gt;Everything above is the same idea in different places: make the disagreement visible. Fingerprint so you can detect it, version so you can detect it, test retrieval so you can detect it, log misses so you can detect it.&lt;/p&gt;

&lt;p&gt;The harder problem underneath all of it is ownership. Retrieval quality tends to sit between the data team and the AI team, which usually means nobody is on call for it. Model performance has an owner. Index freshness often does not.&lt;/p&gt;

&lt;p&gt;Because the default behaviour of this failure is a well-written answer, delivered confidently, that happens to be six months out of date. Nothing in your stack is going to tell you about it.&lt;/p&gt;




&lt;p&gt;I write about production AI systems and what breaks in them. More at &lt;a href="https://ikramulmustafa.com" rel="noopener noreferrer"&gt;ikramulmustafa.com&lt;/a&gt;, and a RAG agent with documented failure handling at &lt;a href="https://github.com/ikramulmustafa/n8n-rag-lead-agent" rel="noopener noreferrer"&gt;github.com/ikramulmustafa/n8n-rag-lead-agent&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>postgres</category>
      <category>python</category>
    </item>
    <item>
      <title>Your RAG Pipeline Is a Data Leak Waiting to Happen</title>
      <dc:creator>Muhammad Ikram UL Mustafa</dc:creator>
      <pubDate>Fri, 09 Oct 2026 06:18:08 +0000</pubDate>
      <link>https://dev.to/ikramulmustafa/your-rag-pipeline-is-a-data-leak-waiting-to-happen-2g4c</link>
      <guid>https://dev.to/ikramulmustafa/your-rag-pipeline-is-a-data-leak-waiting-to-happen-2g4c</guid>
      <description>&lt;p&gt;Your multi-tenant app is probably well isolated. Every query is scoped to a tenant, enforced centrally, and no individual developer has to remember it.&lt;/p&gt;

&lt;p&gt;Then you add retrieval, and you quietly create a second data path that never gets the same discipline.&lt;/p&gt;

&lt;h2&gt;
  
  
  The failure mode
&lt;/h2&gt;

&lt;p&gt;Tenant A asks a question. Semantic search finds the most relevant chunks. One of them came from Tenant B's document. The model reads it and answers fluently, using data the user was never allowed to see.&lt;/p&gt;

&lt;p&gt;No error. No exception. No alert. Just a confident, correct-sounding answer built on someone else's data.&lt;/p&gt;

&lt;p&gt;This is worse than an ordinary data leak, because there is no artifact. The user never saw Tenant B's document. They saw a paragraph of generated text that happened to contain its contents. Your logs show a successful request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;Most teams build the application layer first and get isolation right there. In Laravel it is a global scope. In Django it is a manager. Somewhere central, every query gets &lt;code&gt;WHERE tenant_id = ?&lt;/code&gt; attached, and developers stop thinking about it because they no longer have to.&lt;/p&gt;

&lt;p&gt;Then retrieval arrives as a separate feature. Documents get chunked, embedded, and written to a vector store. The code that does this often lives in a background job or an ingestion script, written by whoever was working on the AI feature that sprint.&lt;/p&gt;

&lt;p&gt;Now you have two paths to the same data, and only one of them inherited the rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  A concrete example
&lt;/h2&gt;

&lt;p&gt;Here is ingestion that looks fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ingest_document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;chunk_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;embed_batch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO doc_chunks (document_id, content, embedding) &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;VALUES ($1, $2, $3)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spot the problem. There is no &lt;code&gt;tenant_id&lt;/code&gt; on the row. The document has one, but it did not make it into the chunk table. You can still get back to it by joining through &lt;code&gt;documents&lt;/code&gt;, which means somebody has to remember to join, which means eventually somebody does not.&lt;/p&gt;

&lt;p&gt;And here is the retrieval that goes with it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;embed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT content FROM doc_chunks &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ORDER BY embedding &amp;lt;=&amp;gt; $1 LIMIT $2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean, fast, and it will happily return any tenant's content.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually fixes it
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Denormalise the tenant onto the chunk
&lt;/h3&gt;

&lt;p&gt;Put &lt;code&gt;tenant_id&lt;/code&gt; directly on the vector rows, even though it is already on the parent document. This is deliberate duplication. It removes the join, and it means the filter cannot be forgotten by accident.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;doc_chunks&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="n"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;doc_chunks&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Make the tenant filter impossible to omit
&lt;/h3&gt;

&lt;p&gt;Do not leave it to the caller. Scope it centrally, the same way your ORM already does for SQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tenant_id is required for retrieval&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;embed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT content FROM doc_chunks &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;WHERE tenant_id = $1 &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ORDER BY embedding &amp;lt;=&amp;gt; $2 LIMIT $3&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A required argument with no default is cheap and effective. A developer who forgets gets an error, not a leak.&lt;/p&gt;

&lt;p&gt;If you are on Postgres, row level security is stronger, because it holds even if someone writes a raw query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;doc_chunks&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;SECURITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="n"&gt;tenant_isolation&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;doc_chunks&lt;/span&gt;
  &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.tenant_id'&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Filter before the similarity search, not after
&lt;/h3&gt;

&lt;p&gt;Retrieving the top 20 chunks globally and then discarding the ones belonging to other tenants is a correctness bug waiting to happen, and it also degrades quality. You are spending your result budget on rows you are going to throw away, so a tenant with little data gets worse answers.&lt;/p&gt;

&lt;p&gt;Filter first. Let the index do the work.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Test isolation as a real case
&lt;/h3&gt;

&lt;p&gt;Most teams test that retrieval returns relevant results. Very few test that it refuses to return someone else's.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_tenant_cannot_retrieve_other_tenant_data&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;ingest_for_tenant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TENANT_B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The launch date is March 14th.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;when is the launch&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;TENANT_A&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tenant_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;TENANT_A&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;March 14th&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write the test where Tenant A asks for something only Tenant B has, and assert the answer is empty. If that test does not exist, you do not know your isolation works. You are assuming it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The harder version
&lt;/h2&gt;

&lt;p&gt;Everything above is filtering, which is an application-level control. It runs in the same process as the bug it is guarding against.&lt;/p&gt;

&lt;p&gt;If your tenant count is low and the data is sensitive enough, physical separation is stronger. A separate index, collection or namespace per tenant means a dropped filter cannot leak anything, because the other tenant's vectors are not reachable from that connection at all.&lt;/p&gt;

&lt;p&gt;The tradeoff is operational. Per tenant indexes get expensive at thousands of small tenants, migrations have to run per tenant, and cross tenant analytics become their own problem. For enterprise B2B with tens or hundreds of tenants, especially under compliance pressure, it is usually the right call. For a product with a large free tier, it usually is not.&lt;/p&gt;

&lt;p&gt;Filters are the floor. Physical isolation is the ceiling. Most teams should at least be standing on the floor.&lt;/p&gt;

&lt;h2&gt;
  
  
  The thing to take away
&lt;/h2&gt;

&lt;p&gt;You can have perfect isolation at the SQL layer and still leak through retrieval, because they are two data paths and only one of them inherited your rules.&lt;/p&gt;

&lt;p&gt;Treat the embedding pipeline as a data path with the same requirements as your database, because that is exactly what it is.&lt;/p&gt;

&lt;p&gt;I write about production AI systems and what breaks in them. More at &lt;a href="https://ikramulmustafa.com" rel="noopener noreferrer"&gt;ikramulmustafa.com&lt;/a&gt;, and there's a RAG agent with the failure handling documented at &lt;a href="https://github.com/ikramulmustafa/n8n-rag-lead-agent" rel="noopener noreferrer"&gt;github.com/ikramulmustafa/n8n-rag-lead-agent&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>security</category>
      <category>python</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
