<?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: Zeeshan Sarwar</title>
    <description>The latest articles on DEV Community by Zeeshan Sarwar (@zeeshan_sarwar_12e2bcbdab).</description>
    <link>https://dev.to/zeeshan_sarwar_12e2bcbdab</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%2F4264702%2F153c5ed9-c4f0-4192-93ce-bf60c6ce4725.jpg</url>
      <title>DEV Community: Zeeshan Sarwar</title>
      <link>https://dev.to/zeeshan_sarwar_12e2bcbdab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zeeshan_sarwar_12e2bcbdab"/>
    <language>en</language>
    <item>
      <title>Getting reliable structured output from LLMs in Go</title>
      <dc:creator>Zeeshan Sarwar</dc:creator>
      <pubDate>Wed, 25 Nov 2026 12:17:42 +0000</pubDate>
      <link>https://dev.to/zeeshan_sarwar_12e2bcbdab/getting-reliable-structured-output-from-llms-in-go-509c</link>
      <guid>https://dev.to/zeeshan_sarwar_12e2bcbdab/getting-reliable-structured-output-from-llms-in-go-509c</guid>
      <description>&lt;p&gt;Every LLM feature I've built in Go eventually needs the same thing: &lt;strong&gt;a typed value, not a string&lt;/strong&gt;. A triage bot needs &lt;code&gt;Severity&lt;/code&gt; to be one of four values. An invoice parser needs &lt;code&gt;Total&lt;/code&gt; to be a number. A scheduler needs &lt;code&gt;End&lt;/code&gt; to come after &lt;code&gt;Start&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The usual approach looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s2"&gt;"\nRespond ONLY with JSON like {...}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="nc"&gt;Ticket&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Unmarshal&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="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// retry? log? give up?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works in the demo and fails in production in boring, predictable ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The model wraps the JSON in prose or Markdown fences.&lt;/li&gt;
&lt;li&gt;It returns &lt;code&gt;"severity": "URGENT"&lt;/code&gt; when you only handle &lt;code&gt;low|medium|high|critical&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It adds fields you didn't ask for, or leaves out ones you need.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;json.Unmarshal&lt;/code&gt; happily accepts all of that, so the bad value flows into your system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Respond with EXACTLY this format" is a hope, not a guarantee. I wanted the guarantee to live &lt;strong&gt;in code&lt;/strong&gt;, so I wrote &lt;a href="https://github.com/shani8615/go-structllm" rel="noopener noreferrer"&gt;go-structllm&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea: the struct is the contract
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Ticket&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Severity&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;   &lt;span class="sb"&gt;`json:"severity" enum:"low,medium,high,critical" desc:"How urgent it is"`&lt;/span&gt;
    &lt;span class="nc"&gt;Summary&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;   &lt;span class="sb"&gt;`json:"summary"  minLen:"1" maxLen:"200"`&lt;/span&gt;
    &lt;span class="nc"&gt;Tags&lt;/span&gt;     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="sb"&gt;`json:"tags"     maxItems:"5" pattern:"^[a-z-]+$"`&lt;/span&gt;
    &lt;span class="nc"&gt;Owner&lt;/span&gt;    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;  &lt;span class="sb"&gt;`json:"owner"`&lt;/span&gt;   &lt;span class="c1"&gt;// pointer = optional and nullable&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"OPENAI_API_KEY"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"gpt-4o-mini"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;structllm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Extract&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Ticket&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Triage this alert: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get back a &lt;code&gt;Ticket&lt;/code&gt; that &lt;strong&gt;passed validation&lt;/strong&gt;, or an error. Never a half-parsed value.&lt;/p&gt;

&lt;p&gt;Under the hood, &lt;code&gt;Extract&lt;/code&gt; does four things.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Build a JSON Schema from the struct
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;structllm.SchemaFor[Ticket]()&lt;/code&gt; reads the field types and tags and produces a JSON Schema: types, required fields, enums, lengths, item counts, numeric bounds, patterns, and &lt;code&gt;date-time&lt;/code&gt; for &lt;code&gt;time.Time&lt;/code&gt;. Properties keep their declaration order, so the prompt is the same on every run.&lt;/p&gt;

