Skip to content

08. Output Formatting

Without format instructions, every LLM response is a surprise. With format instructions, every response is exactly what you need.

Output formatting is one of the highest-leverage prompt engineering techniques — it costs nearly zero tokens but dramatically increases the usability of responses.


You ask: “Compare React and Vue.”

Without format instructions, you might get:

  • A paragraph
  • A bullet list
  • A 3-page essay
  • A table
  • A poetic comparison

You need a specific format because you’re going to:

  • Display it on a webpage (HTML)
  • Parse it programmatically (JSON)
  • Include it in a report (Markdown)
  • Import it into a spreadsheet (CSV)
flowchart TD
subgraph UNFORMATTED["Without Format Instruction"]
U1["User: Compare React and Vue"] --> U2["❌ Random format\nParagraph? List? Table? Essay?"]
end
subgraph FORMATTED["With Format Instruction"]
F1["User: Compare React and Vue\nFormat: Markdown table"] --> F2["✅ Predictable format\n| Aspect | React | Vue |"]
end
style UNFORMATTED fill:#ef4444,color:#fff
style FORMATTED fill:#22c55e,color:#fff

If you submit a free-text form, you might get a novel, a few words, or anything in between. If you submit a form with labeled fields, you get exactly what each field asks for.

Output formatting creates the labeled fields for an LLM.

A format specification is like a form template — it tells the model exactly where to put each piece of information.


flowchart LR
FORMATS["Output Formats"] --> MARKDOWN["Markdown\nDocs, READMEs\nCode blocks, tables"]
FORMATS --> JSON["JSON\nAPIs, data processing\nProgrammatic parsing"]
FORMATS --> HTML["HTML\nWeb content\nEmails, reports"]
FORMATS --> XML["XML\nLegacy systems\nComplex hierarchies"]
FORMATS --> CSV["CSV\nSpreadsheets\nData analysis"]
FORMATS --> YAML["YAML\nConfig files\nFrontmatter"]
FORMATS --> CODE["Code\nDirect output\nNo wrapping"]
style FORMATS fill:#8b5cf6,color:#fff
style MARKDOWN fill:#3b82f6,color:#fff
style JSON fill:#22c55e,color:#fff
style HTML fill:#f59e0b,color:#fff
style XML fill:#ec4899,color:#fff
style CSV fill:#14b8a6,color:#fff
style YAML fill:#f97316,color:#fff
style CODE fill:#ef4444,color:#fff

Documentation, READMEs, formatted text, code examples

Format the response as a markdown document with:
- A level-2 heading for each section
- Code blocks with language tags
- A table at the end for summary
Explain the difference between let, const, and var in JavaScript.
Format your response as a markdown table with columns:
| Feature | let | const | var |
|---------|-----|-------|-----|
| Scope | ... | ... | ... |
| Reassignable | ... | ... | ... |
| Hoisted | ... | ... | ... |
Featureletconstvar
ScopeBlockBlockFunction
ReassignableYesNoYes
HoistedYes (TDZ)Yes (TDZ)Yes (undefined)

APIs, programmatic processing, data extraction, multi-field responses

Return the response as a JSON object with the following structure:
{
"summary": "brief overview",
"key_points": ["point1", "point2"],
"recommendation": "your recommendation"
}
Extract the following information from this invoice and return it as JSON:
{
"vendor_name": "string",
"invoice_date": "YYYY-MM-DD",
"total_amount": "number",
"line_items": [
{
"description": "string",
"quantity": "number",
"unit_price": "number"
}
]
}
Invoice text:
[invoice text here]
{
"vendor_name": "Acme Corp",
"invoice_date": "2024-01-15",
"total_amount": 1500.00,
"line_items": [
{
"description": "Web development services - January",
"quantity": 40,
"unit_price": 37.50
}
]
}

Web content, emails, rendered components

Return the response as HTML. Use semantic tags and inline styles.
Create a pricing card for a SaaS product with three tiers: Basic ($9/mo),
Pro ($29/mo), and Enterprise ($99/mo). Return as HTML with inline styles
for a clean, modern look.

Complex hierarchical data, legacy systems, interoperability

Return the configuration as XML:
<config>
<database>
<host>localhost</host>
<port>5432</port>
</database>
<features>
<feature enabled="true">authentication</feature>
</features>
</config>

Spreadsheets, data analysis, bulk data

Return the data as CSV with headers:
name,email,role,department,start_date

