Structured output

From Archive228/loopkit · View interactive version

Structured output is an AI agent skill from the GitHub repository Archive228/loopkit. Get JSON out of the model reliably. Prefer tool_use with a schema over prompted-JSON, validate on receive, retry on parse fail. Use when downstream code will parse the response.

How to use this skill

Skill file

Structured Output

Prompted JSON — "reply as JSON" in the system prompt — fails on ~2-8% of calls in the wild: stray prose before the object, trailing commas, unescaped quotes, code fences. That failure rate is fine for a demo and fatal for a loop that runs a thousand times.

The hierarchy — use the strongest that fits

  1. Tool use with schema (best) — declare a tool with a JSON Schema for its input. Force the model to call that tool. The API validates the arguments against the schema before you see them. Malformed JSON never leaves the model. Use this whenever the downstream is a real parser.
  1. JSON mode / response_format (good) — where supported. Guarantees a valid JSON object at the top level; does not guarantee schema conformance. Cheap upgrade over prompted-JSON.
  1. Prompted JSON with strict rules (fallback) — for models/tiers without tool use. Say "output ONLY the JSON object, no code fences, no prose", give an example, and validate on receive. Assume ~5% failure and handle it.

The validate-and-retry pattern

call → parse → if fail: retry once with the parse error appended → parse → if fail: hard fail

Schema design — keep it flat

When free JSON is acceptable

Otherwise use tool_use.

Red flags

Loopkit-adjacent

Loopkit's adversarial-verify output is {"passes": bool, "failures": [...]} — that is exactly the shape this skill formalizes. When you compose skills, keep every machine-consumed hop tool_use'd.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.