short · 3 min
Schema validation
Checking the output's values after it arrives, and sending the error back to the model when they're wrong.
updated 29 Sept 2026
Anthropic's structured outputs page has a line in its list of benefits: "No retries needed for schema violations". And it's true. With a strict schema (structured output), the JSON comes back with the right keys and the right types.
So can we delete our retry code? Let's try it on something small.
We ask for {"name": string, "age": integer} from the text "jason is 25
years old", and our system wants names in capitals. Back comes
{"name": "jason", "age": 25}. It parses. The types match. And it's still
wrong for us. The schema promised the shape, never the values.
Rules the schema can't hold
There are a few kinds of rule like that, and they get harder as we go.
Some a schema could express, but the API won't enforce. As of 2026-09,
Claude's structured outputs don't support number limits like minimum and
maximum, or string length limits. Put them in the schema and we get a
400 error. Anthropic's SDKs work around it: they take the limit out of the
schema they send, write it into the field's description instead ("Must be
at least 100"), and check the reply against our original schema once it
arrives. So the model gets a polite hint, and the real guarantee is the
check in our code.
Some no schema language can say at all. A quote the model claims it found has to actually be in the source document. A generated SQL query has to run against our database. A reply shouldn't say anything objectionable, which might take a second model to judge.
All of these need our own code. In Python that's usually a Pydantic model with validators on its fields, and a failing one raises an error that says what's wrong in plain words.
Hand the error back
That error message is the useful part, because we can show it to the model. Libraries like Instructor and Pydantic AI build a loop around it.
When validation fails, the library adds the model's bad reply to the conversation, then a new user message along the lines of "Please correct the function call; errors encountered:" plus the error, and calls again. The model sees exactly what it got wrong. Instructor calls this reasking, and treats self-correction as nothing more than validation errors with clear messages. The same loop catches replies that aren't valid JSON at all.
Instructor can hand the source document to a validator, so it can confirm
a quote really exists. Pydantic AI's output validators can reach outside,
like running EXPLAIN on a generated query, and raise ModelRetry to
send the model back.
Each retry is a full call, and a longer one, since it carries the bad reply and the error. (Pydantic puts a documentation link in every error message; Instructor ships a helper just to strip it and save tokens.) So the budget should be small. Pydantic AI's default is one retry. When we stream, Pydantic AI also runs output validators on every partial result, so a check that needs the whole object has to wait for the final one.
When a retry can't help
Now say the text was just "jason", no age at all, and the schema demands an integer. What does the error message tell the model? That age is missing. And what can the model do about it? Not much, except invent a number (hallucination). No amount of reasking puts a fact into the input.
Pydantic AI's SQL example handles this nicely. The output can be either a query or an "invalid request" with an explanation, so the model has an honest way to say it can't, and isn't pushed to force a bad query past the validator. That's worth copying: before adding retries, give the model a way out.
Further reading
- Validation and Reasking, Instructor docs. The reask loop with code, including the "jason" example and checking quotes against a source.
- Output, Pydantic AI
docs. Output validators,
ModelRetry, the retry budget, and the SQL example with its way out. - Structured outputs, Anthropic docs. Which schema limits Claude won't enforce, and how the SDKs move them into descriptions and check afterwards.