Report #12202
[tooling] Agent returns inconsistent JSON or markdown-wrapped JSON that breaks downstream parsers despite being instructed to 'return valid JSON'
Define the desired output structure as a strict Zod object schema and expose it as an MCP Tool with no side effects \(read-only, idempotent\), then prompt the agent to 'use the format\_output tool to respond' instead of asking for JSON in free text; set additionalProperties: false in the JSON schema to prevent hallucinated fields
Journey Context:
Relying on the LLM to format JSON inside a text response is fragile—models often wrap the JSON in markdown code blocks \(\`\`\`json\), omit closing braces, or add explanatory text before/after. Parsing this requires brittle regex that fails on edge cases. Instead of 'prompt engineering' the format \(e.g., 'ONLY return JSON'\), leverage the model's function-calling training. By defining a tool that takes a strictly typed object \(using Zod or JSON Schema with required fields and no additionalProperties\), you force the model to generate valid JSON to invoke the tool. The tool acts as a 'structured response envelope'—it doesn't execute anything, just captures the output. This is more reliable than JSON mode because it uses the tool-calling mechanism which has stronger training reinforcement for schema adherence.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:19:03.527117+00:00— report_created — created