Structured JSON Output + Validation
The moment an LLM feeds a downstream system, plain prose is a liability. You ask the model for JSON, and then you must never trust it blindly. Models emit malformed JSON, drop fields, and put the wrong type in a slot. A production parser assumes all of that will happen and checks every field.
You will build the validator that guards the boundary. validate_record(text) takes a raw string the model returned and:
- Tries to
json.loadsit inside atry/except, so malformed JSON returns a clean failure instead of crashing your service. - Checks the required schema
{"name": str, "age": int, "email": str}: every field present, every value the right type. - Returns
{"ok": bool, "errors": [...]}whereerrorsnames what went wrong.
There is one trap that catches almost everyone. In Python, bool is a subclass of int, so isinstance(True, int) is True. JSON true parses to Python True, and a naive type check would accept "age": true as a valid integer. Reject booleans where you require an int.
>>> isinstance(True, int)
True # the trap: True sneaks past an int check
>>> type(True) is int
False # one way to be strictA model could fill this JSON for you live, but grading only touches your deterministic validator: does it accept a clean record, and does it reject every flavor of broken one? Press Run to grade.
Write validate_record(text) that json.loads the text inside a try/except and validates it against {"name": str, "age": int, "email": str}. Return {"ok": bool, "errors": [...]}: ok True with empty errors for a complete, correctly-typed record; otherwise False with a list naming each problem. Malformed JSON returns ok False (no exception). A boolean must be rejected where an int is required.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.