Introduction
This lesson turns raw model text into typed, validated objects with Pydantic v2 schemas.
EntityandStructuredResponseenforce allowed entity types, confidence bounds, and optional follow-ups.parse_structured()usesmodel_validate_jsonso invalid JSON and schema violations fail loudly.MetricsStoretracks OK/FAIL parses, entities, follow-ups, and latency; the FastAPI dashboard polls/metrics.POST /demoruns five mixed payloads so every counter leaves zero after one click.
Highlights
Pydantic v2
field_validatorclamps confidence and rejects invalid entity types.model_validate_jsonreplaces deprecatedparse_rawpaths.Demo mixes three valid and two invalid payloads for full validation coverage.
Live dashboard shows parse OK/FAIL, entities, follow-ups, and throughput.
Docker lifecycle on port 8090 via
start.sh,demo.sh, andrun_tests.sh.
What We Build
structured/package:models.py,parser.py,metrics.py,service.py.FastAPI (
app.py):/parse,/demo,/metrics,/health,/dashboard.CLI demo (
main.py) printing OK/FAIL outcomes for each payload.DEMO_RAW_PAYLOADScovering location, org/date, follow-up, bad type, and non-JSON.
Connection to Previous Lesson
Day 10 assembled prompts and routed categories. Day 11 assumes that text may be JSON and proves it against a contract before any downstream agent trusts fields.
Enables Next Lesson
Validated structures become reliable inputs for Day 12’s agent state machine: transitions consume typed answers and entities, not free-form strings.
Architecture Context
Where this component sits: between raw LLM/text ingress and business handlers that need typed fields.
Why it exists: production systems cannot treat model prose as trustworthy data.
Problem solved: schema enforcement with measurable OK/FAIL rates and operator-visible events.


