Zod as the contract
Define the output shape as a Zod schema. Generate TypeScript types from it. Pass the JSON schema to the model. One source of truth across API, prompt, and client.
JSON schema enforcement, validator loops with bounded retries, repair prompts on parse failure. Zod schemas mirror your API contract. Type-safe end-to-end — model output to client.
The model returns 'almost-valid' JSON, your parser throws, your endpoint returns a 500, and the user gets nothing. Or worse — the model returns valid-but-wrong JSON, the field is missing, downstream code crashes with an undefined access. Structured output is about making the gap explicit.
Define the output shape as a Zod schema. Generate TypeScript types from it. Pass the JSON schema to the model. One source of truth across API, prompt, and client.
Parse → validate → if invalid, send a repair prompt with the specific error → retry up to N times → fail loud if budget exhausts. No silent corrupted output.
OpenAI's response_format or Anthropic's tool-use, when the model supports it. Constrained decoding when self-hosting on vLLM.
Golden eval includes parse-validity tests + schema-conformance tests + content tests. The CI gate fails if any drops.