DeepSeek-R1 / Reasoning Model Breaks JSON Parse or Enters Infinite Loop
Resolve <think>...</think> preamble crashes in downstream JSON parsers, temperature=0 repetition loops, and exhausted token budgets.
Look at the raw response string before your script or workflow parses it. Which symptom matches?
curl -s http://127.0.0.1:11434/api/generate -d '{"model":"deepseek-r1:14b","stream":false,"prompt":"Return JSON {\"ok\":true}"}' | jq -r .responseRead Full Reproducible Guide →All 1 Terminal Diagnostic Probes in "DeepSeek-R1 / Reasoning Model Breaks JSON Parse or Enters Infinite Loop"
Complete static reference of every read-only probe command, baseline hint, and branch transition in this decision tree.
Look at the raw response string before your script or workflow parses it. Which symptom matches?
curl -s http://127.0.0.1:11434/api/generate -d '{"model":"deepseek-r1:14b","stream":false,"prompt":"Return JSON {\"ok\":true}"}' | jq -r .response- • Response starts with <think>...</think> followed by JSON, crashing JSON.parse()
→ sol-strip-think-tags - • Model repeats the same sentence ("Wait, let me double check...") endlessly until timeout
→ sol-r1-sampling-params - • Response ends inside <think> before the final answer or JSON is ever emitted
→ sol-r1-token-budget
3 Verified Root Causes, Fix Configs & Commands in "DeepSeek-R1 / Reasoning Model Breaks JSON Parse or Enters Infinite Loop"
Fix: Strip <think>...</think> Block Before Parsing JSON (Avoid Hard Grammar Locks)
sol-strip-think-tagsReasoning models emit chain-of-thought inside <think>...</think> before the final payload. Furthermore, forcing `"format": "json"` at token #0 suppresses the <think> phase and severely degrades reasoning accuracy.
- Allow the model to emit its natural <think>...</think> block without grammar-level `"format": "json"`.
- Strip everything up to `</think>` and optional markdown fences before calling JSON.parse().
export function parseReasoningJson(raw: string) {
const withoutThink = raw.replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
const cleanJson = withoutThink
.replace(/^\s*```(?:json)?\s*/i, '')
.replace(/\s*```\s*$/i, '')
.trim();
return JSON.parse(cleanJson);
}[BOUNDARY]If you only need fast deterministic extraction without chain-of-thought, use Qwen2.5-Instruct instead of DeepSeek-R1.
node -e 'const raw="<think>step 1</think>\n{\"ok\":true}"; console.log(JSON.parse(raw.replace(/<think>[\s\S]*?<\/think>/,"").trim()))'Fix: Set temperature = 0.6 and top_p = 0.95 (Never Use Greedy temperature = 0 on R1)
sol-r1-sampling-paramsAs documented in the official DeepSeek-R1 technical report, setting `temperature: 0` (greedy decoding) on quantized R1 distill models frequently traps the attention heads in self-doubt repetition loops.
- Set `temperature: 0.6` (within the 0.5–0.7 sweet spot) and `top_p: 0.95`.
- Add `repeat_penalty: 1.05` if using Q4_K_M quantization on long prompts.
{
"model": "deepseek-r1:14b",
"options": {
"temperature": 0.6,
"top_p": 0.95,
"repeat_penalty": 1.05,
"num_ctx": 16384
}
}[BOUNDARY]Also avoid adding a separate system prompt to R1; place all instructions inside the user message.
ollama show deepseek-r1:14b --parametersFix: Increase max_tokens / num_predict to >= 4096 to Accommodate <think> Overhead
sol-r1-token-budget`max_tokens` (or `num_predict`) counts BOTH the `<think>` tokens and the final answer tokens. If `max_tokens` is left at 512 or 1024, the model spends all 1024 tokens thinking and truncates before `</think>`.
- Set `num_predict: 4096` (or `max_tokens: 4096`) and `num_ctx: 16384` when calling DeepSeek-R1.
- Check that the response contains a closing `</think>` tag before passing it downstream.
if (rawResponse.includes('<think>') && !rawResponse.includes('</think>')) {
throw new Error('Reasoning model exhausted max_tokens inside <think> block before producing final answer');
}[BOUNDARY]A typical medium-complexity coding or triage prompt consumes 600–1,800 <think> tokens before emitting the final answer.
echo "Verified </think> closure guard"DeepSeek-R1 & Qwen2.5-Coder Local IDE Setup: <think> Token Budgets, FIM Autocomplete & Loop Fixes
Why using DeepSeek-R1 for tab autocomplete ruins IDE latency—and how to pair R1-14B (for architectural reasoning with 4K <think> budget) alongside Qwen2.5-Coder-7B (for sub-250ms FIM edits).
MacBook Pro M3 Max (36GB Unified) & NVIDIA RTX 4070 Ti Super (16GB VRAM)
Ollama 0.5.7 · Continue v1.0.x (VS Code) · deepseek-r1:14b · qwen2.5-coder:7b
100% Air-Gapped Source Code Privacy — All repository indexing (`bge-m3`), FIM tab completion, and multi-file refactoring stay strictly on localhost.