ANTIGRAVITY LABJP
Articles/Integrations
Integrations/2026-06-27Intermediate

Pass Your Agent's Structured Output Downstream With Schema Validation and Bounded Repair

Before the JSON an Antigravity agent returns flows straight into downstream automation and causes an incident, build a safe boundary with JSON Schema validation and a turn-limited repair loop. Includes the implementation.

Antigravity279integrations18JSON-Schemavalidation4agent-design9

Premium Article

Ask an agent to "return the next article candidates as JSON" and most of the time you get clean JSON. Most of the time. About once a week, a preamble sneaks in, a trailing comma appears, or level comes back as "beginner-intermediate", a value that does not exist.

The problem is that the one broken case flows into downstream automation. I run several sites on my own, with a process that takes the agent's output and assembles MDX. If the JSON parse fails, that whole generation is wasted. Even with a smart model, this incident never reaches zero unless you place validation at the boundary of the output.

Agent output is "untrusted input"

The first mindset to hold is to treat agent output the same as input from outside.

Nobody puts a value from a web form into the database without validating it. Yet generative output somehow tends to get passed downstream unvalidated. Because the output is linguistically fluent, you mistake the structure for correct too. Fluency and correctness are different things. Place a single validation layer at the boundary and the downstream code can be written on the assumption that "only the correct shape arrives."

Make the "shape" a contract with JSON Schema

First, write the shape the downstream expects as a schema. Not vague prose inside the prompt, but a contract a machine can judge.

ARTICLE_PLAN_SCHEMA = {
    "type": "object",
    "required": ["title", "slug", "category", "level", "tags"],
    "additionalProperties": False,
    "properties": {
        "title": {"type": "string", "minLength": 8, "maxLength": 60},
        "slug": {"type": "string", "pattern": "^[a-z0-9-]+$"},
        "category": {"enum": ["agents", "integrations", "tips", "editor"]},
        "level": {"enum": ["beginner", "intermediate", "advanced"]},
        "tags": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 2,
            "maxItems": 6,
        },
    },
}

enum pins level to three values, and pattern keeps periods and uppercase out of the slug. additionalProperties: False matters too: if the model helpfully adds an extra key, it gets rejected. A schema is the tool that promotes the "request" in your prompt into an unbreakable "promise."

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
A concrete layer design that treats agent output as a trust boundary and never passes it downstream unvalidated
Locking type and required fields with JSON Schema, plus a turn-limited repair loop that self-heals broken output
Fallback and logging design that prevents infinite repair and runaway cost on failure
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

Integrations2026-04-25
Antigravity × Obsidian — Growing Code and Knowledge in the Same Place
A practical workflow for connecting the code you write in Antigravity to a growing Obsidian knowledge base. Record architecture decisions, accumulate bug patterns, and bring that context into every new AI session.
Integrations2026-06-27
Rotate Keys Without Stopping an Unattended Agent: An Overlap-Window Design
API keys and tokens are worth rotating on a schedule before they leak. But an unattended agent goes quietly dead the moment auth breaks during the swap. As an indie developer running several sites on autopilot, I lay out an overlap-window design that rotates keys without downtime.
Integrations2026-06-25
A Translated Line Had Quietly Reverted to English — Guarding String Resources an Agent's Refactor Touched
Let an agent tidy your values folder and translated strings can silently revert to the source text. Here is a design and implementation that treats the default locale as the source of truth, reads every other locale as a diff, and blocks only dropped keys, reverted translations, and broken format arguments at pre-commit.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →