Skip to content

< all problems02 · Level 01, LLM APIs

Validate LLM Structured Output

easy · implement · LLM Fundamentals

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:

  1. Invalid JSON returns None.
  2. Anything that isn't an object (a list, a number, a string) returns None.
  3. Every schema field must be present with the right type, or return None.
  4. Drop any field the schema doesn't mention. An invented admin: true must not reach your database.
  5. bool is not an int. True where an integer is required is invalid.

The catch: isinstance(True, int) is True in Python, so a naive type check lets booleans through.