CoachnestCoachnest
Sign InGet Started
Back to course

Prompt Engineering Mastery: From Fundamentals to Production

…
—
Contents
1

What Is Prompt Engineering?

ReadingFree
2

How Large Language Models Actually Work

ReadingFree
3

Tokens, Context Windows, Temperature & Sampling

Reading11m
4

The Anatomy of a Great Prompt

Reading13m
5

Module 1 Knowledge Check

Quiz8m
6

Zero-Shot, One-Shot & Few-Shot Prompting

Reading12m
7

Role & Persona Prompting

Reading9m
8

Instruction Clarity, Delimiters & Decomposition

Reading11m

Controlling the Output Format

Reading10m
10

Module 2 Knowledge Check

Quiz8m
11

Chain-of-Thought Prompting

Reading12m
12

Self-Consistency & Tree-of-Thought

Reading11m
13

ReAct — Reasoning + Acting with Tools

Reading12m
14

Structured Output with JSON Schemas

Reading11m
15

Module 3 Knowledge Check

Quiz8m
16

Retrieval-Augmented Generation (RAG)

Reading13m
17

Prompt Templates, Variables & Chaining

Reading11m
18

Tool / Function Calling Patterns

Reading12m
19

Project — Build a Customer Support Assistant

Reading14m
20

Module 4 Knowledge Check

Quiz8m
21

Evaluating Prompt Quality

Reading12m
22

Prompt Injection & Security

Reading12m
23

Reducing Hallucinations

Reading10m
24

Cost, Latency & Optimization

Reading10m
25

Final Assessment — Prompt Engineering Mastery

Quiz15m
←→navigate lessons
Chapter 2 of 5·Module 2 · Core Prompting Techniques
Lesson 9 of 25Reading10 min

Controlling the Output Format

#Controlling the Output Format¶

If code consumes the output, the format is a contract — engineer it like one.

Techniques, Weakest → Strongest¶

1. Describe the format¶

text
1 line
1Answer with a comma-separated list of country names only.

Cheap, but the model may still add "Sure! Here you go:".

2. Show the exact shape¶

text
3 lines
1Respond ONLY with JSON in exactly this shape:
2{"country": string, "capital": string, "population": number}
3No prose, no markdown fences.

3. Few-shot the format¶

Demonstrate 2–3 input→output pairs where the output is exactly the target format. The model copies structure extremely reliably from examples.

4. Prefill / response priming¶

Start the model's answer for it so it has no room to add preamble:

text
2 lines
1... Output the JSON now.
2Assistant: {

5. Use the API's structured-output feature¶

Most providers offer JSON mode or schema-constrained / structured outputs that guarantee valid JSON matching a schema. When available, this is strictly better than prompting alone — use it (covered hands-on in Module 3).

Robust Parsing Still Matters¶

Even with the above, defensively parse:

js
5 lines
1function safeJson(text) {
2  // Strip accidental \`\`\`json fences before parsing
3  const cleaned = text.replace(/^\`\`\`(json)?|\`\`\`$/g, "").trim();
4  return JSON.parse(cleaned);
5}

Common Format Failures & Fixes¶

FailureFix
Adds "Here is the JSON:""Respond with JSON only. No preamble." + prefill
Wraps JSON in ``` fencesAsk for raw JSON; strip fences on parse
Trailing commas / commentsFew-shot valid JSON; use JSON mode
Truncated JSONIncrease max_tokens

Golden rule: The more your downstream code depends on the format, the stronger the technique you should use — and always parse defensively.

Previous

Instruction Clarity, Delimiters & Decomposition

Next

Module 2 Knowledge Check

Use ← → arrow keys to navigate between lessons