Validate LLM Structured Output
A model's output is data you received over a network from something that guesses. Treat it the way you would treat a request body from an unknown client.
Implement validate_response(raw, schema).
raw is the model's reply — a string that is supposed to be JSON. schema maps field names to the type each must have: {"name": str, "age": int, "email": str}.
Return the cleaned object, or None if it cannot be salvaged:
- Parse the JSON. If it isn't valid JSON, return
None. - If the result isn't an object (a list, a number, a string), return
None. - Every field in the schema must be present with the right type, or return
None. - Drop any field the schema doesn't mention. A model that invents an
admin: truefield must not have it reach your database. boolis not anint.Truewhere an integer is required is invalid.
That last rule catches a real bug: in Python isinstance(True, int) is True, so a naive type check lets booleans through where numbers belong.