&lt;p&gt;The schema goes to the model twice: in the system prompt, and as &lt;code&gt;response_format: json_schema&lt;/code&gt; for providers that support constrained decoding.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Never trust the reply
&lt;/h3&gt;

&lt;p&gt;The reply is parsed leniently, so JSON inside code fences or prose is found. Then it is validated strictly against the schema, including fields the model invented.&lt;/p&gt;

&lt;p&gt;If your type needs rules a schema can't express, add a method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Window&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Start&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt; &lt;span class="sb"&gt;`json:"start"`&lt;/span&gt;
    &lt;span class="nc"&gt;End&lt;/span&gt;   &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt; &lt;span class="sb"&gt;`json:"end"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="nc"&gt;Window&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;End&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;After&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Start&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;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"end must be after start"&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="kc"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Let the model fix its own mistakes
&lt;/h3&gt;

&lt;p&gt;This is the part that makes the difference. When validation fails, go-structllm doesn't just retry. It sends the model &lt;strong&gt;exactly what was wrong&lt;/strong&gt;, with JSON paths:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your previous response was not valid. Fix these problems and reply with only the corrected JSON:
- $.eta: is not an allowed field
- $.severity: must be one of [low, medium, high, critical]
- $.tags[0]: must match pattern ^[a-z-]+$
- $.tags[1]: must match pattern ^[a-z-]+$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Models are very good at fixing a specific list of problems. This repair loop runs at most &lt;code&gt;MaxRepairs&lt;/code&gt; times (default 2), so the cost is bounded.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgdwnw8s7g8ec1bejdg4t.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgdwnw8s7g8ec1bejdg4t.gif" alt="The repair loop in action" width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Return a T, or a precise error
&lt;/h3&gt;

&lt;p&gt;If the reply is still invalid after the repairs, you get an &lt;code&gt;*ExhaustedError&lt;/code&gt; that wraps the last &lt;code&gt;*ValidationError&lt;/code&gt;, with every issue and the raw reply for debugging.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;structllm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Extract&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Ticket&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;structllm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"You are a precise incident triage assistant."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;structllm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MaxRepairs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;structllm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OnAttempt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;structllm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"llm_attempt"&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="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&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="nc"&gt;Issues&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Any model, one interface
&lt;/h2&gt;

&lt;p&gt;The model is a one-method interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&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;The included &lt;code&gt;openai&lt;/code&gt; package speaks the OpenAI chat-completions protocol. That protocol is also spoken by Gemini, Azure OpenAI, Ollama, vLLM, LM Studio and Groq, so switching providers is one struct literal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Local model, no API key&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;BaseURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"http://localhost:11434/v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"llama3.1"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Google Gemini (free tier key from AI Studio)&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;BaseURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://generativelanguage.googleapis.com/v1beta/openai"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;APIKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"GEMINI_API_KEY"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="s2"&gt;"gemini-3.5-flash"&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;For anything else, wrap your SDK in a &lt;code&gt;structllm.ClientFunc&lt;/code&gt;. It also makes testing easy: a scripted fake client is a few lines, and the whole library has &lt;strong&gt;no dependencies outside the standard library&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What broke when I tested against a real model
&lt;/h2&gt;

&lt;p&gt;The unit tests covered everything against a fake server. They passed, and they still weren't enough. A fake server proves your code matches &lt;strong&gt;your understanding&lt;/strong&gt; of the API, not the API itself. So before announcing anything, I ran the library against real Gemini.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The good news:&lt;/strong&gt; with the default schema mode, Gemini returned a valid &lt;code&gt;Ticket&lt;/code&gt; on the first attempt: a correct enum value, a summary under the length limit, and three tags matching &lt;code&gt;^[a-z-]+$&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The less good news&lt;/strong&gt; came from the error paths:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The first model name I tried had been retired.&lt;/strong&gt; Gemini answered with a 404, but my error said only &lt;code&gt;HTTP 404: Not Found&lt;/code&gt;. The useful message, &lt;em&gt;"This model … is no longer available to new users. Please update your code to use …"&lt;/em&gt;, was lost.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The next model was overloaded.&lt;/strong&gt; A 503, &lt;em&gt;"currently experiencing high demand … try again later"&lt;/em&gt;, failed the whole call on the first try.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The root cause of the first was a small surprise. OpenAI returns errors as &lt;code&gt;{"error": {...}}&lt;/code&gt;, but Gemini's OpenAI-compatible endpoint returns them &lt;strong&gt;wrapped in an array&lt;/strong&gt;: &lt;code&gt;[{"error": {...}}]&lt;/code&gt;. My client only understood the first shape.&lt;/p&gt;

&lt;p&gt;Both are fixed in v0.2.0:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Errors keep the provider's real message in both shapes. The exact Gemini messages are &lt;strong&gt;pinned as regression tests&lt;/strong&gt;, so this can't quietly come back.&lt;/li&gt;
&lt;li&gt;Rate limits (429), server errors (5xx) and network failures are &lt;strong&gt;retried with exponential backoff and jitter&lt;/strong&gt;. A &lt;code&gt;Retry-After&lt;/code&gt; header is honoured, but the client won't wait absurdly long (30 s by default). Everything is configurable, or you can turn it off with &lt;code&gt;DisableRetries&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The lesson I keep re-learning: &lt;strong&gt;"compatible" APIs are compatible on the happy path.&lt;/strong&gt; Test the error paths against the real thing before you ship.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest comparison
&lt;/h2&gt;

&lt;p&gt;I'm not the first to do this in Go. &lt;a href="https://github.com/jxnl/instructor-go" rel="noopener noreferrer"&gt;instructor-go&lt;/a&gt; brings the Instructor approach to Go too, and it's worth a look. Choose based on what you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;go-structllm:&lt;/strong&gt; zero dependencies, any model behind a one-method interface (including local models), &lt;code&gt;Validate()&lt;/code&gt; business rules fed back to the model, exact JSON-path errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;instructor-go:&lt;/strong&gt; built-in clients for OpenAI, Anthropic, Gemini and Cohere, and streaming of partial objects, which go-structllm doesn't do yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Part of a small toolkit
&lt;/h2&gt;

&lt;p&gt;go-structllm is one of a few dependency-free Go libraries I've published for building LLM-backed services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/shani8615/go-ssekit" rel="noopener noreferrer"&gt;go-ssekit&lt;/a&gt;&lt;/strong&gt;: stream LLM tokens to the browser with Server-Sent Events, with a broker that replays missed events on reconnect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/shani8615/go-redact" rel="noopener noreferrer"&gt;go-redact&lt;/a&gt;&lt;/strong&gt;: a &lt;code&gt;log/slog&lt;/code&gt; handler that scrubs API keys, JWTs, card numbers and emails from your logs, including from prompts and error messages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/shani8615/go-resilience" rel="noopener noreferrer"&gt;go-resilience&lt;/a&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;a href="https://github.com/shani8615/go-httpkit" rel="noopener noreferrer"&gt;go-httpkit&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="https://github.com/shani8615/go-outbox" rel="noopener noreferrer"&gt;go-outbox&lt;/a&gt;&lt;/strong&gt;: retries and circuit breakers, idempotent HTTP APIs, and a transactional outbox.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/shani8615/go-microservices-kit" rel="noopener noreferrer"&gt;go-microservices-kit&lt;/a&gt;&lt;/strong&gt;: a reference project that wires several of them together.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go get github.com/shani8615/go-structllm@latest
git clone https://github.com/shani8615/go-structllm &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; go-structllm
go run ./examples/demo   &lt;span class="c"&gt;# no API key needed: watch the repair loop&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Issues, ideas and pull requests are very welcome, especially reports from providers I haven't tested yet (Groq, vLLM, LM Studio). If you've built LLM features in Go, I'd love to hear how you handle structured output today.&lt;/p&gt;

</description>
      <category>go</category>
      <category>llm</category>
      <category>ai</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
