Stop AI Wrapping JSON in Markdown Fences
```json fences are the most common reason JSON.parse fails on model output. The rule set that prevents them — and why fences happen in the first place.
Overview
Models wrap JSON in markdown fences because their training rewards readable chat answers — a fenced code block IS the polite way to show JSON to a human. Your parser disagrees. The fix is naming the failure mode explicitly: a generic 'return JSON' keeps the fences; 'do not wrap the output in markdown' and 'do not use code fences' as separate rules removes them. This resource loads a webhook-bound contact-form contract in Strict mode, where the anti-fence rules ride alongside the schema — the configuration for output that goes straight into JSON.parse.
Workflow
-
Read the two anti-fence rules
'Do not wrap the output in markdown' and 'do not use code fences' are separate rules on purpose — models treat them as different behaviors.
-
Note the example is unfenced
The example response shows bare JSON. If your example is fenced, the model fences — examples outvote rules.
-
Adopt the contract
Swap in your fields and keep Strict mode for anything that pipes into a parser.
Why This Works
- Naming the failure mode beats prohibiting its category — 'no code fences' outperforms 'JSON only'
- An unfenced example teaches the exact byte stream you want back
- Strict mode's 'nothing else' framing removes the chat-politeness instinct that produces fences
Best for
- Webhook and API pipelines that feed model output to JSON.parse directly
- Chat-model integrations without native JSON mode
- Anyone whose logs show ```json as the first seven characters of a 'JSON' response
Not for
- Repairing already-fenced output you've stored — that's the AI Output Validator
- Display contexts where fences are actually wanted — fencing is correct behavior for humans
Use cases
- Fixing the parse error caused by ```json fences in production
- Removing the strip-the-fences cleanup code from an integration
- Understanding why 'return JSON only' alone doesn't stop fencing