Course Content
Prompt Engineering Mastery
6 sections · 32 lessons
How can you use prompts to generate structured outputs like JSON or HTML?
What you need to know
There are several ways to get machine-readable output, from least to most reliable.
| Method | How it works | Status in 2026 |
|---|---|---|
| Ask in the prompt | "Return only JSON" | Works often; fails sometimes |
| Prefill | Start the assistant reply with { | Not supported on the newest Claude models |
| JSON mode | API guarantees valid JSON, any shape | Valid syntax, but fields can be missing |
| Structured outputs | API enforces your JSON Schema | Recommended for extraction |
| Strict tool calling | Tool arguments follow a strict schema | Good when the output is an action |
Using structured outputs well
- Keep the schema simple — providers support a subset of JSON Schema; deep nesting and exotic keywords may be rejected.
- Describe fields in the schema or prompt — the schema says
string, the prompt says what belongs there. - Use enums for closed sets (
"category": {"enum": ["cookware", "storage", "appliances"]}). - Allow null for optional fields, so the model is not forced to invent.
- Check the stop reason — a truncated or refused reply may not be complete.
HTML output
The schema cannot make HTML safe. A model can produce a <script> tag, an onerror attribute, or copy one from its input. So:
- Tell the model the allowed tags, for example
p,ul,li,strong. - Sanitise with an allow-list library — DOMPurify in JavaScript, nh3 in Python (the older
bleachlibrary is deprecated). - Escape any user-supplied text you insert yourself.
Often the cleanest design is to ask for structured content (JSON with title and bullets) and let your own template render the HTML.
A real-life example
A product-description writer used to ask for "only JSON" and got replies like this:
Sure! Here's the description:{"title": "Steel Kadai", "bullets": ["Heavy base", "Riveted handles",About 2% failed to parse — chatty preambles, Markdown fences around the JSON, truncated arrays, trailing commas. The team moves to structured outputs with this schema and prompt:
Schema: {"title": string (max 60 chars), "bullets": array of 3-5 strings, "body_html": string using only <p>, <ul>, <li>, <strong>}Write the listing for the product in <specs>. Use only facts from <specs>.Parse failures drop to zero. In a test, one supplier's spec sheet contained <img src=x onerror=alert(1)>; the model copied it into body_html, and nh3 removed it before rendering. Later the team drops body_html entirely and renders the bullets with their own template, which removes the risk at the source.
Follow-up questions to expect
- "JSON mode or structured outputs?" — Structured outputs: JSON mode only guarantees valid syntax, not your fields and types.
- "Does a strict schema hurt quality?" — It can if the schema is awkward; let free-text reasoning happen in thinking or a separate field, and keep the schema to the data you need.
- "How do you return an error or 'not found'?" — Put it in the schema: a nullable field or a
statusenum such as"found"or"not_found".