implement-structured-outputs
Installation
SKILL.md
Implement Structured Outputs
Use this skill when integrating LLM responses into application code to guarantee schema adherence and eliminate parsing errors.
Core Rules
- No Regex Parsing: Never use regex to extract JSON from raw LLM output — this is strictly prohibited in production backend pipelines
- Constrained Decoding First: Use provider-native constrained decoding (
response_format: { type: "json_schema", strict: true }for OpenAI/Gemini; XGrammar, Outlines, or SGLang grammar constraints for self-hosted inference) — this guarantees JSON syntax compliance at the token generation layer - Contract-First Schema DSL: Define schemas once in Zod or Pydantic v2 with
strict: true/additionalProperties: false; consider BAML for cross-language type-safe prompt functions; export to JSON Schema 2020-12 for provider compatibility - Dual-Layer Runtime Validation: After constrained decoding, parse through the application-layer validator (
schema.safeParse()/TypeAdapter.validate_json()) to enforce business invariants, enum bounds, and cross-field rules not expressible in JSON Schema - Bounded Repair Loops: On validation failure, inject the exact schema violation diff into a one-turn repair prompt; limit to maximum 2 retry attempts before returning a structured domain error — never loop indefinitely
- Format Tax Mitigation: Include a
thought_processorreasoningfield in the schema before final output fields to preserve model reasoning space under grammar constraints; strip this field before returning to callers - Centralized Gateway Routing: Route all LLM structured output requests through a centralized LLM gateway responsible for token budgeting, provider fallback, rate limiting, and prompt injection filtering