<?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: Ravi Prakash meena</title>
    <description>The latest articles on DEV Community by Ravi Prakash meena (@ravi_prakashmeena_2b2b5b).</description>
    <link>https://dev.to/ravi_prakashmeena_2b2b5b</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%2F2174136%2Fd3018c87-9aa7-4695-9122-589a7de97f60.jpg</url>
      <title>DEV Community: Ravi Prakash meena</title>
      <link>https://dev.to/ravi_prakashmeena_2b2b5b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ravi_prakashmeena_2b2b5b"/>
    <language>en</language>
    <item>
      <title>A correct answer, one curly brace, and an agent that couldn't finish</title>
      <dc:creator>Ravi Prakash meena</dc:creator>
      <pubDate>Tue, 17 Nov 2026 16:18:03 +0000</pubDate>
      <link>https://dev.to/ravi_prakashmeena_2b2b5b/a-correct-answer-one-curly-brace-and-an-agent-that-couldnt-finish-21jg</link>
      <guid>https://dev.to/ravi_prakashmeena_2b2b5b/a-correct-answer-one-curly-brace-and-an-agent-that-couldnt-finish-21jg</guid>
      <description>&lt;p&gt;Ask a coding assistant how to add two numbers in JavaScript and you get something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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;In BeeAI Framework, an answer like that used to stop the agent run. The model was right. The hard part was reading its answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the final answer is modelled
&lt;/h2&gt;

&lt;p&gt;BeeAI's &lt;code&gt;RequirementAgent&lt;/code&gt; and &lt;code&gt;ToolCallingAgent&lt;/code&gt; treat the final answer as one more tool: &lt;code&gt;final_answer&lt;/code&gt;, with a pydantic schema (&lt;code&gt;FinalAnswerToolSchema&lt;/code&gt;, or your own &lt;code&gt;expected_output&lt;/code&gt; schema). Every step is a tool call, and every tool call is validated. That keeps the loop simple.&lt;/p&gt;

&lt;p&gt;Models don't always play along, though. Sometimes they reply in plain text. So the agent converts plain text into a &lt;code&gt;final_answer&lt;/code&gt; tool call.&lt;/p&gt;

&lt;p&gt;The old conversion, roughly:&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;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;first_curly_block&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="c1"&gt;# first {...} in the reply&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;tool_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse_broken_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# lenient repair parser&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;tool_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"response"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;parse_broken_json&lt;/code&gt; is lenient by design. It repairs whatever it gets and returns &lt;em&gt;a value&lt;/em&gt;. But a value isn't necessarily an object, and nothing checked the result against the schema before it became the tool input.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three correct answers, three failures
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Answer&lt;/th&gt;
&lt;th&gt;What the parser returned&lt;/th&gt;
&lt;th&gt;What happened&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;function add(a, b) { return a + b; }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;a list&lt;/td&gt;
&lt;td&gt;Tool input must be an object, so the call couldn't be built and the run stopped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Use the placeholder {name} in your template.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;a list&lt;/td&gt;
&lt;td&gt;Same&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Set the config to {"port": 8080} and restart.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{"port": 8080}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Valid JSON, invalid final answer. The model was told it made an error; the original text was gone&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For the third case, with a scripted model that repeats its answer, the agent kept retrying until &lt;code&gt;max_iterations&lt;/code&gt;: 96 model calls for one question (156 in the other &lt;code&gt;final_answer_as_tool&lt;/code&gt; mode). A real model may recover, but by then the right answer was already discarded and you'd paid for the extra calls.&lt;/p&gt;

&lt;h2&gt;
  
  
  The second problem: the error path
&lt;/h2&gt;

&lt;p&gt;When a tool fails, &lt;code&gt;Tool.run&lt;/code&gt; emits a &lt;code&gt;ToolErrorEvent(input=...)&lt;/code&gt; inside its &lt;code&gt;except&lt;/code&gt; block. &lt;code&gt;ToolErrorEvent.input&lt;/code&gt; was typed &lt;code&gt;BaseModel | dict&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So when the tool input was a list, &lt;em&gt;building the error event&lt;/em&gt; raised a pydantic &lt;code&gt;ValidationError&lt;/code&gt;. That isn't a &lt;code&gt;ToolError&lt;/code&gt;, so &lt;code&gt;run_tool&lt;/code&gt; didn't catch it, and the whole run aborted. This hit any tool called with JSON-array arguments, not only &lt;code&gt;final_answer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code meant to report the problem became the problem, and the model never got feedback it could act on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;One rule: &lt;strong&gt;only treat text as structure if it passes the schema.&lt;/strong&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;def&lt;/span&gt; &lt;span class="nf"&gt;text_to_final_answer_input&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="n"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;parsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;try_parse_first_object&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;FinalAnswerToolSchema&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;parsed&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nf"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schema&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;parsed&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"response"&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="c1"&gt;# keep the answer exactly as written&lt;/span&gt;
    &lt;span class="c1"&gt;# custom expected_output: pass JSON objects on (useful validation errors),&lt;/span&gt;
    &lt;span class="c1"&gt;# but never lists or plain values&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parsed&lt;/span&gt; &lt;span class="k"&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;parsed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"response"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Simplified; the real helper lives in the PR.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both agents now use &lt;code&gt;text_to_final_answer_input&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ToolErrorEvent.input&lt;/code&gt; accepts any value, so a badly shaped tool call becomes a tool error the model can see and fix, and the run continues.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No public API changed. 23 tests cover the helper, both agents, both &lt;code&gt;final_answer_as_tool&lt;/code&gt; modes, and array-argument tool calls; 16 of them fail on the old code. The PR merged on November 12, my first merged Python contribution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;"Is this structured?" and "is this valid?" must use the same schema.&lt;/strong&gt; If the first check is looser, it quietly passes the wrong thing to the second.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lenient parsers need a strict gate after them.&lt;/strong&gt; Repairing JSON is fine; trusting the repaired value without validation isn't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the error path with strange input.&lt;/strong&gt; A list, a string, &lt;code&gt;null&lt;/code&gt;. An error handler that can throw turns a recoverable mistake into a stopped run.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The model is often fine. What breaks is the code that reads its output.&lt;/p&gt;

&lt;p&gt;What's the strangest correct answer you've seen your agent reject?&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;PR 1935: &lt;a href="https://github.com/i-am-bee/beeai-framework/pull/1935" rel="noopener noreferrer"&gt;https://github.com/i-am-bee/beeai-framework/pull/1935&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Issue 1934: &lt;a href="https://github.com/i-am-bee/beeai-framework/issues/1934" rel="noopener noreferrer"&gt;https://github.com/i-am-bee/beeai-framework/issues/1934&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>opensource</category>
      <category>python</category>
      <category>agents</category>
    </item>
    <item>
      <title>An AI agent believes whatever its tools send back</title>
      <dc:creator>Ravi Prakash meena</dc:creator>
      <pubDate>Fri, 13 Nov 2026 17:53:35 +0000</pubDate>
      <link>https://dev.to/ravi_prakashmeena_2b2b5b/an-ai-agent-believes-whatever-its-tools-send-back-2a0g</link>
      <guid>https://dev.to/ravi_prakashmeena_2b2b5b/an-ai-agent-believes-whatever-its-tools-send-back-2a0g</guid>
      <description>&lt;p&gt;It asks a tool for data, gets JSON, and acts on it. If that JSON says a price is &lt;code&gt;"twelve"&lt;/code&gt; where a number was promised, the agent doesn't stop to argue. It carries on with whatever it got.&lt;/p&gt;

&lt;p&gt;For the last few weeks I've been working on one question: how do you check structured output, with plain code, before anything acts on it? My first two open-source fixes came out of that question, a few days apart, in two different projects. On their own they look unrelated. Together they taught me one thing I now apply everywhere:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check what comes back. Then check the thing doing the checking.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 1: nobody was checking what the tool returned
&lt;/h2&gt;

&lt;p&gt;MCP (Model Context Protocol) is the standard way for an AI agent to talk to tools. A tool can publish an output schema, a description of what its result will look like: these fields, these types, these are required. The spec is clear that a server &lt;strong&gt;must&lt;/strong&gt; return results that match it.&lt;/p&gt;

