惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
H
Heimdal Security Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
AWS News Blog
AWS News Blog
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
S
Securelist
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
博客园 - 叶小钗
J
Java Code Geeks
V
V2EX
博客园 - Franky
Spread Privacy
Spread Privacy
K
Kaspersky official blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
C
CERT Recently Published Vulnerability Notes
Latest news
Latest news
NISL@THU
NISL@THU
罗磊的独立博客
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
V
Visual Studio Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Bulletproofing LLM Structured Output in Python: Healing Retries, Cost Caps, and Drift Detection (Runnable Code)
Nitin Srivas · 2026-05-10 · via DEV Community

I shipped a structured-output endpoint to production in March. The schema was clean, JSON mode was on, the model was GPT-4.1, the eval suite was green. Three weeks in, the on-call channel lit up because a downstream billing job had silently skipped 4,200 records over a weekend.

The output was valid JSON. It just wasn't the JSON we asked for.

That was my last "JSON mode is good enough" deployment. Since then I've shipped four more LLM structured-output systems and the failures keep coming from the same places — and JSON mode catches roughly two of them. This post is the toolkit I wish I had on day one, with runnable Python you can drop into a FastAPI service this afternoon.

The six failure modes JSON mode does not save you from

Two months of incident logs across two enterprise deployments, sorted by frequency:

  1. Silent truncation. max_tokens runs out mid-object. You get parseable JSON for the first 80% of an array, the last item is gone.
  2. Hallucinated keys. Model returns customer_id when the schema says client_id. JSON mode does not check field names against your schema unless you use strict structured output, and even then nested types slip through.
  3. Type coercion. "price": "1,499.00" instead of 1499.00. JSON parser is happy. Your billing job is not.
  4. Semantic drift. Schema-valid output where the values are wrong — wrong customer, wrong amount, wrong country code.
  5. Refusals returning JSON. Safety filter triggers, model returns {"refusal": "I can't help with that"}. Your code parses it as a normal response.
  6. Schema-version desync. You ship a new field, an in-flight worker is still on the old schema, batch fails for two hours until someone notices.

JSON mode catches #1 and #3 sometimes. The other four need real validation, healing, and observability layered on top.

The toolkit

We're building this around four pieces:

  • A strict validator that runs after JSON mode (catches what JSON mode misses).
  • A healing retry loop that feeds the validation error back to the model — not a blind retry.
  • A cost-bounded fallback chain so a bad prompt cannot burn through $400 in tokens.
  • A drift detector that tracks parse compliance and field-distribution shifts over time.

Full file structure:

llm_structured/
├── schemas.py        # Pydantic models with versioning
├── validator.py      # Strict validation beyond JSON mode
├── healer.py         # Healing retry loop
├── budget.py         # Per-request and global cost caps
├── chain.py          # Multi-provider fallback with circuit breaker
├── observability.py  # Metrics + drift detection
└── service.py        # FastAPI endpoint that ties it all together

Enter fullscreen mode Exit fullscreen mode

Install dependencies:

pip install pydantic==2.7.4 openai==1.30.0 anthropic==0.30.0 \
    tenacity==8.3.0 prometheus-client==0.20.0 fastapi==0.111.0 \
    uvicorn==0.30.1 httpx==0.27.0

Enter fullscreen mode Exit fullscreen mode

1. Schemas with versioning baked in

Schema versioning sounds boring until you've had two services on different versions for ninety minutes during a deploy.

# schemas.py
from pydantic import BaseModel, Field, field_validator
from typing import Literal
from decimal import Decimal


class InvoiceLineV2(BaseModel):
    schema_version: Literal["2.0"] = "2.0"
    client_id: str = Field(min_length=3, max_length=64)
    amount: Decimal = Field(gt=0, decimal_places=2)
    currency: Literal["USD", "EUR", "GBP", "INR"]
    invoice_date: str = Field(pattern=r"^\d{4}-\d{2}-\d{2}$")
    line_items: list[str] = Field(min_length=1, max_length=50)
    confidence: float = Field(ge=0.0, le=1.0)

    @field_validator("amount", mode="before")
    @classmethod
    def coerce_amount(cls, v):
        if isinstance(v, str):
            cleaned = v.replace(",", "").replace("$", "").strip()
            return Decimal(cleaned)
        return v


def schema_for_prompt(model: type[BaseModel]) -> dict:
    """Return a JSON-schema dict suitable for OpenAI response_format."""
    return {
        "type": "json_schema",
        "json_schema": {
            "name": model.__name__,
            "schema": model.model_json_schema(),
            "strict": True,
        },
    }

