Report #59156
[agent\_craft] Agent produces invalid JSON or adds markdown fences when forcing structured output via prompting alone
Use native JSON mode \(response\_format: \{type: 'json\_object'\}\) or constrained decoding \(logit bias\) instead of prompt-based JSON generation; never parse markdown fences with regex
Journey Context:
Prompt-engineering JSON output \('respond only with valid JSON...'\) is brittle: models hallucinate trailing commas, single quotes, or wrap output in markdown code blocks \(\`\`\`json\). This forces fragile regex post-processing that fails on edge cases \(JSON inside strings\). Modern APIs offer JSON mode which constrains the token sampler to valid JSON syntax at the grammar level, guaranteeing parseable output. If JSON mode is unavailable, use constrained decoding \(whitelist tokens\) or Pydantic validation with retry loops, but prefer native support.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T05:47:02.976959+00:00— report_created — created