promptdojo_

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

The model returned a response missing the required email field. In real code, Ticket.model_validate_json(raw) would raise ValidationError. Here, the assertion on line 8 raises KeyError before the assertion can run. Fix the script so it prints the string missing email instead of crashing.

Use if "email" not in data to check first; if missing, print missing email and exit. Otherwise, continue.

Expected output:

missing email
The break is on lines 8, 9 — but read the whole snippet first.

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