Prompt Engineering Mastery

Course Content

Prompt Engineering Mastery

6 sections · 32 lessons

How can you use prompts to generate structured outputs like JSON or HTML?


Ways to get JSON, from weakest upAsk for JSONin the promptPrefill a brace(now rejected)JSON mode:valid syntax onlyStructuredoutputs: your schemaValidatevalues in codetopbottomStructured outputs removed the 2 percent parse failures; only code catches a wrong but well-formed value.
Each rung up removes a failure mode, but no rung makes the values true.

What you need to know

There are several ways to get machine-readable output, from least to most reliable.

MethodHow it worksStatus in 2026
Ask in the prompt"Return only JSON"Works often; fails sometimes
PrefillStart the assistant reply with {Not supported on the newest Claude models
JSON modeAPI guarantees valid JSON, any shapeValid syntax, but fields can be missing
Structured outputsAPI enforces your JSON SchemaRecommended for extraction
Strict tool callingTool arguments follow a strict schemaGood 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:

  1. Tell the model the allowed tags, for example p, ul, li, strong.
  2. Sanitise with an allow-list library — DOMPurify in JavaScript, nh3 in Python (the older bleach library is deprecated).
  3. 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:

Text
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:

Text
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 status enum such as "found" or "not_found".