Cortex · OutputCustom validation (
Custom validation (output.validate)
output.validate is a custom validation function for rules the output schema cannot express. When it returns { retry }, the run emits a retry event and sends the reason to the model in the same run, then validates the next output again. The model’s two turns are scripted, the first with a night that has no show; no model is called.
const Order = z.object({ night: z.string(), seats: z.number().int() });
const NIGHTS = ['2026-10-23', '2026-10-24', '2026-10-25'];
const clerk = defineAgent({
id: 'clerk',
instructions: 'Read what the member wants into an order.',
output: {
schema: Order,
validate: ({ data }) =>
NIGHTS.includes(data.night)
? { ok: true }
: { retry: `nothing plays on ${data.night}, only on ${NIGHTS.join(', ')}` },
},
});
const result = await clerk.run('Two seats for Sunday, please.', { llm }).result;Run, attempt by attempt
writes {"data":{"night":"2026-10-26","seats":2}}{
type: 'retry',
kind: 'output',
attempt: 1,
issues: 'nothing plays on 2026-10-26, only on 2026-10-23, 2026-10-24, 2026-10-25',
}Runs in this page on the installed package. The input and the expected output are defined by the example.