<aside> ๐
Date: Thursday, October 8, 2026 ยท Week 2, Day 11 ยท Type: Build Day
</aside>
11.1 The Fragility of Free Text โ why production software crashes when an LLM returns a conversational sentence instead of strict JSON.
11.2 Schema Validation with Pydantic โ defining typed models, required fields, and data validation rules in Python.
11.3 Automated JSON Repair โ handling unescaped quotes, trailing commas, and truncated braces.
Build an "Unstructured Invoice Extractor" that takes messy invoice text, extracts vendor name, date, line items, and total amount, and validates the output against a strict Pydantic model with automated retry logic on failure.
+-------------------+ +------------------+ +-----------------------+
| Raw Invoice Text | --> | LLM Generator | --> | Pydantic Schema Check |
+-------------------+ +------------------+ +-----------------------+
|
+--- [Validation Failed] ---+
| |
v v
[Re-prompt with Error] [Valid JSON Object]
Problem 11. Write a Python validation function validate_and_repair_json(raw_text: str, required_keys: list[str]) -> dict that extracts a JSON substring from conversational LLM output, verifies all required keys exist, and raises an explicit error if a key is missing.
Data Structure: Abstract Syntax Tree (AST) Validation Stack.
Mechanics: Using a character stack to match opening and closing braces to isolate JSON objects from noisy text streams.
Real-World Production Use: Foundational parser used in Instructor and LangChain Output Parsers.