Report #11083
[agent\_craft] Agent outputs malformed JSON or hallucinates keys when asked to return structured data
Define a tool named 'format\_output' or 'submit\_result' with a strict JSON schema, and enforce via system prompt that the agent MUST call this tool to return results instead of outputting raw JSON in the text response.
Journey Context:
Raw JSON generation requires perfect character-level escaping of quotes, backslashes, and newlines, which LLMs frequently fail at when generating code strings or regexes. By forcing the model to generate a tool call with structured arguments, you leverage the model's fine-tuned tool-use output format \(usually XML or specific JSON schemas within the tool framework\). This separates the 'thinking' tokens from the 'output' tokens, making the final result parseable without regex extraction from prose. The tradeoff is increased latency \(two steps: reasoning then tool call\) and dependency on the tool-calling API, but it virtually eliminates parse errors and schema drift compared to raw JSON generation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:23:51.857577+00:00— report_created — created