Validation & structured output
Force structured JSON output with Zod
validation() re-runs the whole call (not just re-parsing) when the model's output fails your schema -- LLM non-determinism is often fixed by a fresh attempt.
← All recipes
import { Klaro, validation } from "@klaroshield/sdk";
import OpenAI from "openai";
import { z } from "zod";
const openai = new OpenAI();
const InvoiceSchema = z.object({
total: z.number(),
currency: z.string(),
lineItems: z.array(z.string()),
});
const klaro = new Klaro().use(
validation({ schema: InvoiceSchema, maxRetries: 2 })
);
const extract = klaro.wrap(
openai.chat.completions.create.bind(openai.chat.completions)
);Checked against the shipped @klaroshield/sdk API. Full docs at klaro.services/klaroshield/docs.