Prompting & Structured Output
Write clear prompts and validate structured output.
Write prompts that state the task, the inputs, and the expected shape of the answer. Validate what comes back and recover when the output does not match the contract.
4 questions
Questions
Easy
1.5 h
Rewrite a vague request into a testable extraction prompt with category rules, a missing-data policy, consistent examples, and a JSON output contract, then explain why parseable JSON can still be wrong.
1.5 h
Write a pure function that accepts a model's JSON review summary or returns every contract violation as a stable error code, predict its result for eight replies, and say what a valid summary still cannot prove.
Medium
3 h
Implement a dependency-free validator for a ticket contract, classify each fixture failure by layer, and design a two-attempt recovery that never commits invalid output.
Hard
4 h
Upgrade a ticket classifier to a versioned contract with multiple IDs, evidence, and review states, keep two v1-only consumers working, and decide from evaluation data whether a candidate prompt should ship.
What you will be able to do
- Write an explicit task instruction with an observable definition of success
- Keep application instructions separate from untrusted source data, and explain why delimiters alone are not a defense
- Explain zero-shot and few-shot prompting, and choose examples that cover decision boundaries
- Give an omitted field, null, and an empty string distinct, documented meanings
- Distinguish parseable JSON, schema-valid output, and a correct answer
- Restate an output contract and settle its unclear cases (null versus missing, unknown keys, letter case, whole-number floats) before writing code
- Check runtime JSON types in JavaScript and Python, including numeric strings, fractions, booleans, null, and arrays
- Implement a pure validator that returns ordered error codes for every failing field and never throws on bad model output
- Write table-driven tests for valid, invalid, boundary, and cross-language replies
Before you start
Suggested, not required.
No prerequisites: a good place to start.
Hands-on exercises
Interview rounds that use this topic
Related on CodeSnatch
System Design
Covers machine-readable error codes, response envelopes, and which changes force a new API version.
AI Engineering
Scores structured outputs against labeled cases with named denominators, the way prompt changes get measured.
AI Engineering
Carries the output contract idea into tool calls, where code validates the arguments a model proposes.
Code snippet
Keeps one runtime schema as the source of truth for types and returns a Result instead of throwing.
System Design
Treats event schemas as contracts, with version fields and backward and forward compatibility modes.
System Design
Explains backoff with jitter, timeouts, and retry storms, the limits any retry after invalid output must respect.
Community article
A practitioner's notes on boundary-case few-shot examples, a failure_reason field, and eval-driven prompt changes.
