Implement output validation with Pydantic/JSON Schema
Jump & context
Week 9: AI-SDLC Release Gates, PyRIT CI/CD, CFG Output Caging & Model Extraction Defense
The five hours
- 01
Concepts & system design
60 min· read · map the mechanismR07 — JSON Schema strict validation
R30 — OWASP LLM05: Improper Output Handling
Read the assigned sections; map the mechanism, threat model and trade-offs.
- 02
Project implementation
120 min· build · integrate · debugBuild
output-contract(D6): strict server-side Pydantic/JSON Schema validation, enum/range/length constraints, unexpected-field rejection (extra='forbid'), and sink-specific HTML/Markdown sanitization.Build, integrate, debug and improve the increment in the active project.
- 03
Security lab
60 min· attack your own systemInject XSS payloads (
<script>,javascript:links, malicious Markdown image beacons) into simulated model outputs and proveD6neutralizes them before browser/API rendering.Test the abuse case, failure path, authorization bypass or resource limit.
- 04
Verification & evidence
30 min· test · record evidenceD6output validation and sink-sanitization test suite.Add or run regression tests; record the 9-point evidence record.
- 05
Deliberate practice & Socratic review
30 min· explain aloud · update gapsAnswer the 8 Socratic questions without notes: What? Why? How? What if? Why not? How can I prove it? Where can it break? What have I learned?
Re-explain the hardest concept aloud, inspect one edge case, update the gap log.
- total5 h
Read today · inside the concepts block
R07 R30Core schema types, required properties, additionalProperties: false, enums, and numeric/string bounds.
Improper output handling (LLM05): pairing schema validation with sink-specific encoding (XSS/SQLi/SSRF prevention).
Read only the named sections needed for today's task. Reading sits inside the 60-minute concepts block; it is not extra time.
Free resources for this day
5 curated- docsJSON Schema (official)Getting Started Step-By-Step
Builds a schema incrementally with required properties, type constraints, and validation runs — the contract you mirror in the D6 output schema.
R07checked 2026-11-11 - docsPydantic docsStrict Mode
Explains the coercion that silently turns '123' into 123 and how strict mode stops it, which is required so model output cannot smuggle in wrong-typed fields.
R07checked 2026-11-11 - videoLearn with AmineHow do you use Pydantic v2 for validation and structured outputs with LLMs
Shows Pydantic v2 models used as the LLM output contract with validation errors surfaced, i.e. schema-first parsing rather than regex on prose.
R07checked 2026-11-11 - videoAnkit BansalData Validation for Data & AI Engineering Using Pydantic | Pydantic Basics to Structured Output
Covers field constraints, validators, and using Pydantic to get reliable structured output from models, the enum/range/length enforcement in D6.
R07checked 2026-11-11 - articleOWASP Gen AI Security ProjectOWASP LLM05: 2025 Improper Output Handling
Explains why schema validation alone is not enough and pairs it with sink-specific encoding to stop XSS, SQLi, and SSRF through model output.
R30checked 2026-11-11
Evidence record · 9-point standard
A lab is complete only when it produces evidence. Installing PyRIT, Garak, Presidio or TransformerLens is not completion. Saved on this device only.
Day notes
emptyThe two source formulations
verbatim · nothing summarisedProject & Deliverable Target
P2 (Primary) & P1/P3 | `D6` (CFG Output Caging Layer) | S7
Core Implementation
Build output-contract (D6): strict server-side Pydantic/JSON Schema validation, enum/range/length constraints, unexpected-field rejection (extra='forbid'), and sink-specific HTML/Markdown sanitization.
Security Lab
Inject XSS payloads (<script>, javascript: links, malicious Markdown image beacons) into simulated model outputs and prove D6 neutralizes them before browser/API rendering.
Verification & Evidence
D6 output validation and sink-sanitization test suite.
Read Today (60m Concepts Block)
R07 (JSON Schema strict validation), R30 (OWASP LLM05: Improper Output Handling).
---