Skip to content

Validate, then repair

Read · 1 of 3

Never trust, always check

Parsed isn't the same as correct. A field can be missing, or a number can arrive as "3" (text).

Check each field against what you expect. If something's wrong, don't guess: send the model a short list of the problems and ask again. That repair loop turns most bad replies into good ones on the second try.

Read this lesson as text

Check every field. When it's wrong, tell the model exactly what to fix.

Never trust, always check

Parsed isn't the same as correct. A field can be missing, or a number can arrive as "3" (text).

Check each field against what you expect. If something's wrong, don't guess: send the model a short list of the problems and ask again. That repair loop turns most bad replies into good ones on the second try.

A Python trap

print(isinstance(True, int), isinstance(3, bool))

What does this print?

  • True False
  • False False
  • True True
Show the answer

True False In Python, bool is a kind of int, so isinstance(True, int) is True. A validator that wants a number must rule out booleans on purpose.

Exercise: A validator

Line 1 is a schema like name:str,age:int,tags:list,active:bool. Line 2 is the model's JSON.

Print ok if every field is present with the right type. Otherwise print one line per problem, in schema order: missing: <field> or wrong type: <field> (expected <type>).

Careful: true is not an int.