&lt;p&gt;I was reading the Quarkus MCP Server code to see how it handled this. It built the schema from the Java return type and sent it to the agent. Then, when the tool ran, it turned the result into JSON and sent that back too. Nothing compared the two.&lt;/p&gt;

&lt;p&gt;That gap is easy to fall into. The schema is generated by one library and the JSON is written by another. A naming setting, an ignored field, a custom serializer, and the two quietly disagree. The agent was told one shape and handed another.&lt;/p&gt;

&lt;p&gt;Here is what that looks like from the agent's side. The tool promised this:&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;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"properties"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"number"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"currency"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"string"&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;"required"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"currency"&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;and sent this:&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;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"12.50"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"currency_code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"EUR"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A string where a number was promised, and a field under a different name. Nothing failed. The agent got it as if it were fine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fail by default, not log by default
&lt;/h3&gt;

&lt;p&gt;I opened an issue first. The maintainer asked a fair question: what do the other MCP SDKs do? So I read their code. The TypeScript, Python and Java SDKs all check the result against the schema, and all return an error when it doesn't match. None of them has a switch to turn it off.&lt;/p&gt;

&lt;p&gt;That settled the most important design question for me. A check that only writes a warning to a log is a check nobody reads. So the default became &lt;strong&gt;fail&lt;/strong&gt;: if the result doesn't match, the agent gets a tool error that names what went wrong, something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output validation error: Invalid structured content for tool price: [/amount: ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of the bad data. An agent can handle a clear error. It can't handle data that looks right and isn't.&lt;/p&gt;

&lt;p&gt;There is still a setting for teams who need time to adjust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="c1"&gt;# fail (default) | log | off
&lt;/span&gt;&lt;span class="na"&gt;quarkus.mcp.server.schema-validator.tool-output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;fail&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because this changes behaviour for existing tools, it is listed as a breaking change in the release notes, not hidden among new features. That was the maintainer's call in review, and he was right: if you change what happens on a mismatch, people deserve to be told plainly.&lt;/p&gt;

&lt;p&gt;The review also caught something I hadn't thought about: two MCP servers in one app can each have a tool with the same name, so the check has to know which server's schema it is looking at. Small detail, real consequence. The PR was merged on November 6, about a day after I opened it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2: the checker itself was wrong
&lt;/h2&gt;

&lt;p&gt;Every check like this rests on a JSON Schema validator. If the validator says yes, we treat the data as good.&lt;/p&gt;

&lt;p&gt;I build an open-source Java library for checking structured LLM output, Outputgate, and its schema check uses networknt's json-schema-validator, a widely used Java validator. So I wanted to know how much I could trust it.&lt;/p&gt;

&lt;p&gt;I ran it against the newest version of the official JSON Schema test suite, the shared set of cases every validator is meant to pass. Some of the &lt;code&gt;hostname&lt;/code&gt; tests failed.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;hostname&lt;/code&gt; format was checked by one regular expression. It looked at which characters were allowed and stopped there. So it said yes to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;names longer than 253 characters, which the rules for DNS names don't allow;&lt;/li&gt;
&lt;li&gt;international names in their encoded form (labels that start with &lt;code&gt;xn--&lt;/code&gt;) that don't decode to anything valid, like &lt;code&gt;xn--X&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hostnames sound like a niche corner. But think about where they show up in structured output: URLs, email domains, the target of a webhook, the server an agent is told to call. If a field is marked as a hostname and the validator waves through anything that starts with &lt;code&gt;xn--&lt;/code&gt;, then "it passed the schema" means less than you think.&lt;/p&gt;

&lt;h3&gt;
  
  
  A million names against a reference
&lt;/h3&gt;

&lt;p&gt;The fix itself is short to describe: enforce the length limits, and for encoded labels, decode them and apply the rules for international names (which characters are allowed, which can appear only next to certain others, how right-to-left scripts must be arranged).&lt;/p&gt;

&lt;p&gt;Getting it right was not short. It took eight rounds of review with the maintainer, and every round found something real. One example: Java's built-in decoder follows the 2003 version of these rules, so it rejects names the newer rules allow, such as names with the German &lt;code&gt;ß&lt;/code&gt;. I had to write a small decoder that follows the current standard instead.&lt;/p&gt;

&lt;p&gt;What changed the outcome was testing differently. At first I checked a sample of about 2,600 names, and it passed. Then I generated every assigned Unicode character in every context the rules care about, about 460,000 labels, and compared my answers with Python's reference library for international domain names. That run found seven differences the sample had missed completely. By the end, the comparison covered about a million labels and names, and the maintainer ran another 1.2 million random names on his side.&lt;/p&gt;

&lt;p&gt;I also overcorrected once. In one round I applied a rule for international names to plain hostnames too, and it started rejecting names that are perfectly valid. The reference comparison didn't catch it, because I had copied the same wrong expectation into it. The maintainer did. Lesson: when you make a check stricter, check which standard that specific field follows, and make sure your test isn't sharing the check's assumptions.&lt;/p&gt;

&lt;p&gt;The fix was merged on November 7. Outputgate still bundles an older version of the validator, so upgrading it is on my list.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the two fixes have in common
&lt;/h2&gt;

&lt;p&gt;When an agent works, data passes through layers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The model produces output.&lt;/li&gt;
&lt;li&gt;A tool sends a result back.&lt;/li&gt;
&lt;li&gt;A validator decides whether either of those is acceptable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most guardrail advice is about layer 1. My two fixes were in layers 2 and 3, and both were the same kind of problem: something that looks like a check, isn't fully checking, and fails quietly. No exception, no log line, just data that's treated as good.&lt;/p&gt;

&lt;p&gt;Here's what I do differently now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fail closed.&lt;/strong&gt; If a check can't confirm the data, the answer is no. A warning in a log is not a check.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check at the boundary where data enters.&lt;/strong&gt; Tool results are input to the agent, the same as user input. Treat them that way.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the checker with a reference, at scale.&lt;/strong&gt; A sample of a few thousand cases told me everything was fine. A million cases told me otherwise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't let the test share the check's assumptions.&lt;/strong&gt; My one regression got through because the expected answers were wrong in the same way the code was.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make the change visible.&lt;/strong&gt; If a stricter check will start rejecting data that used to pass, say so as a breaking change.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A JSON Schema tells you the output has the right shape. It doesn't tell you the output is right, and it only tells you the shape is right if the validator is right. I now ask that question about every check in the path, including the ones I didn't write.&lt;/p&gt;

&lt;p&gt;When did you last check the checker in your own pipeline?&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Links&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quarkus MCP Server, tool results checked against their output schema: &lt;a href="https://github.com/quarkiverse/quarkus-mcp-server/pull/1127" rel="noopener noreferrer"&gt;https://github.com/quarkiverse/quarkus-mcp-server/pull/1127&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;networknt json-schema-validator, stricter &lt;code&gt;hostname&lt;/code&gt; format: &lt;a href="https://github.com/networknt/json-schema-validator/pull/1377" rel="noopener noreferrer"&gt;https://github.com/networknt/json-schema-validator/pull/1377&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Outputgate, deterministic checks for structured LLM output in Java: &lt;a href="https://github.com/ravismash/outputgate" rel="noopener noreferrer"&gt;https://github.com/ravismash/outputgate&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>agents</category>
      <category>ai</category>
      <category>llm</category>
      <category>mcp</category>
    </item>
    <item>
      <title>Your LLM's JSON passed the schema. It was still wrong.</title>
      <dc:creator>Ravi Prakash meena</dc:creator>
      <pubDate>Mon, 02 Nov 2026 07:53:28 +0000</pubDate>
      <link>https://dev.to/ravi_prakashmeena_2b2b5b/your-llms-json-passed-the-schema-it-was-still-wrong-29if</link>
      <guid>https://dev.to/ravi_prakashmeena_2b2b5b/your-llms-json-passed-the-schema-it-was-still-wrong-29if</guid>
      <description>&lt;p&gt;&lt;em&gt;Deterministic contract tests for structured LLM output on the JVM, and what 120 real documents taught me about where schemas stop.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you put an LLM behind a feature that returns structured data, you have probably written this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modelOutput&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MyRecord&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;schemaValidator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// ship it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JSON parses. It matches the schema. The types bind. And the answer can still be wrong in ways none of that can see: a line dropped from a list while its total stayed the same, a number the model computed instead of copying, a value quoted from the wrong table, an apology tucked inside a text field.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://github.com/ravismash/outputgate" rel="noopener noreferrer"&gt;outputgate&lt;/a&gt;, an MIT-licensed Java 21 library, to catch those. It treats a model's structured output the way you would treat any untrusted input: as something that has to satisfy a &lt;strong&gt;contract&lt;/strong&gt; before your system acts on it. No gate calls a model. Every check is plain, deterministic code, so the same output always gets the same verdict, and you can run it in JUnit, in CI, or at runtime.&lt;/p&gt;

&lt;p&gt;This post covers the idea, the API, and the benchmark that showed where a JSON Schema alone falls short.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: valid shape, wrong content
&lt;/h2&gt;

&lt;p&gt;A JSON Schema answers one question: &lt;em&gt;is this the right shape?&lt;/em&gt; For structured LLM output that is necessary, but it is the easy half. The hard half is whether the content is consistent with itself and with the source it came from.&lt;/p&gt;

&lt;p&gt;Take any record whose parts must add up and whose text must come from a document. Here the model dropped a line, kept the old subtotal, and apologised inside a field:&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;"vendor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Acme"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"subtotal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;100.00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"line_items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Widgets"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;40.00&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Sorry, I could not read this line"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;34.50&lt;/span&gt;&lt;span class="p"&gt;}]}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A schema that requires &lt;code&gt;vendor&lt;/code&gt;, &lt;code&gt;subtotal&lt;/code&gt; and &lt;code&gt;line_items&lt;/code&gt; is perfectly happy with that. A contract is not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Contract&lt;/span&gt; &lt;span class="n"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Contract&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SchemaGate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fromString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""
                {"type": "object", "required": ["vendor", "subtotal", "line_items"]}"""&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MathGate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sumEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"line_items[*].amount"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"subtotal"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tolerance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RegexGate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forbid&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"line_items[*].description"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"(?i)\\b(sorry|as an ai)\\b"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="nc"&gt;Verdict&lt;/span&gt; &lt;span class="n"&gt;verdict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;evaluate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REJECTED
[math] subtotal: sum of line_items[*].amount is 74.50 but subtotal is 100.00 (tolerance 0.01)
[regex] line_items[1].description: forbidden pattern /(?i)\b(sorry|as an ai)\b/ matched "Sorry"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to notice. Every gate runs, so the verdict lists &lt;strong&gt;every&lt;/strong&gt; violation, not just the first. And the output is either accepted or rejected with reasons, which is exactly what you need to decide whether to accept the answer, retry it, or send it to a person.&lt;/p&gt;

&lt;p&gt;That README example is itself run as a test on every build, so the docs can't drift from the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The gates
&lt;/h2&gt;

&lt;p&gt;A contract composes gates. Each one checks something you can write down as an invariant:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Gate&lt;/th&gt;
&lt;th&gt;What it checks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SchemaGate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;JSON Schema 2020-12. The schema itself is validated against the meta-schema when the gate is built.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TypeGate&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Strict binding to a Java type: unknown properties, null primitives, &lt;code&gt;"2"&lt;/code&gt; for &lt;code&gt;2&lt;/code&gt;, floats for ints all fail.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MathGate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cross-field arithmetic with an explicit tolerance: sums, products, ranges, custom rules, and rounding-aware sums for printed figures.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ProvenanceGate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Each required value carries evidence, &lt;code&gt;{value, source_span: {page, text}}&lt;/code&gt;, and the quote must literally occur on the page it names.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;RegexGate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Text rules per field: required patterns, forbidden patterns, ISO dates, ISO 4217 currency codes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;BudgetGate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ceilings on cost, latency and output tokens for the call.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Before any gate runs, output that is not exactly one JSON value is rejected: prose around a code fence, trailing notes, duplicate keys and empty output included.&lt;/p&gt;

&lt;p&gt;The gate that does the most work is &lt;code&gt;ProvenanceGate&lt;/code&gt;. Asking the model to return a quote alongside each value turns "I hope this number is right" into "show me where you read it", and the check is just a string search over the source text. No embeddings, no second model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fail closed, everywhere
&lt;/h2&gt;

&lt;p&gt;The most dangerous guardrail failure is a silent pass, because a wrong output then &lt;em&gt;looks&lt;/em&gt; validated. So outputgate treats everything ambiguous as a rejection: a missing or null value, a gate that throws, a gate configured with no rules, grounding with no source document, unknown token usage when a cost ceiling is set.&lt;/p&gt;

&lt;p&gt;That rule came from a real bug. Mutation testing (PIT) on a core with 95% line coverage showed that 23% of injected bugs went unnoticed. One of the real ones: a schema typo like &lt;code&gt;"required": "vendor"&lt;/code&gt; (a string, not an array) was silently ignored by the validator, so &lt;code&gt;SchemaGate&lt;/code&gt; &lt;strong&gt;passed every output&lt;/strong&gt;. The fix was to validate each schema against the bundled 2020-12 meta-schema when the gate is built. The mutation score went from 77.7% to 95.4%. Coverage told me which lines ran; mutation testing told me whether the tests would notice a bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  The benchmark: 120 real documents, 4 models
&lt;/h2&gt;

&lt;p&gt;Claims about guardrails are cheap, so I measured. I needed a dataset that is public, messy and &lt;em&gt;checkable&lt;/em&gt;, where totals must add up and the right answer is independently known. SEC 10-Q balance sheets fit: every filing ships official XBRL figures to compare against.&lt;/p&gt;

&lt;p&gt;Four models (three hosted Gemini models and &lt;code&gt;qwen2.5:14b&lt;/code&gt; running locally on Ollama) extracted 60 company balance sheets. The contract was then &lt;strong&gt;frozen&lt;/strong&gt; and run on 60 different companies, so the second set is a genuine held-out test. Each accepted answer was compared with the filing's XBRL values.&lt;/p&gt;

&lt;p&gt;Then each run got 50 corrupted copies of accepted answers, one seeded defect each: a dropped line, swapped digits, an invented quote, a wrong page, a bare value, a non-ISO date, an amount as text, a code fence, a trailing note and so on. Same answers scored three ways:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Naive parse&lt;/th&gt;
&lt;th&gt;JSON Schema only&lt;/th&gt;
&lt;th&gt;Full contract&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Seeded defects caught per run&lt;/td&gt;
&lt;td&gt;4/50&lt;/td&gt;
&lt;td&gt;20/50&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;50/50&lt;/strong&gt; in all 8 runs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Real answers accepted but wrong against the filing (of 452 with ground truth)&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;0 in-sample, 1 held out&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To be clear about what that means: the defect kinds were written knowing what the gates check, so the first row shows that each gate fires on the error it targets, not how often models make that error. The second row is the one about real model behaviour.&lt;/p&gt;

&lt;p&gt;The balance sheets are only the eval set. The same contract shape applies to anything with a structure and rules: extracted form fields, lab results, contract clauses, tool-call arguments, classification output with a justification.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding 1: the retry trap
&lt;/h2&gt;

&lt;p&gt;This was the result that surprised me most.&lt;/p&gt;

&lt;p&gt;A common pattern for structured output is &lt;em&gt;validate, send the errors back, ask again&lt;/em&gt;. In the benchmark, one model's line items added up to 78,902 while the document printed a total of 70,402. The retry was told the sum didn't match.&lt;/p&gt;

&lt;p&gt;It left the lines alone and &lt;strong&gt;changed the total to 78,902&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Schema: pass. Arithmetic: pass. The answer now agrees with itself and is wrong. outputgate rejected it because every value carries a quote, and 78,902 is not the number its own quote prints (&lt;code&gt;numbersMatchQuotes()&lt;/code&gt;). That happened in 3 of the 12 retries, and the quote check caught all 3.&lt;/p&gt;

&lt;p&gt;The takeaway generalises well beyond finance: &lt;strong&gt;a retry loop will game any validator that only checks the output against itself.&lt;/strong&gt; Retrying is only safe when at least one check is tied to the source.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding 2: every quote was real, and the answer was still wrong
&lt;/h2&gt;

&lt;p&gt;The one wrong answer that got through the frozen contract was Corteva's held-out filing. The page prints two near-identical balance sheets, the parent company's and then its subsidiary's. Same labels, similar numbers. The local model took one block from each.&lt;/p&gt;

&lt;p&gt;Every quote was on the page. Every sum added up. The contract couldn't tell.&lt;/p&gt;

&lt;p&gt;Proving a value is &lt;em&gt;in&lt;/em&gt; the document is not the same as proving it came from the &lt;em&gt;right part&lt;/em&gt; of the document. So I added regions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Region&lt;/span&gt; &lt;span class="no"&gt;FIRST_STATEMENT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="nc"&gt;Region&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fromDocumentStart&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;through&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"(?i)^liabilities and equity\\b"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;ProvenanceGate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;require&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"period_end"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"total_assets"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"total_liabilities_and_equity"&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;groundedIgnoringWhitespace&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;numbersMatchQuotes&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;within&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;FIRST_STATEMENT&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"period_end"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"total_assets"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"total_liabilities_and_equity"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Re-scoring every recorded run offline, that check rejects Corteva and changes no other verdict, and the seeded defects are still 50 of 50. But I wrote it &lt;em&gt;after&lt;/em&gt; seeing the failure, so it is a fix, not a held-out result, and the published held-out table still shows the miss. If you build guardrails, keep that distinction honest; it is the easiest number to inflate.&lt;/p&gt;

&lt;p&gt;Any document that repeats its labels sets the same trap: a lab report with prior results next to current ones, a contract with an amendment, a form with a summary page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding 3: most early failures were mine, not the model's
&lt;/h2&gt;

&lt;p&gt;Before the balance sheets, I tried scanned public invoices. The first local run passed &lt;strong&gt;1 of 20&lt;/strong&gt;. It was tempting to blame the model. Classifying every rejection told a different story:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;4 real model errors caught.&lt;/strong&gt; It computed a 15% commission and &lt;em&gt;invented a quote&lt;/em&gt; for the result, though the document only prints "15%".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1 document problem.&lt;/strong&gt; A garbled PDF text layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;14 problems on my side.&lt;/strong&gt; The prompt never said an ID field was text, so it came back as a number. Quotes were stitched across table rows. Spacing differed from the PDF's extracted text.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And one infrastructure bug worth knowing about: Ollama's default context in that setup was 4,096 tokens. A ~12k-token prompt was silently cut to about 2,050 tokens, &lt;strong&gt;dropping the start&lt;/strong&gt;, which is where the instructions were. No error. A model "ignoring instructions" may never have seen them. Set limits explicitly and log what was applied, not what was requested.&lt;/p&gt;

&lt;p&gt;After fixing my side (and constraining generation with Ollama's structured-output format, while still judging every answer), the same model went from 1 to 13 of 20 without loosening a single gate.&lt;/p&gt;

&lt;p&gt;Lesson: a low pass rate needs a root cause per violation (model, document, or contract) before it means anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  PDF and OCR text without fuzzy matching
&lt;/h2&gt;

&lt;p&gt;Grounding compares a quote with extracted text, and PDF or OCR text differs from what a model types in ways that carry no meaning: line breaks, ligatures (&lt;code&gt;ﬁ&lt;/code&gt;), thousands separators, curly quotes, a currency sign present or absent. The obvious fix, fuzzy matching, is also how a misread digit slips through.&lt;/p&gt;

&lt;p&gt;So each tolerance is a named, opt-in fold that applies to the quote and the document alike: &lt;code&gt;groundedIgnoringWhitespace()&lt;/code&gt;, &lt;code&gt;ignoringCurrencySigns()&lt;/code&gt;, &lt;code&gt;ignoringTypography()&lt;/code&gt;, &lt;code&gt;joiningHyphenatedLines()&lt;/code&gt;, &lt;code&gt;ignoringDigitGrouping()&lt;/code&gt;. None of them ever lets one letter or digit match another. &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;O&lt;/code&gt; are still different characters. When OCR is too poor for faithful quotes to pass, fix the extraction rather than loosen the contract: a rejection costs a retry, an accepted wrong value costs more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using it
&lt;/h2&gt;

&lt;p&gt;Add the core module and, optionally, the JUnit module and an adapter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"io.github.ravismash:outputgate-core:0.1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;testImplementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"io.github.ravismash:outputgate-junit:0.1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// implementation("io.github.ravismash:outputgate-spring-ai:0.1.0")&lt;/span&gt;
    &lt;span class="c1"&gt;// implementation("io.github.ravismash:outputgate-langchain4j:0.1.0")&lt;/span&gt;
    &lt;span class="c1"&gt;// implementation("io.github.ravismash:outputgate-ollama:0.1.0")&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In tests, assert on recorded outputs and run golden sets (JSONL, one recorded answer per line):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;LlmAssert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;passes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;LlmAssert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;isRejectedBy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;byGate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"math"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;GoldenRunner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;withContext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"golden/extractions.jsonl"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// also writes a self-contained HTML report&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertPassRateAtLeast&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At runtime, the same contract decides what to do with a live answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;LlmOutput&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;complete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Verdict&lt;/span&gt; &lt;span class="n"&gt;verdict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;evaluate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;EvalContext&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;withSource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few design choices that matter in real builds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Core depends only on Jackson and slf4j-api.&lt;/strong&gt; The JSON Schema validator is shaded and relocated, because Spring AI 1.x and 2.x pull newer, incompatible versions of it, and a guardrail that throws &lt;code&gt;NoClassDefFoundError&lt;/code&gt; in your build is no guardrail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adapters are tiny.&lt;/strong&gt; The Spring AI and LangChain4j clients are one class each, under 60 lines, and CI replays a real recorded model answer through each framework's own client on several framework versions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tokens aren't money, and unknown isn't zero.&lt;/strong&gt; Cost comes from explicit per-million-token pricing. A provider that reports no usage means &lt;em&gt;unknown cost&lt;/em&gt;, which fails a cost ceiling rather than passing as free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contracts are thread-safe.&lt;/strong&gt; Build one, share it across parallel tests or a server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's also a Python/Pydantic v2 port of the grounding and arithmetic checks in the repo; it gives the same verdict as the Java library on all 480 recorded benchmark answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it doesn't do
&lt;/h2&gt;

&lt;p&gt;outputgate checks what can be written as an invariant: shape, arithmetic, provenance, patterns, budgets. It is not an LLM-as-judge and doesn't score tone, relevance or toxicity. It's meant to sit &lt;em&gt;next to&lt;/em&gt; a judge, handling the part that should never depend on another model's opinion. It also doesn't manage prompts or run retries for you; it tells you, deterministically, whether an answer can be trusted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A schema checks shape, not truth.&lt;/strong&gt; Add cross-field rules for anything that must add up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask for evidence and verify it in code.&lt;/strong&gt; A value plus a quote, checked literally against the source, catches invented and rewritten numbers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tie at least one check to the source before you retry.&lt;/strong&gt; Otherwise the retry loop learns to satisfy the validator instead of the document.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"In the document" isn't "from the right place".&lt;/strong&gt; Scope quotes when a document repeats its labels.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fail closed and measure your tests with mutation testing.&lt;/strong&gt; Silent passes are the worst failure a guardrail can have.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Classify every rejection before you blame the model.&lt;/strong&gt; Mine were mostly prompt and pipeline bugs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code, every benchmark run, and a write-up of each failure are public:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/ravismash/outputgate" rel="noopener noreferrer"&gt;https://github.com/ravismash/outputgate&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Every failure, case by case: &lt;a href="https://ravismash.github.io/outputgate/sec-10q-llm-extraction-failures.html" rel="noopener noreferrer"&gt;https://ravismash.github.io/outputgate/sec-10q-llm-extraction-failures.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Maven Central: &lt;code&gt;io.github.ravismash:outputgate-core:0.1.0&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your model quoted the right label from the wrong table, would your checks notice? I'd like to hear how you validate structured output in production, and what broke.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>java</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