Enter fullscreen mode Exit fullscreen mode

The schema_version field is the key. Every output carries the version that produced it; downstream consumers fail loudly when they see a version they don't understand instead of silently mis-mapping fields.

2. Validation that goes beyond JSON mode

JSON mode + strict: true will catch type errors and missing required fields. It will not catch refusals, won't catch semantic anchors, and won't tell you about partial truncation. So we run a second-pass validator.

# validator.py
from pydantic import BaseModel, ValidationError
import json
import re

REFUSAL_PATTERNS = [
    r"i can'?t help",
    r"i'?m not able to",
    r"as an ai",
    r"i'?m unable to provide",
]


class ValidationResult:
    def __init__(self, ok: bool, value=None, errors=None, raw=None):
        self.ok = ok
        self.value = value
        self.errors = errors or []
        self.raw = raw


def validate(raw: str, model: type[BaseModel]) -> ValidationResult:
    if not raw or not raw.strip():
        return ValidationResult(False, errors=["empty_response"], raw=raw)

    lower = raw.lower()
    for pat in REFUSAL_PATTERNS:
        if re.search(pat, lower):
            return ValidationResult(False, errors=["refusal_detected"], raw=raw)

    try:
        parsed = json.loads(raw)
    except json.JSONDecodeError as e:
        return ValidationResult(False, errors=[f"json_decode: {e}"], raw=raw)

    try:
        instance = model.model_validate(parsed)
    except ValidationError as e:
        return ValidationResult(False, errors=_format_errors(e), raw=raw)

    return ValidationResult(True, value=instance, raw=raw)


def _format_errors(e: ValidationError) -> list[str]:
    out = []
    for err in e.errors():
        loc = ".".join(str(p) for p in err["loc"])
        out.append(f"{loc}: {err['msg']}")
    return out

Enter fullscreen mode Exit fullscreen mode

The _format_errors step matters. If you feed ValidationError.json() back to the model verbatim, you get a wall of stack-trace-looking text the model wastes tokens trying to parse. Plain English errors heal in one round most of the time.

3. Healing retries — not blind retries

A blind retry on the same prompt with temperature=0 gives you the same broken output. The fix is to tell the model what was wrong and ask it to repair that specific output.

# healer.py
from .validator import validate, ValidationResult
from pydantic import BaseModel
from openai import AsyncOpenAI

REPAIR_PROMPT = """The previous response failed validation.

Original schema requirements:
{schema}

Your previous output:
{previous}

Validation errors:
{errors}

Return ONLY corrected JSON matching the schema. Do not explain.
"""


async def heal(
    client: AsyncOpenAI,
    model_name: str,
    user_prompt: str,
    response_model: type[BaseModel],
    max_attempts: int = 3,
) -> ValidationResult:
    history = [{"role": "user", "content": user_prompt}]
    last_raw = ""

    for attempt in range(max_attempts):
        resp = await client.chat.completions.create(
            model=model_name,
            messages=history,
            response_format={"type": "json_object"},
            temperature=0.0,
        )
        last_raw = resp.choices[0].message.content or ""

        result = validate(last_raw, response_model)
        if result.ok:
            return result

        history.append({"role": "assistant", "content": last_raw})
        history.append({
            "role": "user",
            "content": REPAIR_PROMPT.format(
                schema=response_model.model_json_schema(),
                previous=last_raw,
                errors="\n".join(result.errors),
            ),
        })

    return ValidationResult(False, errors=["max_heal_attempts"], raw=last_raw)

Enter fullscreen mode Exit fullscreen mode

