Report #15766
[agent\_craft] Agent outputs markdown fences or explanatory text around JSON, breaking parsers
Append the instruction 'Return ONLY the JSON object. Do not include markdown code fences, explanations, or trailing commas. End your response immediately after the closing brace.' to the final user message, not the system prompt
Journey Context:
Even with JSON mode enabled, models often wrap output in markdown fences \(\`\`\`json\) or add conversational filler \('Here is the JSON you requested:'\). Placing strict formatting instructions in the system prompt is less effective because the system prompt is 'far' from the actual generation point when the context grows. The hard-won fix is to place the formatting constraints in the final user message \(or the last turn before the model responds\). This leverages recency bias—the model follows the most immediate instruction. Additionally, explicitly forbidding trailing commas is crucial because standard JSON parsers reject them, yet models often generate them when continuing from partial examples.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T00:54:55.784396+00:00— report_created — created