{"success":true,"name":"never-trust-llm-json-validate-against-a-schema-then-repair-o","description":"Your code parses structured output (JSON/YAML) coming from an LLM","category":"ai-brimind","path":"ai-brimind/never-trust-llm-json-validate-against-a-schema-then-repair-o/SKILL.md","file":"SKILL.md","content":"---\nname: never-trust-llm-json-validate-against-a-schema-then-repair-o\ndescription: \"Your code parses structured output (JSON/YAML) coming from an LLM\"\n---\n\n# Never Trust LLM JSON: Validate Against a Schema, Then Repair or Retry\n\n## When to use\nThe project calls an LLM and expects a machine-readable answer (JSON, YAML, fields), and code downstream will act on those fields directly.\n\n## Example request\nModel wraps the JSON in markdown fences or adds 'Here is your JSON:' → JSON.parse crashes on day one; extraction plus strict parsing is the standard first layer. — Model returns valid JSON but 'files' is a string instead of an array → the code loops over characters; schema validation catches shape, not just syntax. — Model invents an extra field or an enum value that doesn't exist → silent wrong behavior later; an allowlist schema rejects unknown values at the door. — Output fails validation once → one retry with the validation error fed back usually fixes it; the retry-with-feedback loop is a named pattern in LLM engineering.\n\n## Instructions\nAct as a border guard who treats every LLM response as untrusted input from an unreliable stranger, exactly like user input. Before writing any code that consumes model output: 1) Define the expected shape as a real schema (required fields, types, allowed enum values, array vs object) in one place — this schema IS the contract. 2) Extract first: strip markdown fences, preambles, and trailing text before parsing; never feed the raw response straight to the parser. 3) Validate the parsed object against the schema and classify failures: unparseable, wrong shape, or forbidden value — each is a different bug signal. 4) On failure, do one of three real things: retry once with the validation error included in the prompt, fall back to a safe default, or stop loudly with a clear error — never continue with a half-valid object. 5) Check for an existing schema validator in the stack before hand-rolling checks; field-by-field if-statements rot, a declared schema stays readable. If you are unsure what shapes the model actually produces, log a few real responses and look before designing the schema. Only wire the output into real actions once validation is confirmed in place, and explain how the schema stops each failure class — parse errors, shape drift, and invented values — not just the happy case.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #959\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}