Three attempts is the cap I land on most of the time. In incident data from one client, attempt 1 succeeds 87.4% of the time, attempt 2 takes another 9.1%, attempt 3 captures 2.8%, and the remaining 0.7% is genuinely broken (model can't comply, downstream needs a human). Anything past three is just burning tokens.

4. Cost budget that actually caps spend

The 0.7% that fail-loud are also the prompts that recursively spiral. So we cap.

# budget.py
import time
from dataclasses import dataclass
from contextvars import ContextVar


@dataclass
class CostState:
    spent_usd: float = 0.0
    started_at: float = 0.0
    request_cap_usd: float = 0.10
    global_cap_usd_per_min: float = 5.0
    global_window_start: float = 0.0
    global_spent_in_window: float = 0.0


_state: ContextVar[CostState] = ContextVar("_cost_state")


PRICING = {
    "gpt-4.1": (0.00250, 0.01000),
    "gpt-4.1-mini": (0.00015, 0.00060),
    "claude-sonnet-4.5": (0.00300, 0.01500),
}


def estimate(model: str, prompt_tokens: int, completion_tokens: int) -> float:
    in_price, out_price = PRICING.get(model, (0.0, 0.0))
    return (prompt_tokens / 1000) * in_price + (completion_tokens / 1000) * out_price


def charge(model: str, prompt_tokens: int, completion_tokens: int) -> None:
    state = _state.get()
    cost = estimate(model, prompt_tokens, completion_tokens)
    state.spent_usd += cost

    now = time.time()
    if now - state.global_window_start > 60:
        state.global_window_start = now
        state.global_spent_in_window = 0.0
    state.global_spent_in_window += cost

    if state.spent_usd > state.request_cap_usd:
        raise BudgetExceeded(f"per-request cap hit: {state.spent_usd:.4f}")
    if state.global_spent_in_window > state.global_cap_usd_per_min:
        raise BudgetExceeded(
            f"global rate cap hit: {state.global_spent_in_window:.4f}/min"
        )


class BudgetExceeded(Exception):
    pass


def with_budget(request_cap_usd: float = 0.10) -> CostState:
    state = CostState(request_cap_usd=request_cap_usd, started_at=time.time())
    _state.set(state)
    return state

Enter fullscreen mode Exit fullscreen mode

The per-request cap is what saves you from one runaway prompt. The per-minute global cap is what saves you from a bug — like the time I deployed a regex that turned every retrieved doc into a 200KB context, and we caught it because the global cap kicked in at minute three instead of the next billing cycle.

5. Multi-provider fallback with a circuit breaker

If OpenAI returns a 5xx burst, retrying OpenAI is wasted seconds. Fall over to Anthropic, but don't fall back forever — open the circuit, let one probe through every 30 seconds, recover when it succeeds.

# chain.py
import time
from dataclasses import dataclass
from openai import AsyncOpenAI
from anthropic import AsyncAnthropic
from pydantic import BaseModel
from .healer import heal
from .validator import ValidationResult


@dataclass
class Breaker:
    failures: int = 0
    open_until: float = 0.0
    threshold: int = 3
    cooldown: float = 30.0


class FallbackChain:
    def __init__(self, openai_client: AsyncOpenAI, anthropic_client: AsyncAnthropic):
        self.openai = openai_client
        self.anthropic = anthropic_client
        self.breakers = {"openai": Breaker(), "anthropic": Breaker()}

    def _can_call(self, name: str) -> bool:
        return time.time() >= self.breakers[name].open_until

    def _record(self, name: str, ok: bool) -> None:
        b = self.breakers[name]
        if ok:
            b.failures = 0
            b.open_until = 0.0
        else:
            b.failures += 1
            if b.failures >= b.threshold:
                b.open_until = time.time() + b.cooldown

    async def run(
        self, user_prompt: str, model: type[BaseModel]
    ) -> ValidationResult:
        if self._can_call("openai"):
            try:
                result = await heal(self.openai, "gpt-4.1-mini", user_prompt, model)
                self._record("openai", result.ok)
                if result.ok:
                    return result
            except Exception:
                self._record("openai", False)

        if self._can_call("anthropic"):
            try:
                result = await self._call_anthropic(user_prompt, model)
                self._record("anthropic", result.ok)
                return result
            except Exception:
                self._record("anthropic", False)

        return ValidationResult(False, errors=["all_providers_unavailable"])

    async def _call_anthropic(self, prompt: str, model: type[BaseModel]):
        # Anthropic uses tool_use to force structured output
        from .validator import validate
        resp = await self.anthropic.messages.create(
            model="claude-sonnet-4.5",
            max_tokens=2000,
            tools=[{
                "name": model.__name__,
                "description": f"Return a {model.__name__}",
                "input_schema": model.model_json_schema(),
            }],
            tool_choice={"type": "tool", "name": model.__name__},
            messages=[{"role": "user", "content": prompt}],
        )
        import json
        for block in resp.content:
            if block.type == "tool_use":
                return validate(json.dumps(block.input), model)
        return ValidationResult(False, errors=["no_tool_use_block"])

Enter fullscreen mode Exit fullscreen mode

6. Observability — drift detection beyond a parse rate

Most teams stop at parse_compliance_rate and call it observability. That tells you nothing on the day a model upgrade silently shifts your confidence field from a 0.85 mean to 0.62.

# observability.py
from prometheus_client import Counter, Histogram, Gauge
from collections import deque
import statistics

PARSE_OK = Counter("llm_parse_ok_total", "Successful parses", ["model", "schema"])
PARSE_FAIL = Counter(
    "llm_parse_fail_total", "Failed parses", ["model", "schema", "reason"]
)
HEAL_ATTEMPTS = Histogram(
    "llm_heal_attempts", "Attempts to validation success", ["model", "schema"]
)
COST_USD = Counter("llm_cost_usd_total", "Cost in USD", ["model"])

_field_windows: dict[str, deque] = {}
DRIFT_GAUGE = Gauge("llm_field_drift_zscore", "Z-score of field mean", ["field"])


def track_field(field_name: str, value: float, window: int = 1000) -> None:
    if field_name not in _field_windows:
        _field_windows[field_name] = deque(maxlen=window)
    q = _field_windows[field_name]
    q.append(value)
    if len(q) >= 50:
        old = list(q)[: len(q) // 2]
        new = list(q)[len(q) // 2 :]
        if statistics.stdev(old) > 0:
            z = (statistics.mean(new) - statistics.mean(old)) / statistics.stdev(old)
            DRIFT_GAUGE.labels(field=field_name).set(z)

Enter fullscreen mode Exit fullscreen mode

The track_field helper is what catches the silent model-upgrade regression. Wire it to alert when |z| > 2.5 on any field for ten minutes — same pattern I broke down for the LLM evaluation harness in pytest, except that one runs at CI time and this one runs in production.

7. The endpoint that ties it together

# service.py
from fastapi import FastAPI, HTTPException
from openai import AsyncOpenAI
from anthropic import AsyncAnthropic
from .schemas import InvoiceLineV2
from .chain import FallbackChain
from .budget import with_budget, BudgetExceeded
from .observability import PARSE_OK, PARSE_FAIL, HEAL_ATTEMPTS, track_field

app = FastAPI()
chain = FallbackChain(AsyncOpenAI(), AsyncAnthropic())


@app.post("/extract/invoice")
async def extract_invoice(payload: dict):
    text = payload.get("text", "")
    with_budget(request_cap_usd=0.05)

    try:
        result = await chain.run(text, InvoiceLineV2)
    except BudgetExceeded as e:
        raise HTTPException(429, f"cost cap: {e}")

    label = {"model": "gpt-4.1-mini", "schema": "InvoiceLineV2"}
    if not result.ok:
        for reason in result.errors:
            PARSE_FAIL.labels(**label, reason=reason[:32]).inc()
        raise HTTPException(422, {"errors": result.errors, "raw": result.raw})

    PARSE_OK.labels(**label).inc()
    track_field("invoice.confidence", float(result.value.confidence))
    return result.value.model_dump()

Enter fullscreen mode Exit fullscreen mode

Chaos-test before you ship

The whole point of the toolkit is that the bad days behave. Test the bad days on purpose.

# tests/test_chaos.py
import pytest
from unittest.mock import AsyncMock, patch
from llm_structured.service import app
from fastapi.testclient import TestClient

client = TestClient(app)


@pytest.mark.parametrize("bad_response", [
    "I can't help with that request.",
    '{"client_id": "abc", "amount": "not_a_number"}',
    '{"customer_id": "abc", "amount": 100}',
    '{"client_id": "abc", "amount": 100, "currency": "ZZZ"}',
])
def test_chaos_responses(bad_response):
    with patch("openai.AsyncOpenAI") as mock:
        mock.return_value.chat.completions.create = AsyncMock(
            return_value=type("R", (), {
                "choices": [type("C", (), {
                    "message": type("M", (), {"content": bad_response})()
                })()]
            })()
        )
        r = client.post("/extract/invoice", json={"text": "..."})
        assert r.status_code in (422, 429)

Enter fullscreen mode Exit fullscreen mode

The point of the parametrize block isn't coverage. It's to make sure none of these failure modes can crash the service or silently succeed. A green test on this file is the closest thing to a guarantee you get.

What to actually do today

If you have a structured-output endpoint in production right now, do this Monday morning, in this order:

  1. Add the validator from section 2 after JSON mode. You will catch hallucinated keys you didn't know you had.
  2. Wire the per-request cost cap. It's twelve lines of code and it will save you the day a bad prompt loops.
  3. Add track_field to one numerical field with a known distribution. That's your drift canary.

Steps 4 through 6 are the ones that take a sprint. These three take an afternoon, and they cover the failure modes that have hit me on every production rollout.

I'm working on the next post in this cluster — a teardown of the failure-injection harness we use to test these endpoints under realistic chaos before they ship. If that's interesting, follow along.

For teams that want this implemented end-to-end, our work on LLM integration and custom AI agents at Velocity Software Solutions covers exactly this kind of production-hardening; we ship Python services like this one for clients regularly through our Python development practice.

Related production-grade pieces I've written:

External references worth bookmarking: