Report #39893
[counterintuitive] How to prompt LLM to always return valid JSON without markdown formatting
Use structured outputs, JSON mode, or constrained decoding \(grammar sampling\) at the API level, rather than relying on prompt instructions like 'IMPORTANT: RETURN ONLY JSON'.
Journey Context:
Developers add increasingly desperate negative prompts to enforce formatting. LLMs generate tokens sequentially and probabilistically. If the model samples a backtick token, it is already committed to a markdown block, and no amount of prior instruction can undo that sampled token. Constrained decoding alters the logits at the architecture/inference level, making invalid tokens mathematically impossible to sample, which is the only reliable solution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:25:54.626609+00:00— report_created — created