---
title: "LLM Structured Output: Zod schemas, validator loops"
description: "JSON schema enforcement on LLM output. Zod schemas mirror the API contract. Validator loops with bounded retries and repair prompts. Type-safe end-to-end."
source: "https://www.kensink.com/llm/structured-output/"
canonical: "https://www.kensink.com/llm/structured-output/"
---
★ Structured Output Direct LLM · no framework 8-week engagement

STRUCTURED OUTPUT · FUNCTION CALLING

# LLM as a typed component, not a chatbot.

JSON schema enforcement, validator loops with bounded retries, repair prompts on parse failure. Zod schemas mirror your API contract. Type-safe end-to-end, from model output to client.

TypeScript Zod OpenAI Anthropic

[Start this engagement →](https://www.kensink.com/contact) [All LLM services →](https://www.kensink.com/llm)

Cycle

8 weeks · schema to ship

Stack

TypeScript · Zod · OpenAI / Anthropic

Output

Schemas + validator loop + repair prompts

Discipline

Output validates or doesn't ship, no maybe

\[WHY THIS EXISTS\]

## JSON.parse on model output is a vulnerability.

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, and downstream code crashes with an undefined access. Structured output is about making the gap explicit.

-   Zod schemas as the single source of truth for both API and prompt
-   Validator loop: parse, validate, repair, retry, all with bounded retries
-   JSON-mode / function-calling on the model when supported
-   Type-safe end-to-end, with TypeScript types derived from the Zod schema

\[HOW WE BUILD IT\]

## Schema first, prompt second.

01

### 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.

02

### Validator loop

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.

03

### Function-calling mode

OpenAI's response\_format or Anthropic's tool-use, when the model supports it. Constrained decoding when self-hosting on vLLM.

04

### Eval coverage

Golden eval includes parse-validity tests + schema-conformance tests + content tests. The CI gate fails if any drops.

\[ OUTCOMES AT HANDOFF \]

## What's live at week eight.

100%

Schema-valid outputs reaching the client

Bounded

Repair-retry budget on every call

One source

Zod schema drives types + prompt

Type-safe

End-to-end from model to UI

\[ALSO WORTH READING\]

## Related LLM engagements.

[

PRODUCTION AGENTS

Production agents

Read the engagement

](https://www.kensink.com/llm/agents)[

MODEL EVALUATION

Model Evaluation

Read the engagement

](https://www.kensink.com/llm/evaluation)[

RAG

RAG architecture

Read the engagement

](https://www.kensink.com/llm/rag)

DIRECT LLM · APPLIED K

## Bring the problem.  
We’ll bring the build.

Eight weeks, fixed price, eval suite at handoff. Direct LLM engineering on top of the K-Framework. Two Q3 slots remain.

[Start this engagement →](https://www.kensink.com/contact) [Read the K-Framework](https://www.kensink.com/k-framework)
