/major-ai-skills:llm-json-contract-checkfrom major-ai-skills
Validate AI-generated JSON against schema and business rules, separating refusals, truncation, parse failures, schema violations, and domain invariant breaches. Use before any tool call, DB write, or external side effect consumes model output.
Ships in major-ai-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Treat every model string as untrusted input. Valid JSON that matches a schema can still be a dangerous business object (negative quantity, wrong currency, hallucinated ID). Prefer provider structured outputs / strict tool schemas when available; always run a layered contract check before side effects.
Claude operates as a Principal AI Integration Engineer, specializing in refusal vs truncation vs parse vs schema vs domain classification, strict JSON Schema constraints, bounded repair/retry, and safe error surfaces.
┌─────────────────────────────────────────────────────────────┐
│ JSON Contract Pipeline │
│ │
│ 0. Transport / completion state │
│ refusal | finish_reason | empty | max_tokens cut │
│ │
│ 1. Extract candidate │
│ direct JSON → strip fences → extract object/array │
│ │
│ 2. Parse syntax │
│ JSON.parse / json.loads (no regex “JSON”) │
│ │
│ 3. Schema validate │
│ app dialect: JSON Schema / Zod / Pydantic / AJV │
│ │
│ 4. Domain invariants │
│ cross-field rules, ID formats, money, ACL, enums │
│ │
│ 5. Side-effect gate │
│ only accept → tool/DB/API; else retry or human path │
└─────────────────────────────────────────────────────────────┘
Use when
@ai-evaluation-dataset).Do not use when
null ≠ "" ≠ [] — document which the API means.response_format: {type:"json_schema", json_schema:{strict:true,...}} or strict function/tool parameters; Anthropic/Gemini tool/structured modes when in stack. JSON mode (json_object) ≠ schema adherence.@agent-injection-boundary-test).Structured Outputs (json_schema + strict) | JSON mode (json_object) | |
|---|---|---|
| Valid JSON | Yes | Yes |
| Schema adherence | Yes (supported subset) | No |
| Refusals | Detect via refusal field | May look like prose/JSON |
Before deserializing: check refusal, then finish_reason / incomplete generation, then parse.
Strict schema habits (OpenAI / Azure docs):
additionalProperties: falserequired (optional values → "type": ["string","null"] unions)pattern, minLength, minimum, maxItems, … on many stacks)—enforce those in domain layer instead$ref limits exist; flatten or stage extraction if the API rejects the schemaIf the model only supports prompt-JSON or weak JSON mode: keep the same layered validator; expect fences/preambles; do not skip schema/domain steps.
| Layer | Signal | Recoverable? | Action |
|---|---|---|---|
| Refusal | refusal set / policy decline | No (as JSON) | Surface refusal; do not parse as contract |
| Truncation | length / max_tokens / unclosed braces | Maybe | Raise token budget; optional close-brackets only for non-critical previews—never for payments |
| Empty | No content (common when reasoning burns budget) | Retry | Increase output budget; simplify schema |
| Fence/prose wrap | ```json … ``` or “Sure, here is…” | Yes | Strip/extract then parse |
| Syntax | Trailing commas, single quotes, None/True | Limited | Deterministic repair or retry; log steps |
| Schema | Missing required, wrong type, extra props, bad enum | Retry | Return validator error paths to model |
| Domain | qty < 0, FX mismatch, unknown SKU, ACL | Rarely via LLM | Reject; fix data/tools—not blind retry loops |
| Semantic | Valid + schema-OK but factually wrong | Eval/human | Not a JSON contract pass |
completion_state → extract → parse → schema_validate → domain_invariants → accept
Stop at first hard failure class; record reject_layer for metrics.
| Case | Expect |
|---|---|
| Happy path | accept |
| Missing required field | schema reject |
additionalProperties / unknown key | schema reject (if closed) |
Wrong type ("1" vs 1) | schema reject (no silent coerce for IDs/money) |
| Unknown enum | schema reject |
| Oversized array / string | domain or post-schema limit |
| Truncated JSON | truncation class — not “schema” |
| Markdown-fenced valid JSON | extract → accept (if enabled) |
| Refusal payload | refusal class |
| Cross-field contradiction (ship date < order date) | domain reject |
| Schema-valid negative quantity | domain reject (classic deliverable) |
validation_attempts in prod—persistent retries mean schema/model mismatch.reject_layer metrics dashboard fields"""LLM JSON contract check: completion → parse → JSON Schema → domain."""
from __future__ import annotations
import json
import re
from dataclasses import dataclass
from enum import Enum
from typing import Any
# pip install jsonschema
from jsonschema import Draft202012Validator
class RejectLayer(str, Enum):
REFUSAL = "refusal"
TRUNCATION = "truncation"
EMPTY = "empty"
PARSE = "parse"
SCHEMA = "schema"
DOMAIN = "domain"
@dataclass
class ContractResult:
ok: bool
data: dict[str, Any] | None
layer: RejectLayer | None
errors: list[str]
raw: str
_FENCE = re.compile(r"```(?:json)?\s*([\s\S]*?)\s*```", re.IGNORECASE)
def extract_json_candidate(text: str) -> str:
text = text.strip()
if not text:
return text
m = _FENCE.search(text)
if m:
return m.group(1).strip()
# first object/array span
for opener, closer in (("{", "}"), ("[", "]")):
start = text.find(opener)
end = text.rfind(closer)
if start != -1 and end != -1 and end > start:
return text[start : end + 1]
return text
def domain_order_invariants(order: dict[str, Any]) -> list[str]:
errs: list[str] = []
qty = order.get("quantity")
if isinstance(qty, (int, float)) and qty <= 0:
errs.append("quantity must be > 0")
currency = order.get("currency")
if currency not in {"USD", "EUR", "GBP"}:
errs.append(f"unsupported currency: {currency!r}")
# never silently rewrite sku / customer_id
return errs
def check_contract(
*,
raw_text: str,
schema: dict[str, Any],
refused: bool = False,
finish_reason: str | None = None,
allow_extract: bool = True,
) -> ContractResult:
if refused:
return ContractResult(False, None, RejectLayer.REFUSAL, ["model refused"], raw_text)
if not (raw_text or "").strip():
return ContractResult(False, None, RejectLayer.EMPTY, ["empty completion"], raw_text)
if finish_reason in {"length", "max_tokens"}:
# still try parse only for diagnostics; classify as truncation if parse fails
pass
candidate = extract_json_candidate(raw_text) if allow_extract else raw_text.strip()
try:
data = json.loads(candidate)
except json.JSONDecodeError as e:
layer = RejectLayer.TRUNCATION if finish_reason in {"length", "max_tokens"} else RejectLayer.PARSE
return ContractResult(False, None, layer, [str(e)], raw_text)
if not isinstance(data, dict):
return ContractResult(False, None, RejectLayer.SCHEMA, ["root must be object"], raw_text)
validator = Draft202012Validator(schema)
schema_errs = [f"{e.json_path}: {e.message}" for e in validator.iter_errors(data)]
if schema_errs:
return ContractResult(False, None, RejectLayer.SCHEMA, schema_errs, raw_text)
domain_errs = domain_order_invariants(data)
if domain_errs:
return ContractResult(False, None, RejectLayer.DOMAIN, domain_errs, raw_text)
return ContractResult(True, data, None, [], raw_text)
ORDER_SCHEMA: dict[str, Any] = {
"type": "object",
"additionalProperties": False,
"required": ["sku", "quantity", "currency"],
"properties": {
"sku": {"type": "string"},
"quantity": {"type": "integer"},
"currency": {"type": "string", "enum": ["USD", "EUR", "GBP"]},
"note": {"type": ["string", "null"]},
},
}
# Syntactically valid JSON with negative quantity → DOMAIN reject, not accept.
import { z } from "zod";
export const OrderSchema = z
.object({
sku: z.string().min(1),
quantity: z.number().int(),
currency: z.enum(["USD", "EUR", "GBP"]),
note: z.string().nullable().optional(),
})
.strict()
.superRefine((val, ctx) => {
if (val.quantity <= 0) {
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "quantity must be > 0", path: ["quantity"] });
}
});
Use the same Zod/Pydantic object to (a) generate provider JSON Schema when supported and (b) validate after parse—one source of truth.
Do (deterministic, logged)
{...} / [...] from brief proseTrue/None on non-critical pathsDon’t
eval() / execute repaired contentPrefer structured outputs so repair is rare; keep repair as defense in depth.
| Signature | Root cause | Fix |
|---|---|---|
| Valid JSON, wrong shape | JSON mode only / weak prompt | Enable strict json_schema or tools |
| API 400 on schema | Unsupported keywords / deep $ref | Simplify schema; move constraints to domain |
| Persistent schema retries | Schema too complex / wrong model | Split stages; simplify enums |
| Empty completions | Token budget spent on reasoning | Raise max_output_tokens; shorten schema |
| Extra fields break clients | Open objects | additionalProperties: false / .strict() |
“Fixed” by coercing "01"→1 | Silent type cast | Keep string IDs; reject wrong types |
| Schema pass, bad business row | Missing domain layer | Add invariants; fixture with qty < 0 |
schema_hash on eval/result rows) next to prompts (@prompt-regression-gate).$.items[2].qty) to the model on repair turns.@llm-json-contract-check — this skill@ai-evaluation-dataset — accept/reject fixtures + hashes@prompt-regression-gate — prompt changes vs frozen contract tests@agent-injection-boundary-test — untrusted model/tool text@ai-pii-redaction-review — raw payload logging@llm-cost-latency-benchmark — cost of validation retriesMANDATORY: Validate in layers (completion → parse → schema → domain) with the app’s real validator. Prefer strict structured outputs. Never silently coerce critical fields. Never treat refusal/truncation as schema failures. Reject schema-valid but business-invalid payloads before side effects. Preserve raw rejects securely; bound retries.
$ref nesting failures with strict schemas; fences as top malformation classWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub alivirgo/major-ai-skills --plugin major-ai-skills412 skills in this plugin
/3d-slicerAutomate 3D Slicer research workflows with MRML, VTK/ITK, DICOM data, and Segment Editor. Use for software workflows, not clinical interpretation./abbreviated-git-statusInspect Git branch, staging, and working-tree changes with short or porcelain status output when a compact repository check is needed./ableton-liveAutomate Ableton Live sessions with Python MIDI Remote Scripts, Max for Live, AbletonOSC, and Link synchronization./act-as-a-personal-tutorExplain a subject step by step, assess prior knowledge, and use short exercises to check understanding before advancing.Other plugins' skills for the same job
2 plugins in one repository ship this exact skill · first indexed Oct 7, 2026