Output guard pipeline # raw string schema value no issues issues Issue[] budget spent model generate feedback, attempt process parseJSON 100_000 byte cap process checkSchema preflight, depth 32 process validate Issue[] with paths client accepted value plus trace queue repair maxAttempts budget client exhausted every attempt in trace raw string schema value no issues issues Issue[] budget spent model generate feedback, attempt process parseJSON 100_000 byte cap process checkSchema preflight, depth 32 process validate Issue[] with paths client accepted value plus trace queue repair maxAttempts budget client exhausted every attempt in trace One constrained decoding step # 1 Model emits a logit vector over the full vocabulary (~100k tokens). 2 FSM fsm.valid_tokens(state, tokenizer) lists the tokens that keep an accepting path. 3 Mask mask_logits sets every other logit to float("-inf"). 4 Sample softmax puts all probability on valid tokens. Pick one. 5 Advance fsm.transition(state, tok). Stop when fsm.is_accept(state).
Run the output guard project # Command What it does python3 scripts/project_test.py json-schema-output-guard --init <ws> Creates your workspace once. A fresh starter fails python3 scripts/project_test.py json-schema-output-guard --stage 1 --path <ws> --strict Grades one stage, 1 to 4 python3 scripts/project_test.py json-schema-output-guard --all --solution --strict Grades the reference solution node cli.ts --schema samples/schema.json --attempts samples/attempts.json --output guard.json --html guard.html Recorded repair. Attempt 1 fails at $/confidence
Tasks in code # Task Code Local JSON with an FSM generator = outlines.generate.json(model, Review) Pydantic plus retries, any provider client = instructor.from_anthropic(Anthropic()) Vendor schema mode client.beta.chat.completions.parse(..., response_format=Product) Validate a dict validate_schema(data, schema) returns a list of errorsParse, validate, retry extract_with_retry(text, schema, max_retries=3) Three-branch handler process_model_output(raw, INVOICE_SCHEMA) Guard one raw string guard(raw, schema) returns ok, value, issuesBounded repair await repair(generate, schema, maxAttempts)
Questions #
Should a refusal trigger a retry? No. Log it and return a typed refusal.
Is constrained decoding slower? Often faster. Forced tokens skip generation.
Does it need a big model? No. A 3B model with grammar beats a 70B model with prompts.
Does acceptance prove the answer? No. It does not establish factual correctness.
Why call checkSchema first? A broken schema then cannot spend provider budget. Pydantic schema with Instructor # Copy class Invoice (BaseModel ):
vendor: str
total_usd: float = Field (ge = 0 ) numeric bound
line_items: list[ str ]
client = instructor. from_anthropic ( Anthropic ())
invoice = client. messages. create (
model = "claude-opus-4-7" ,
max_tokens = 1024 ,
response_model = Invoice, retries on validation failure
messages = [{ "role" : "user" , "content" : "<text> " }],
)