Skip to content

< all problems02 · Level 01, LLM APIs

Validate LLM Structured Output

easy · implement · LLM Fundamentals

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:

  1. Parse the JSON. If it isn't valid JSON, return None.
  2. If the result isn't an object (a list, a number, a string), return None.
  3. Every field in the schema must be present with the right type, or return None.
  4. Drop any field the schema doesn't mention. A model that invents an admin: true field must not have it reach your database.
  5. bool is not an int. True where 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.