High severity intermediate · Fix: 2-5 min

OutputParserException

deepseek.exceptions.OutputParserException

What this error means
DeepSeek's JSON mode parser failed because the LLM response did not match the expected strict JSON format.

Stack trace

traceback
deepseek.exceptions.OutputParserException: Could not parse LLM output as JSON: Unexpected token at line 1 column 1
  File "/app/deepseek/parser.py", line 87, in parse
    return json.loads(response)
  File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
QUICK FIX
Use DeepSeek's JsonOutputParser which automatically strips markdown fences and retries parsing on failure.

Why it happens

DeepSeek's JSON mode expects the LLM to return a strictly formatted JSON string without any extra text or markdown fences. If the LLM includes markdown code blocks, explanatory text, or deviates from the exact JSON schema, the parser raises this exception due to invalid JSON syntax.

Detection

Monitor parser exceptions by wrapping DeepSeek JSON parsing calls in try/except OutputParserException and log the raw LLM output to detect format mismatches early.

Causes & fixes

1

LLM response includes markdown fences (e.g., ```json) around the JSON output

✓ Fix

Modify the prompt to instruct the LLM to return raw JSON only, or use DeepSeek's built-in JSON parser that strips markdown fences automatically.

2

LLM returns additional explanatory text or preamble before or after the JSON

✓ Fix

Update the prompt to explicitly request only the JSON object with no extra text or comments.

3

Using a base model that does not reliably follow output format instructions

✓ Fix

Switch to an instruction-tuned model supported by DeepSeek that enforces strict JSON output.

Code: broken vs fixed

Broken - triggers the error
python
import os
from deepseek import DeepSeekClient

client = DeepSeekClient(api_key=os.environ['DEEPSEEK_API_KEY'])

response = client.chat.completions.create(
    model="deepseek-3-json",
    messages=[{"role": "user", "content": "Return data in JSON format."}]
)

# This line raises OutputParserException due to markdown fences
parsed = client.parsers.json.parse(response.choices[0].message.content)
print(parsed)
Fixed - works correctly
python
import os
from deepseek import DeepSeekClient
from deepseek.parsers import JsonOutputParser

client = DeepSeekClient(api_key=os.environ['DEEPSEEK_API_KEY'])

response = client.chat.completions.create(
    model="deepseek-3-json",
    messages=[{"role": "user", "content": "Return ONLY raw JSON, no markdown fences."}]
)

# Use JsonOutputParser to handle markdown fences and parse safely
parser = JsonOutputParser()
parsed = parser.parse(response.choices[0].message.content)
print(parsed)  # Successfully parsed JSON output
Added JsonOutputParser which strips markdown fences and enforces strict JSON parsing, preventing OutputParserException.

Workaround

Catch OutputParserException, then use regex to extract JSON substring from the raw LLM response and parse it manually with json.loads() as a fallback.

Prevention

Design prompts to request structured outputs using DeepSeek's JSON mode with explicit instructions to return raw JSON only, and use instruction-tuned models that reliably follow format constraints.

Python 3.9+ · deepseek >=3.0.0 · tested on 3.2.1
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.