Lesson · 12 min
Structured output
If code will consume it, do not parse vibes.
Ask for JSON that matches a schema you validate on the server (zod, etc.). If the parse fails, retry once with the validator error, then stop. Never eval model text. Never concatenate model text into SQL.
const parsed = ResultSchema.safeParse(JSON.parse(content));
if (!parsed.success) {
return { ok: false, error: "Could not read the model output" };
}
return { ok: true, data: parsed.data };