OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{
"name": "John Doe",
"age": "twenty-five"
}`
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)
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: `{
"name": "John Doe",
"age": "twenty-five"
}` Why it happens
LangChain's output parsers expect the LLM to respond in a specific structured format defined by your parser, such as a Pydantic schema. When the LLM returns output that deviates from this format, such as incorrect types, extra markdown fences, or mismatched field names, the parser raises this exception. This often happens if the prompt does not enforce strict output formatting or if the model is not instruction-tuned.
Detection
Wrap parser calls in try/except OutputParserException and log the raw LLM response to identify format mismatches before retrying or alerting.
Causes & fixes
LLM returned JSON wrapped in markdown fences (```json) instead of raw JSON
Add instructions in your prompt to return only raw JSON without markdown fences, or use a JsonOutputParser that automatically strips fences.
Pydantic schema field names do not exactly match the keys in the LLM output
Ensure your Pydantic model field names exactly match the expected output keys, including case sensitivity.
LLM returned values with incorrect types, e.g., string instead of integer
Add explicit type instructions in your prompt or validate and coerce types before parsing.
Using a base model that ignores output format instructions
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
from langchain.schema.output_parser import PydanticOutputParser
parser = PydanticOutputParser(pydantic_object=MyModel)
llm_response = '''```json
{"name": "John Doe", "age": "twenty-five"}
```'''
result = parser.parse(llm_response) # Raises OutputParserException due to markdown fences and type mismatch import os
from langchain.schema.output_parser import JsonOutputParser, PydanticOutputParser
from pydantic import BaseModel
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY') # Use env var for API key
class MyModel(BaseModel):
name: str
age: int
# Use JsonOutputParser to handle markdown fences automatically
json_parser = JsonOutputParser()
llm_response = '''```json
{"name": "John Doe", "age": 25}
```'''
# Strip fences and parse JSON first
clean_json = json_parser.parse(llm_response)
# Then parse with Pydantic model
parser = PydanticOutputParser(pydantic_object=MyModel)
result = parser.parse(clean_json)
print(result) # Works correctly Workaround
Catch OutputParserException, extract JSON substring from the raw LLM output using regex, then parse it manually with json.loads() before passing to Pydantic.
Prevention
Use structured output features like OpenAI's response_format or Anthropic's tool use to enforce schema-valid responses at the API level, avoiding parser fragility.