Prompt Engineering JSON Output Markdown Fences

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.

How to use this resource

  1. 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.

  2. Note the example is unfenced

    The example response shows bare JSON. If your example is fenced, the model fences — examples outvote rules.

  3. 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

FAQ

Why does this JSON prompt list 'do not wrap the output in markdown' and 'do not use code fences' as two separate rules?

Models treat markdown wrapping and code fences as different behaviors, so the OUTPUT RULES name each failure mode explicitly instead of bundling them. A single 'return JSON only' leaves the ```json fence intact because fencing feels like polite formatting, not a violation. Splitting them into two prohibitions removes the fence and the surrounding markdown separately.

After I generate this prompt in the JSON output builder, will the model's response go straight into JSON.parse?

The builder outputs the prompt text; you run it in your own assistant, and the OUTPUT RULES line 'Output only valid JSON — nothing else' is what aims the response at JSON.parse directly. NewPrompt does not run the model or strip fences, so you still parse and review the contact-form object against its schema (full_name, email, subject, message, urgency) before trusting it.

I added the no-fence rules but the model still returns ```json — what am I missing?

Check that your EXAMPLE OF A VALID RESPONSE is bare JSON, not fenced. Examples outvote rules, so a fenced example teaches the model to fence even with 'Do not use code fences' present. The template deliberately shows the full_name/email/subject example unfenced; if you swapped in your own fields inside a ```json block, the fence comes back.

More resources from JSON Output Prompt Builder

Resources that pair well

Related tools

Guides for this resource

Tip: Save time by exploring related resources and tools that integrate with this resource.