Report #21390
[agent\_craft] Agent fails to follow strict output schema \(XML tags, JSON mode\) causing parser failures
Implement 'schema-first prompting with forced delimiters': For JSON output, use the API's JSON mode \(response\_format=\{type:'json\_object'\}\) or constrained decoding if available. For XML/structured text, use 'few-shot with sentinel tokens': Provide examples showing exact tag placement, then end the prompt with an incomplete starting tag \(e.g., ''\) to force the model to continue in that format. Validate output against schema before parsing, and on failure, retry with a 'format reminder' in the prompt.
Journey Context:
Agents often choke when required to output valid JSON with specific keys or XML with specific tags. Simple instructions like 'output JSON' are insufficient—models may wrap it in markdown fences \(\`\`\`json\) or omit required fields. JSON mode forces valid JSON but not schema compliance. The 'incomplete tag' technique exploits the model's completion tendency to maintain format consistency. The validation-retry loop is crucial because even with these techniques, hallucinated keys or malformed XML occur in long generations. Without this, downstream parsers throw exceptions and the agent crashes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T14:18:45.563207+00:00— report_created — created