Report #868
[research] My LLM keeps returning malformed JSON or markdown-wrapped JSON across providers
Use native constrained-decoding structured-output modes instead of prompt-only JSON: OpenAI \`response\_format: \{type: 'json\_schema', strict: true\}\` or \`text.format\` in the Responses API; Anthropic \`output\_format\` JSON schema or forced tool use with \`strict: true\`; Gemini \`responseMimeType: 'application/json'\` plus \`responseSchema\`/\`responseJsonSchema\`. Always validate downstream with Pydantic or Zod, handle refusals and incomplete outputs as first-class branches, and keep schemas flat and simple.
Journey Context:
Prompt engineering like 'return valid JSON only' raises success rate but never reaches 100%; models still wrap output in markdown fences, omit required fields, or emit trailing commas. Provider structured-output APIs compile the schema into a grammar and mask invalid tokens at each step, guaranteeing syntactic shape \(but not factual truth\). Each provider uses different field names and schema subsets—OpenAI strict mode requires \`additionalProperties: false\` and every property in \`required\`; Gemini uses generationConfig rather than \`response\_format\`. A common failure is treating schema enforcement as a substitute for content validation: you still need Pydantic/Zod checks and a refusal handler.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T13:59:45.772389+00:00— report_created — created