Validate LLM Structured Output
Implement validate_response(raw, schema). raw is a model's reply that is supposed to be JSON. schema maps field names to required types, like {"name": str, "age": int}.
Return the cleaned dict, or None if it cannot be trusted:
- Invalid JSON returns
None. - Anything that isn't an object (a list, a number, a string) returns
None. - Every schema field must be present with the right type, or return
None. - Drop any field the schema doesn't mention. An invented
admin: truemust not reach your database. boolis not anint.Truewhere an integer is required is invalid.
The catch: isinstance(True, int) is True in Python, so a naive type check lets booleans through.