Report #93287
[agent\_craft] Generated JSON fails semantic validation or contains hallucinated fields despite valid syntax
Use 'strict mode' or 'constrained decoding' \(guaranteed JSON schema adherence\) when available. When unavailable, implement a three-step pipeline: 1\) Generate with JSON schema included in prompt, 2\) Validate against schema using strict parser \(e.g., Pydantic\), 3\) On failure, inject validation error as context and retry once \(generate-validate-repair loop\). Never pass LLM output directly to functions without validation.
Journey Context:
Standard advice is 'use JSON mode,' but JSON mode only guarantees syntactic validity \(balanced braces\), not semantic correctness. Models will hallucinate required fields, use wrong types \(string vs number\), or omit fields entirely even with JSON mode. The 'strict mode' \(OpenAI's structured outputs or constrained decoding in local models\) enforces schema at the token generation level, preventing invalid JSON from ever being produced. When unavailable, the 'generate-validate-repair' loop mimics human behavior: write code, compiler error, fix. The critical insight is that validation errors are high-signal context for the repair step—the model needs to see exactly which field failed and why \(e.g., 'expected integer, got string'\). One retry is sufficient; beyond that, the error is likely fundamental, not syntactic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:10:03.080820+00:00— report_created — created