← nopea.tech
ScaneaConsenteaMaskeaBundlesPricing
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 { Maskea, validation } from "@nopeatech/maskea";
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 maskea = new Maskea().use(
  validation({ schema: InvoiceSchema, maxRetries: 2 })
);

const extract = maskea.wrap(
  openai.chat.completions.create.bind(openai.chat.completions)
);

Checked against the shipped @nopeatech/maskea API. Full docs at nopea.tech/maskea/docs.

More in Validation & structured output

Validate without Zod →Retries and validation together →