High severity intermediate · Fix: 2-5 min

OutputParserException

langchain.schema.output_parser.OutputParserException

What this error means
LangChain's output parser received an LLM response that doesn't match the expected format or schema.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{"name": "John", "age": "twenty"}`
    at langchain.schema.output_parser.OutputParser.parse (output_parser.py:45)
    at my_app.main (main.py:27)
QUICK FIX
Add JsonOutputParser().with_retry(n=3) to auto-retry on parse failures with format correction.

Why it happens

LangChain's output parsers expect the LLM to respond in a specific structured format defined by your parser (Pydantic schema, JSON schema, or regex). When the LLM returns data that does not conform to the expected schema, such as wrong field types or extra fields, the parser raises this exception. This often happens when the prompt does not clearly specify the output format or the model ignores format instructions.

Detection

Wrap parser calls in try/except OutputParserException and log the raw LLM response to identify the format mismatch before retrying.

Causes & fixes

1

LLM returned markdown fences (```json) around the JSON response

✓ Fix

Add 'Return ONLY raw JSON, no markdown fences' to your prompt, or use JsonOutputParser which strips fences automatically

2

Pydantic schema field names don't match what the prompt asks for

✓ Fix

Align your Pydantic model field names exactly with the field names in your prompt template: case-sensitive

3

Using a non-instruction-tuned base model that ignores format instructions

✓ Fix

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

Code: broken vs fixed

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

parser = PydanticOutputParser(pydantic_object=MyModel)
llm = ChatOpenAI(model='gpt-4o')

response = llm.call(prompt)
result = parser.parse(response)  # Raises OutputParserException here
Fixed - works correctly
python
import os
from langchain.schema.output_parser import JsonOutputParser
from langchain_openai import ChatOpenAI

os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY', '')  # Use environment variable

parser = JsonOutputParser().with_retry(n=3)  # Changed to JsonOutputParser with retry
llm = ChatOpenAI(model='gpt-4o-mini')  # Use instruction-tuned model

response = llm.call(prompt)
result = parser.parse(response)  # Now parses correctly
print(result)
Added JsonOutputParser() which handles markdown fences and retries on malformed JSON.

Workaround

Wrap the parser call in try/except OutputParserException, catch the exception, extract JSON from the raw LLM string using regex, and parse with json.loads() as a fallback.

Prevention

Use structured outputs via OpenAI's response_format or Anthropic's tool use to guarantee schema-valid responses at the API level, bypassing parser fragility entirely.

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.