promptdojo_

Schemas, Pydantic, and validation — making the model return real data — step 8 of 9

Build the parse-and-validate step. Write a function extract_user(raw) that takes a JSON string the model returned, parses it with json.loads, validates that:

  • name is present and a str
  • age is present and an int

If the JSON is missing a field or has the wrong type, return the string "invalid". Otherwise return the parsed dict.

Then the script will call it on a good response and print the name.

Expected output:

priya

full-screen editor opens — close anytime to keep reading.