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:
nameis present and astrageis present and anint
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
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:
nameis present and astrageis present and anint
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
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.