Structuring Outputs and Formats
Specify output schemas, enforce structure, and design responses for easy parsing, scoring, and downstream use.
Content
Markdown and Code Blocks
Versions:
Watch & Learn
AI-discovered learning video
Sign in to watch the learning video for this topic.
Markdown and Code Blocks — the pretty, parseable output style
You're coming in hot from JSON/schema enforcement and CSV/XML outputs. Those taught you how to make machines happy with rigid formats; now let's make humans (and machines) both happy using Markdown and fenced code blocks. This is the bridge between readable documentation and reliably parseable payloads — the sweet spot where clarity meets automation.
"Markdown is what humans read. Code blocks are what machines parse (and devs admire)." — Your future self, after shipping clean docs
Why Markdown + Code Blocks?
- Human-first: Markdown is readable without rendering. Headings, lists, and emphasis help quick comprehension.
- Machine-friendly: Fenced code blocks with a language tag (
json,python) give parsers clear, extractable blobs. - Versatile: Use it for README-style answers, example payloads, command snippets, or embedding structured outputs.
Think of Markdown as a layered cake: the frosting (headings, prose) makes it appetizing; the cake layers (code blocks) deliver the payload.
When to use Markdown vs. raw JSON/CSV
| Use case | Prefer Markdown | Prefer JSON/CSV |
|---|---|---|
| Human-facing instructions | ✅ | ❌ |
| Machine-to-machine strict data | ❌ | ✅ |
| Mixed: docs + example payload | ✅ (with code block) | ⚠️ (wrap JSON in code block) |
If the consumer is a program, give them raw JSON/CSV. If it's a person or a person-plus-toolchain, wrap structured data inside code blocks in Markdown.
Practical patterns and prompt snippets
1) Enforce 'Markdown-only' output
Prompt trick: be explicit and terse.
Respond only in valid Markdown. No explanations, no additional text.
Produce a level-2 heading 'Summary' and a fenced code block labelled 'json' containing a single JSON object with keys: name, tags.
Why this works: you remove ambiguity. The model can't sneak in extra prose if you forbid it, and you specify structure.
2) Embed JSON inside Markdown (for both humans and machines)
Example desired output (what you'd expect the model to return):
## Payload Example
```json
{ "name": "Alice", "age": 30, "skills": ["python", "prompting"] }
Use the language tag (json) so syntax highlighters and many parsers will recognize it.
Note: In the example above the inner JSON is the structured data you can extract programmatically.
### 3) Provide a golden example (few-shot) and a bad example
Few-shot wins when you need style and form. Show one high-quality exemplar and one forbidden format.
Good exemplar (put this first or last depending on the model/version; test order effects):
```text
---GOOD EXAMPLE---
## Result
```json
{ "id": 42, "status": "ok" }
---END---
Bad exemplar:
```text
---BAD EXAMPLE---
Result: id=42 status ok
(plain text, no code fences)
---END---
Why contrast helps: the model learns not just what to do, but what not to do. Remember order effects — in some models the last example has more pull; in others the first does. If behavior is inconsistent, experiment: put the golden example last.
Common pitfalls and how to avoid them
- Extra text outside the code block
- Mitigation: explicitly say 'only the fenced code block' and show an example that contains only the code block.
- Incorrect language tag (
js vsjavascript)- Use the conventional tag for parsing tools (
json,python, ```bash).
- Use the conventional tag for parsing tools (
- Unescaped triple backticks inside content
- If you must include ``` inside a fenced block, fence the outer block with 4 backticks (````) or use indentation or HTML entities.
- Model adds commentary like 'Here is the result:'
- Force brevity: 'Return only the Markdown content; no preface or explanation.'
Validation: quick strategies
- For JSON inside a code block: extract the block with a regex and run a JSON parser.
- For required sections (heading + code block): validate with a lightweight rule-set (pseudo):
if not markdown.contains('## Summary') -> reject
json_blob = extract_fenced_block(language='json')
if parse_json(json_blob) fails -> reject
Automate this in your pipeline so the model's output is validated before being used downstream.
Advanced tips (tricks only the pros whisper about)
- Use YAML front matter for metadata. Start your Markdown with:
---
format: example
version: 1
---
- For ultra-strict output, require the model to produce a single fenced block with a specific language tag. Example instruction: 'Output exactly one fenced block delimited by
json andwith only a compact JSON object.' - If you need the model to include literal code fences inside the response, use 4 backticks to fence the outer block and 3 for the inner, or instruct the model to use HTML code entities.
Quick reference: Dos and Don'ts
- Do: Specify language tags in code fences (
json,bash). - Do: Provide a golden example if you need precise formatting.
- Don't: Ask for prose and code and expect consistent structure — separate responsibilities.
- Don't: Trust visual inspection for structured data — parse and validate.
Closing — takeaways (so you can act fast)
- Markdown + code blocks = human-readable docs + machine-readable payloads. Use both when you need the best of both worlds.
- Few-shot examples steer style — show a perfect example and (optionally) a bad one. Watch order effects and test.
- Validate outputs — extract fenced blocks and run parsers. Never assume correctness.
Action checklist:
- When you want human + structured data, ask for Markdown with a fenced code block.
- Provide a gold example (one high-quality exemplar). If the model misbehaves, swap example order and retry.
- Implement automated checks for the presence of the required heading and code-block-parsable content.
Go forth and make your chatbot outputs pretty, precise, and unambiguously useful. If Markdown is a polished storefront, fenced code blocks are the neatly labeled products on the shelf — people can browse, bots can buy.
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!