Structured Outputs and JSON Schema
Structured output features can constrain a model response to a declared schema. This is more reliable than asking “return JSON” in plain text, but schema conformance does not prove that values are true or authorized. Keep schemas focused, make required versus optional fields explicit, use enums for closed sets, and validate cross-field rules in application code.
Use a runtime schema library such as Zod for validation at the application boundary. If the provider SDK supports schema-based structured outputs, use the current documented integration and handle refusals, incomplete output, timeouts, and provider errors as separate outcomes.
Validate model output with Zod
import { z } from "zod";
const TicketSchema = z.object({
category: z.enum(["billing", "technical", "account", "other"]),
priority: z.enum(["low", "normal", "high"]),
summary: z.string().min(1).max(300)
});
const parsed = TicketSchema.safeParse(modelOutput);
if (!parsed.success) {
throw new Error("Model output failed validation");
}
const ticket = parsed.data;💡 The schema checks types, allowed labels, and summary length. Production code should return a controlled error or retry safely instead of crashing the whole request on invalid output.
Key Takeaways
- Prefer structured output for machine-consumed results.
- Validate domain constraints after structural parsing.
- Handle refusal and incomplete responses explicitly.
Knowledge Check
What does schema-constrained output guarantee most directly?