High severity intermediate · Fix: 2-5 min

OutputParserException

langchain.schema.output_parser.OutputParserException

What this error means
LangChain's output parser failed because the OpenAI LLM response was not valid JSON matching the expected schema.

Stack trace

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

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "app.py", line 42, in <module>
    result = parser.parse(llm_response)  # <-- raises OutputParserException
  File "/usr/local/lib/python3.9/site-packages/langchain_core/schema/output_parser.py", line 123, in parse
    raise OutputParserException(f"Could not parse LLM output: {text}")
langchain_core.exceptions.OutputParserException: Could not parse LLM output: ```json
{"name": "John", "age": 30}
```
QUICK FIX
Use LangChain's JsonOutputParser() which strips markdown fences and retries parsing automatically.

Why it happens

LangChain's output parsers expect the LLM to respond in a strict JSON format without markdown fences or extra text. When the OpenAI model returns JSON wrapped in markdown code blocks or with unexpected formatting, the parser fails to decode it, raising this exception.

Detection

Wrap calls to the output parser in try/except OutputParserException and log the raw LLM output to detect formatting issues before the app crashes.

Causes & fixes

1

LLM returned JSON wrapped in markdown fences (```json ... ```)

✓ Fix

Modify the prompt to instruct the model to return raw JSON only, or use LangChain's JsonOutputParser which automatically strips markdown fences.

2

Pydantic schema field names do not match the JSON keys returned by the LLM

✓ Fix

Ensure your Pydantic model field names exactly match the expected JSON keys, including case sensitivity.

3

Using a base OpenAI model that ignores output format instructions

✓ Fix

Switch to an instruction-tuned model like gpt-4o-mini or claude-3-5-haiku-20241022 that reliably follows output format instructions.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.schema.output_parser import PydanticOutputParser

parser = PydanticOutputParser(pydantic_object=MyModel)
llm_response = '''```json
{"name": "John", "age": 30}
```'''

result = parser.parse(llm_response)  # Raises OutputParserException due to markdown fences
Fixed - works correctly
python
import os
from langchain.schema.output_parser import JsonOutputParser

parser = JsonOutputParser()
llm_response = '''```json
{"name": "John", "age": 30}
```'''

result = parser.parse(llm_response)  # Works: JsonOutputParser strips fences and parses JSON
print(result)

# Note: API keys should be set in environment variables like os.environ['OPENAI_API_KEY']
Replaced PydanticOutputParser with JsonOutputParser which automatically removes markdown fences and parses JSON correctly.

Workaround

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

Prevention

Use OpenAI's response_format parameter to request structured JSON output directly from the API, or use instruction-tuned models that reliably follow output format instructions to avoid parser errors.

Python 3.9+ · langchain-core >=0.1.0 · tested on 0.2.x
Verified 2026-04 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗

Community Notes

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