OutputParserException
deepseek_chat.exceptions.OutputParserException
Stack trace
deepseek_chat.exceptions.OutputParserException: Could not parse reasoning model output: `{"reasoning": "The answer is 42"}`
File "/app/deepseek_client.py", line 45, in parse_output
parsed = parser.parse(raw_output) # <-- error here
File "/usr/local/lib/python3.10/site-packages/deepseek_chat/exceptions.py", line 22, in parse
raise OutputParserException(f"Could not parse reasoning model output: `{output}`") Why it happens
DeepSeek's output parser expects the reasoning model to return a strictly formatted JSON or structured response. If the model returns extra text, markdown fences, or deviates from the schema, the parser raises this exception. This often happens when prompt instructions are unclear or the model is not instruction-tuned.
Detection
Catch OutputParserException around the parsing call and log the raw model output to detect format mismatches before the application crashes.
Causes & fixes
The reasoning model output includes markdown fences or extra explanatory text around the JSON.
Update the prompt to instruct the model to return only raw JSON without markdown fences, or use a parser that strips markdown automatically.
The output JSON keys do not match the expected schema field names exactly.
Ensure the prompt and the Pydantic or JSON schema used for parsing have matching field names and casing.
Using a base model that is not instruction-tuned, causing it to ignore output format instructions.
Switch to an instruction-tuned model like deepseek-reasoner-v3 or similar that reliably follows output format instructions.
Code: broken vs fixed
from deepseek_chat import DeepSeekClient
client = DeepSeekClient()
raw_output = client.reasoning_model("What is the answer to life?")
parsed = client.parse_output(raw_output) # This line raises OutputParserException
print(parsed) import os
from deepseek_chat import DeepSeekClient, JsonOutputParser
os.environ["DEEPSEEK_API_KEY"] = os.environ["DEEPSEEK_API_KEY"]
client = DeepSeekClient()
raw_output = client.reasoning_model("What is the answer to life? Return ONLY raw JSON without markdown fences.")
parser = JsonOutputParser().with_retry(n=3) # Added parser that strips fences and retries
parsed = parser.parse(raw_output)
print(parsed) # Works without parse error Workaround
Wrap the parsing call in try/except OutputParserException, then extract JSON from the raw output using regex and parse it manually with json.loads() as a fallback.
Prevention
Use structured output features at the API level, such as DeepSeek's response_format options, to guarantee schema-valid responses and avoid parser errors.