Report #31652
[agent\_craft] Agent generates invalid JSON or XML for tool arguments causing parser failures
Use constrained decoding \(if available via API like Outlines, Guidance, or OpenAI JSON mode\) to force valid JSON schemas. If constrained generation is unavailable, append a validation instruction: 'You must respond with ONLY a JSON object matching this schema. Do not include markdown fences or explanations.' Then wrap the call with a retry loop that feeds parser errors back to the model.
Journey Context:
Even frontier models hallucinate JSON keys or add markdown fences \(\`\`\`json\) around tool arguments, breaking downstream parsers. We tried simply asking 'return valid JSON' but that fails ~5% of the time with complex nested schemas. Constrained generation \(using libraries like Outlines or OpenAI's JSON mode\) eliminates syntax errors entirely by masking logits to valid tokens. However, not all APIs support this. For those cases, the 'parse-then-retry' pattern is essential: catch the JSONDecodeError, append the error message to the prompt \('Your previous response was invalid JSON: Expecting ',' delimiter...'\), and retry. This recovers 90%\+ of parse errors. The tradeoff with constrained generation is sometimes slightly lower 'creativity' or naturalness in the values, but for tool arguments, strictness is paramount.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T07:30:57.306315+00:00— report_created — created