Config files, frontmatter, simple structured data

Format the API documentation as YAML:
endpoints:
- path: /users
method: GET
description: List all users

flowchart TD
Q1["Who consumes the output?"]
Q1 -->|"Humans reading"| Q2["How complex?"]
Q1 -->|"Machines parsing"| JSON["JSON or YAML"]
Q2 -->|"Simple"| MARKDOWN["Markdown"]
Q2 -->|"Rich content"| HTML["HTML"]
Q2 -->|"Code"| CODE["Code blocks"]
Q2 -->|"Data table"| TABLE["Table or CSV"]
FormatHuman ReadableMachine ParsableComplexityToken Efficiency
Markdown★★★★★★★Low★★★★
JSON★★★★★★★★Medium★★★
HTML★★★★★★Medium★★
XML★★★★★★High★
CSV★★★★★★★★Low★★★★★
YAML★★★★★★★★Low★★★★
Code★★★★★★★★Low★★★★

Don’t describe the format — show it:

❌ "Return a JSON object with the user's name, email, and role."
✅ "Return as JSON:
{
"name": "string",
"email": "email format",
"role": "admin | user | viewer"
}"
✅ "Format each item as: [Name] — [Role] — [Years of Experience]
Example: Jane Smith — Senior Engineer — 8 years"
✅ "Return ONLY the JSON object. No markdown formatting, no code blocks,
no explanation text before or after the JSON."
✅ "Return a 3-sentence summary in a single paragraph."
✅ "Remember: Return ONLY JSON. No other text."

❌ "Extract the key data from this email."
✅ "Extract the following from this email and return as JSON:
{
"sender": "sender's email address",
"subject": "email subject line",
"urgency": "high | medium | low",
"action_items": ["item1", "item2"],
"deadline": "YYYY-MM-DD or null if none"
}
Return ONLY the JSON object."
❌ "Explain this API endpoint."
✅ "Document this API endpoint in markdown with the following sections:
## Endpoint
## Method
## Request Body
## Response
## Example
## Error Codes
Use a code block for the example request/response."

MistakeWhy It’s Wrong
❌ Describing format without showing it”Return JSON” is vague. Show the schema.
❌ Conflicting format instructions”Return as JSON with a markdown table” — choose one
❌ Forgetting to strip surrounding textThe model wraps JSON in “Here’s the JSON:” — tell it not to
❌ Using complex nested formatsDeeply nested JSON is harder for the model to get right
❌ No validation stepAlways validate structured output matches the expected schema

AspectBad FormatGood Format
Clarity”Format nicely""Return as a markdown table with columns: X, Y, Z”
ExampleNoneShows the exact JSON/YAML/table structure expected
ConstraintsNone”No markdown code block around JSON”
ValidationNone”Each entry must have all required fields”
Edge casesNot handled”If no data, return { data: [], total: 0 }“

OpenAI’s API supports response_format parameter:

{
"model": "gpt-4o",
"response_format": { "type": "json_object" },
"messages": [
{"role": "system", "content": "You are a helpful assistant that outputs JSON."},
{"role": "user", "content": "Extract the name and age from: John is 30 years old"}
]
}

Anthropic’s API supports structured output via prompt engineering — specify the JSON schema in the system prompt and validate the response.


Q: Why is specifying the output format important in prompt engineering?

It constrains the model’s output, making it predictable and immediately usable. Without format instructions, the model may return inconsistent formats that require manual parsing.

Q: What’s the best output format for programmatic consumption and why?

JSON is typically best because it’s widely supported, has built-in parsing in every programming language, supports nested structures, and most LLMs are well-trained on JSON generation.

Q: How would you design a prompt that produces validated structured output in production?

I’d use: (1) A clear JSON schema in the prompt with example values, (2) A system instruction to return ONLY the JSON with no surrounding text, (3) Programmatic JSON parsing with error handling, (4) Retry logic with the parse error as feedback, (5) Validation against the expected schema using a library like Zod or Pydantic.


FormatUse CaseKey Tip
MarkdownDocumentation, readable contentUse tables for comparisons
JSONAPIs, programmatic processingShow the exact schema
HTMLWeb contentUse inline styles
CSVData export, spreadsheetsInclude headers
YAMLConfig filesMatch existing config format
CodeDirect code outputSpecify language

Previous: 07 — Context Engineering →

Next: 09 — JSON & Structured Outputs →