High severity intermediate · Fix: 2-5 min

OutputParserException

langchain.schema.output_parser.OutputParserException

What this error means
Mistral LLM JSON mode output parse failure occurs when the returned response does not match the expected strict JSON format required by the parser.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: ```json
{"name": "John", "age": 30}
```

During handling of the above exception, the program crashed.
QUICK FIX
Use JsonOutputParser() which strips markdown fences and retries parsing to handle Mistral's JSON output format flexibly.

Why it happens

Mistral models in JSON mode sometimes return output wrapped in markdown fences or include extra text, causing strict JSON parsers to fail. The parser expects raw JSON without any formatting or preamble, so any deviation triggers this exception.

Detection

Catch OutputParserException around the parser call and log the raw LLM output to detect unexpected markdown fences or formatting before retrying or cleaning the response.

Causes & fixes

1

Mistral returns JSON output wrapped in markdown fences (```json ... ```)

✓ Fix

Modify the prompt to instruct Mistral to return raw JSON only, or use a parser like JsonOutputParser that automatically strips markdown fences.

2

Extra explanatory text or preamble is included before or after the JSON output

✓ Fix

Refine the prompt to explicitly request only the JSON object without any additional text or commentary.

3

Mismatch between expected JSON schema and actual keys or structure returned by Mistral

✓ Fix

Align the expected Pydantic or JSON schema fields exactly with the keys Mistral outputs, including case sensitivity.

4

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

✓ Fix

Switch to an instruction-tuned Mistral variant or add stronger prompt instructions emphasizing strict JSON output.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage

client = ChatOpenAI(model_name='mistral-7b', temperature=0)
prompt = 'Return a JSON object with name and age.'
response = client([HumanMessage(content=prompt)])

# This line triggers OutputParserException due to markdown fences
parsed = json.loads(response.content)
Fixed - works correctly
python
import os
import json
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage
from langchain.output_parsers import JsonOutputParser

client = ChatOpenAI(model_name='mistral-7b', temperature=0)
prompt = 'Return ONLY raw JSON object with name and age, no markdown fences or extra text.'
response = client([HumanMessage(content=prompt)])

parser = JsonOutputParser()  # Handles markdown fences automatically
parsed = parser.parse(response.content)
print(parsed)  # Should print the parsed JSON dict
Added JsonOutputParser() which automatically strips markdown fences and retries parsing, fixing parse failures from Mistral's JSON output.

Workaround

Wrap the parsing call in try/except OutputParserException, then use regex to extract JSON substring from the raw response and parse it with json.loads() as a fallback.

Prevention

Use structured output modes or response_format parameters at the API level to enforce strict JSON output, avoiding parser errors from formatting inconsistencies.

Python 3.9+ · langchain-core >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

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