Prompt
Why is Anthropic not returning valid JSON?
Latest observation
Oct 9, 2026
OpenAI APIWeb search: off
Common reasons Anthropic outputs “almost JSON” instead of valid JSON:
-
You didn’t force strict JSON mode
- If you ask for JSON in plain text, the model may add markdown fences, comments, or trailing text.
- Use a response format / tool schema if your API wrapper supports it.
-
The prompt is ambiguous
- If examples include prose, explanations, or code fences, the model may imitate that.
- Be explicit: “Return only raw JSON. No markdown. No code fences. No extra text.”
-
The schema is underspecified or conflicting
- If you say “include optional fields” but also require all fields, the model may improvise.
- Keep field names, types, and nesting very clear.
-
The model is trying to be helpful
- It may add natural-language clarifications before/after the JSON unless constrained.
-
Streaming/parsing issues
- Sometimes the model output is fine, but your client reads partial chunks or concatenates them incorrectly.
-
Post-processing is altering the output
- Middleware, logging, or templating may insert text around the JSON.
-
The output is valid JSON in spirit, but not strict JSON
- Common issues:
- trailing commas
- single quotes instead of double quotes
- unescaped newlines in strings
- comments
- NaN/Infinity
- Common issues:
How to fix it:
- Use a strict JSON response mechanism if available.
- Add a system prompt like:
- “You must output only valid JSON, with no markdown or extra text.”
- Provide a concrete schema and one minimal example.
- Validate on the client side and retry if parsing fails.
- Lower temperature if the model is being creative.
If you want, paste:
- your exact prompt,
- a sample response,
- and the API call code,
and I can tell you exactly why it’s breaking.
Brands