<aside> ๐Ÿ“…

Date: Thursday, October 8, 2026 ยท Week 2, Day 11 ยท Type: Build Day

</aside>

Learning Manual

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.

The Daily Build

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.

Architecture Diagram

+-------------------+     +------------------+     +-----------------------+
| Raw Invoice Text  | --> | LLM Generator    | --> | Pydantic Schema Check |
+-------------------+     +------------------+     +-----------------------+
                                                              |
                                  +--- [Validation Failed] ---+
                                  |                           |
                                  v                           v
                       [Re-prompt with Error]       [Valid JSON Object]

Interview Questions

  1. Why does parsing LLM outputs with regular expressions (regex) fail in production?
  2. What is grammar-constrained decoding (e.g., JSON mode / Outlines), and how does it guarantee valid JSON at the token level?
  3. How do you handle cases where an LLM returns a valid JSON object, but the extracted date or email is logically invalid?

Problem Statement

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.

AI-DSA Primitive

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.

Software Design Pattern