Session 5· 05· 10 min
Parse & Validate Structured Output
What you'll learn
- ▸Parse the JSON string into a Python dict
- ▸Validate types and required keys programmatically
- ▸Handle cases where the model output is malformed
Even with strict schema mode, your code should validate. Network hiccups, older models, or a rare edge case could still produce something unexpected. This lesson shows the belt-and-braces pattern production apps use.
Defense in depth
Schema = guarantee from the API. Validation = your safety net. Use both. Takes 4 lines and saves you from rare but painful bugs.
05_parse_and_validate_structured_output.py
raw = response.choices[0].message.content or "{}"
data = json.loads(raw) ①
required = {"question", "answer", "confidence"}
missing = required - data.keys() ②
if missing:
raise ValueError(f"Missing keys: {missing}")
if not isinstance(data["confidence"], int): ③
raise TypeError("confidence must be an integer")①Parse the raw JSON string into a Python dict.
②Python set math: compute which required keys are missing. Empty set = all good.
③Spot-check critical types. Integer confidence scoring is a great example.
$ python 05_parse_and_validate_structured_output.py
Code Check
What does {"a", "b", "c"} - {"a", "b"} evaluate to?
Recap — what you just learned
- ✓Always json.loads() the response content
- ✓Use set math to find missing required keys in one line
- ✓Type-check critical fields — even strict mode can surprise you