AI agents and models Required
Check every model answer against a schema
Parse every model answer your code acts on against a schema before you use it. Reject, retry or fall back when it does not match.
How to do it
Ask for structured output where the provider supports it, and still validate it, for example with a JSON Schema validator, zod or pydantic. Check value ranges and allowed options, not only the shape. Log the answers that fail, without personal data. See also retry a bad answer and then fall back.
Example
const Refund = z.object({ orderId: z.string(), amount: z.number().positive().max(500), reason: z.enum(['damaged', 'late', 'wrong-item']) });
const parseJson = text => { try { return JSON.parse(text); } catch { return undefined; } };
const parsed = Refund.safeParse(parseJson(answer));
if (!parsed.success) return askAPersonInstead(answer);
# Python with pydantic
class Refund(BaseModel):
order_id: str
amount: float = Field(gt=0, le=500)
reason: Literal["damaged", "late", "wrong-item"]
try:
refund = Refund.model_validate_json(answer)
except ValidationError:
return ask_a_person_instead(answer)
How to check
Make a test model return text instead of JSON, then JSON with an unknown option. Both are rejected, and nothing downstream runs.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers security testing playbook: https://agentwranglers.se/playbook/adversarial-testing/ai-agents-and-models/validate-model-output.