Structured output is the cheapest reliability upgrade
Free-form text is where AI pipelines quietly break. Give the model a shape to fill and validate every answer against it.
Sample post Placeholder content that shows the blog's design. Replace it before launch.
A language model will happily answer in prose, in Markdown, in half-finished JSON or in all three at once. That’s fine in a chat window. In a pipeline, the next step is a program, and programs don’t read between the lines.
Give the answer a shape
The fix is to decide the shape of the output up front and make the model fill it. In Python, a Pydantic model does the job:
from pydantic import BaseModel, EmailStr, HttpUrl
class Contact(BaseModel): email: EmailStr | None phone: str | None source_url: HttpUrlMost model APIs can now constrain generation to a JSON schema, and libraries like DSPy build the same idea into the program itself. Either way, the schema becomes part of the contract, not a hopeful line at the end of the prompt.
Validate anyway
Constrained generation gets the shape right. It can’t promise the content makes sense. So every answer still goes through validation before anything downstream sees it:
from pydantic import ValidationError
def parse(raw: str) -> Contact | None: try: return Contact.model_validate_json(raw) except ValidationError as err: log_rejected(raw, err) # keep it: rejects are future test cases return NoneWhat you get
Once outputs have a shape:
- Failures become loud. A bad answer raises an error instead of slipping into a report.
- Tests become easy. You compare fields, not paragraphs.
- Swapping models gets safer. The contract stays the same when the model behind it changes.
It isn’t clever, and that’s the point. Structured output is the step that turns a demo into a component other code can depend on.