Schemas, Pydantic, and validation — making the model return real data — step 9 of 9
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Write safe_extract(raw) that parses a JSON string the
model returned and validates a Ticket shape:
emailmust be a non-emptystrseveritymust be anintbetween 1 and 5 inclusive
If any check fails, return the dict {"ok": False, "error": "<reason>"}
where <reason> is one of "bad json", "missing email",
"bad severity". On success, return {"ok": True, "data": <parsed dict>}.
Then the script will run it on three inputs and print the result of each. Expected output:
ok=True [email protected]
ok=False error=missing email
ok=False error=bad severity
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Write safe_extract(raw) that parses a JSON string the
model returned and validates a Ticket shape:
emailmust be a non-emptystrseveritymust be anintbetween 1 and 5 inclusive
If any check fails, return the dict {"ok": False, "error": "<reason>"}
where <reason> is one of "bad json", "missing email",
"bad severity". On success, return {"ok": True, "data": <parsed dict>}.
Then the script will run it on three inputs and print the result of each. Expected output:
ok=True [email protected]
ok=False error=missing email
ok=False error=bad severity
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.