AI Engineering · Module 03
Prompting and structured outputs
- Lessons
- 12
- Free
- 1
- Reading
- ~3 h
Your progress
0 / 12 lessons done
Sign in to trackLessons
- Prompt anatomy: instructions, context, examples, formatFreeWhen a model’s output misses the point, the cause is often a prompt that leaves out something a new colleague would need. Knowing the parts lets you find the missing one instead of guessing or paying for a bigger model.
- System prompts: setting the role and the rulesEvery model feature you ship has standing rules: a tone, a format, things it must never say. Putting them in the right place makes them apply on every call and keeps them apart from what users type, and knowing their limit tells you when to add a check in code.
- Few-shot prompting: teaching by exampleSome output styles are easier to show than to describe. Two good examples often fix a format that a paragraph of rules keeps missing, but they are paid for on every call and the model copies more from them than you meant.
- Step-by-step prompting: asking for the reasoningMany AI features make decisions that depend on several rules: refunds, approvals, routing. Knowing what step-by-step prompting still does, and what it no longer does, keeps those decisions checkable without paying for work the model already did.
- Structured outputs: getting reliable JSONMost AI features stop being demos when code, not a person, reads the model’s reply. Sorting, extracting and filling forms all depend on JSON that parses on every call, and on knowing which mistakes a valid reply can still contain.
- Validating model output with PydanticEvery feature that turns model output into an action, such as moving a ticket, filling a form or calling a tool, needs a check between the model and the action. Valid JSON only tells you the shape is right; your own rules decide whether the values are safe to use.
- Prompt templates and version controlA prompt edit changes what every output says without changing a single code path, so tests stay green while quality drops. Templates, version ids and a careful rollout are what let you find the change that did it, and undo it in minutes.
- Long inputs: splitting work across many callsReports over thousands of tickets, reviews over a whole catalogue, notes over a year of meetings: many useful AI features read far more text than one call should. Splitting the work well decides whether the result is complete, cheap and repeatable.
- Multimodal inputs: images, PDFs and audioCustomers explain problems with photos, screenshots, invoices and voice notes, not only words. Any AI feature that reads tickets, claims or forms has to decide how those files reach the model, what they cost and when to trust what the model saw.
- Prompt pitfalls: ambiguity, drift and leakagePrompt bugs pass code review and unit tests, because no code path changes. They show up later as drafts that skip what the customer needs, a queue that slowly fills, or a customer reading another customer’s address.
- Design: an invoice data extractorPulling fields out of documents is one of the most common AI features: invoices, receipts, forms, contracts. The model call is the easy part. The work is deciding which answers can go straight into a system of record and which must reach a person first.
- Lab: turning messy emails into clean recordsMost AI features start by turning text people wrote into fields a program can use: a ticket record, an invoice line, a form. Running an extractor on messy real input, and counting what fails, is how you learn where it breaks before your users do.
Practise with
Terms this module teaches 30
Open glossaryBase64 encoding
A way to write binary data, such as an image, as plain text characters, which makes it about a third larger.
Chain-of-thought prompting
Prompting a model to write out the steps of a problem before its final answer, with worked examples or an instruction such as “Let’s think step by step”. It helped older models on multi-step problems; current models that think before answering need it less.
Constrained decoding
Generating text while removing, at each step, every token that would break a set of rules such as a grammar, so the output always follows the rules.
Enum
A fixed list of the only values a field may take.
Extraction
Pulling specific fields, such as a name, an order number or a category, out of free text into a fixed record a program can use.
Few-shot prompting
Putting a few worked examples of input and the wanted output in the prompt, so the model copies their pattern.
Field validator
A function run on one field’s value during validation, which returns the value, possibly cleaned, or rejects it with an error.
Files API
A provider service where you upload a file once and then refer to it by an id in later model requests, instead of sending its bytes each time.
JSON Schema
A JSON document that describes the shape of other JSON: which keys it must have and what type and values each may take.
Map-reduce
A way to process a large input in two steps: run the same small job on each piece independently (map), then combine the partial results into one answer (reduce).
Model validator
A function run on the whole object after its fields pass, used for rules that involve more than one field.
Multimodal input
A model request that mixes text with other kinds of input, such as images, PDF pages or audio.
Nullable field
A field whose value may be null, meaning “no value”, as well as its normal type.
Prompt
Everything sent to a model in one call: the instructions plus any context, examples and input it needs.
Prompt delimiter
A marker, such as an XML tag, that shows where one part of a prompt starts and ends, so the model can tell instructions from the input they apply to.
Prompt drift
A slow change in an AI feature’s behaviour as small prompt edits pile up, or as the prompt moves to a new model, with no single change to blame.
Prompt leakage
Text from a model’s context, such as the system prompt or another customer’s data, appearing in an output that shouldn’t contain it.
Prompt template
The fixed wording of a prompt with named slots that code fills on every call, kept in a file so it can be reviewed and versioned like code.
Prompt version
An identifier for one exact wording of a prompt, saved with every output it produces so results can be traced and compared.
Pydantic
A Python library that checks data against a class with type hints and rules, and can produce a JSON schema from that class.
Redaction
Replacing sensitive details in text, such as names, addresses and order numbers, with neutral labels before the text is stored, shown or sent to a model.
Review queue
A list of items an automated step was not sure enough about, waiting for a person to check, correct or reject them before they take effect.
Strict tool use
A tool definition setting that guarantees the model’s call to that tool uses a valid tool name and inputs matching the tool’s schema.
Structured outputs
A model API feature that constrains the reply to JSON matching a schema you supply.
System prompt
Instructions sent with a model call that apply to the whole conversation, such as the model’s role, rules and output format.
Text layer
The characters stored inside a PDF, with their positions on the page, that let software copy or search the text. Scanned PDFs and photos have none, and its order does not always follow a table’s rows.
User message
A turn in a model call’s messages list with the role user. It carries this request’s input, such as the text to work on and what to do with it.
Validation error
The error raised when data breaks one or more rules of a schema or validator, listing each broken rule and where it is.
Visual token
One small square of an image, 28 by 28 pixels on Claude models, which the model reads and bills like one input token.
Zero-shot prompting
Asking a model to do a task from instructions alone, with no examples of the expected output in the prompt.