OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
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) 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
LLM returned markdown fences (```json) around the JSON response
Add 'Return ONLY raw JSON, no markdown fences' to your prompt, or use JsonOutputParser which strips fences automatically
Pydantic schema field names don't match what the prompt asks for
Align your Pydantic model field names exactly with the field names in your prompt template: case-sensitive
Using a non-instruction-tuned base model that ignores format instructions
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
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 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) 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.