Report #73453
[tooling] Agents fail to generate valid JSON or wrap responses in markdown despite explicit instructions
Define the desired output structure as an MCP tool schema with additionalProperties: false, set the tool description to 'Extracts data into structured format - do not wrap in markdown', and invoke the tool with the data as arguments rather than expecting the LLM to generate raw JSON in the content field
Journey Context:
The naive approach adds 'respond with JSON' to system prompts, then parses the output with regex to strip markdown fences. This fails because models reliably wrap JSON in \`\`\`json blocks when prompted for JSON in text generation mode. The robust solution inverts the control flow: instead of asking the model to generate text that happens to be JSON, use the function-calling mechanism. When a model invokes a tool, it must provide arguments that validate against the JSON Schema. This is enforced at the API level, not via prompt engineering. By setting additionalProperties: false, you prevent the model from hallucinating extra fields. The tool's implementation simply returns the arguments as the 'result', effectively using the tool call as a structured output parser. This eliminates markdown wrapping, ensures schema compliance, and works with all major function-calling models.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T05:53:12.833661+00:00— report_created — created