Validating AI output before it reaches a user
Understand the idea
A model body can look finished and still be the wrong shape. The route has to notice before the client does.
The idea
The AI-native lesson Why AI code looks right and isn't says a model is rewarded for output that looks finished, not for output that matches a contract you own. That lesson is about code that passes a glance and still encodes the wrong rule. This lesson is the same claim at the HTTP boundary. A of { note: 'looks finished' } is not a summary. The unvalidated route reads choices[0] and crashes. summaryFrom returns null when choices is missing, when content is missing, or when the string is blank. The route then sends 502 and does not forward the model JSON. A shape check stops the crash and stops you from handing over a body you did not promise. It does not prove the summary is true. A summary can have the right shape and still state the wrong fact. That further check is the contract the AI-native lesson asks you to write down.
What changes
| Body | Unread check | summaryFrom |
|---|---|---|
| note looks finished | Throws on choices[0] | null, then 502 |
| content is spaces | Would send a blank summary | null, then 502 |
| content Summary: Milk | Sends it | 200 and that string |
The command
Crash on a finished-looking body
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Refuse a body with no choices
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Refuse a blank summary
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Accept a summary that matches
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
The unvalidated run is 500 and the body names a TypeError on reading 0. Both guarded failures are 502 and the contract error. The valid body is 200 and summary Summary: Milk.
Guided challenge
Return 502 when the summary is missing
The route ignores a null summary and still tries to send it. If summaryFrom returns null, respond with 502 and the contract error instead.
Return 502 when the summary is missing
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
status 502 summary contract
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.