In this article, you will learn how to get reliable structured outputs from large language models by validating them against a contract and the source text, retrying only when it helps, and constraining generation.
Topics we will cover include:
How to define a strict Pydantic contract and check every validated object against the source it came from.
How to build a bounded retry loop that regenerates only on shape errors and keeps the SDK’s hidden transport retries under control.
How to use constrained decoding with OpenAI’s Responses API without dropping validation, and how to test each failure path with a mocked client.
Structured output is a standard feature now. OpenAI’s Responses API takes a JSON schema or a Pydantic model and promises a response that fits it, vLLM does the same on a local server with xgrammar behind it, and the Python SDKs hand you a parsed object instead of a string. Most tutorials stop at the moment the JSON parses.…