Validating & Repairing Output

Validation

Part of: Structured Outputs

JSON mode gave you valid brackets. A schema gave you the right keys. Function calling gave you typed arguments. And the model can still hand you {"age": -7} or {"email": "not-an-email"} with a perfectly straight face. The last line of defense is validation : checking that the values themselves make sense before your pipeline acts on them. What it is Validation is verifying that structured output satisfies your real-world rules, not just that it parses. Syntax is "is this valid JSON?" Schema is "are the keys and types right?" Validation is "are the values actually acceptable?" An age must be positive. An email must contain an "@". A status must be one of a known set. When validation fails, you have a choice: reject the output, or repair it, fix it programmatically, or send it back to the model with the error and ask for a corrected version. How it works A reliable structured-output step has layers, each catching what the last can't: 1. Parse. json.loads, does it parse at all? Wrap in try/except. 2. Check the shape. Are all required keys present, with the right types? 3. Check the values. Apply business rules: ranges, allowed sets, formats, cross-field logic. 4. Repair or reject. If

Challenge: Validate with allowed values