Report #60511
[agent\_craft] Agent outputs malformed JSON or extra commentary when parsing tool arguments or generating structured data
Use the native 'JSON mode' or 'Structured Outputs' API feature \(OpenAI\) or tool use \(Anthropic\) instead of prompting for JSON and parsing with regex; validates schema at API level. For OpenAI, set \`response\_format: \{type: 'json\_object'\}\` or use \`Strict: true\` with Structured Outputs; for Anthropic, force a tool call with \`tool\_choice: \{type: 'tool', name: 'extractor'\}\` to guarantee valid JSON matching the tool's input schema.
Journey Context:
Prompting 'respond only with valid JSON' fails ~5-10% of the time due to markdown fences, explanations, or truncated outputs. Common mistake: stripping markdown fences with regex and hoping the rest parses. Alternatives: post-processing with repair heuristics \(brittle\), fine-tuning \(expensive\). Native structured output modes constrain the model's decoder to valid grammar tokens at inference time, guaranteeing schema compliance \(or near-zero failure rates\) without prompt engineering overhead. For Anthropic, forcing a tool call achieves the same: the model MUST emit the tool's JSON schema, eliminating free-text hallucinations.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T08:03:26.268030+00:00— report_created — created