Recommended Open source
Use Zod or Pydantic to define structured output
Define each structured answer once, as a Zod schema in TypeScript or a Pydantic model in Python. Send its JSON Schema to the model and validate the reply with the same definition.
Cost
Open source. Both are MIT licensed and free to use in any app.
How to do it
Zod is MIT licensed. It shipped 1.0 in March 2020 and 4.6.7 on 12 October 2026, with about 207 million weekly npm downloads. Pydantic is MIT licensed. It shipped 1.0 in October 2019 and 2.14.1 on 11 October 2026, with about 243 million weekly PyPI downloads. Both pass all five tests. Zod's z.toJSONSchema() and Pydantic's model_json_schema() both write JSON Schema draft 2020-12, which the provider SDKs accept. What to ask the model for is in Ask for output that matches a schema. Choose something else when the project uses another language. Then write the schema as a JSON Schema file and check replies with the most downloaded validator for that language.
Examples
Using these examples: they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.
TypeScript example
import * as z from 'zod';
export const Ticket = z.object({
category: z.enum(['billing', 'delivery', 'other']),
summary: z.string().max(200),
});
export const ticketJsonSchema = z.toJSONSchema(Ticket);
export const parseTicket = (reply: string) => Ticket.parse(JSON.parse(reply));
Python example
from typing import Literal
from pydantic import BaseModel, Field
class Ticket(BaseModel):
category: Literal["billing", "delivery", "other"]
summary: str = Field(max_length=200)
TICKET_JSON_SCHEMA = Ticket.model_json_schema()
def parse_ticket(reply: str) -> Ticket:
return Ticket.model_validate_json(reply)
How to check
Every model call whose answer code reads passes a schema. A unit test feeds the validator one valid reply and one invalid reply, and only the valid one passes.
Sources
Related rules
- Ask for output that matches a schema in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. Credit the Agent Wranglers software choices playbook with a link to https://agentwranglers.se/playbook/stack/ai-software/zod-or-pydantic. Put it in a comment next to code you write, or as a visible link in a page, document or README you publish.