OutputParserException
deepseek.exceptions.OutputParserException
Stack trace
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) 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
LLM response includes markdown fences (e.g., ```json) around the JSON output
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.
LLM returns additional explanatory text or preamble before or after the JSON
Update the prompt to explicitly request only the JSON object with no extra text or comments.
Using a base model that does not reliably follow output format instructions
Switch to an instruction-tuned model supported by DeepSeek that enforces strict JSON output.
Code: broken vs fixed
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) 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 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.