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"}`
  File "/usr/local/lib/python3.9/site-packages/langchain/schema/output_parser.py", line 123, in parse
    return self.pydantic_model.parse_raw(text)
  File "/usr/local/lib/python3.9/site-packages/pydantic/main.py", line 341, in parse_raw
    raise ValidationError(errors, cls) from e
pydantic.error_wrappers.ValidationError: 1 validation error for User
age
  value is not a valid integer (type=type_error.integer)
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 Pydantic schema. When the LLM returns data that does not conform exactly: such as wrong types, extra text, or markdown fences: 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 mismatches in expected schema before retrying or alerting.

Causes & fixes

1

LLM returned JSON wrapped in markdown fences (```json ... ```), causing parse failure

✓ Fix

Add instructions to the prompt to return only raw JSON without markdown fences, or use LangChain's JsonOutputParser which strips fences automatically.

2

Pydantic model field types do not match the actual LLM output types (e.g., string instead of int)

✓ Fix

Ensure your Pydantic model field types exactly match the expected output types and validate the prompt to produce correct types.

3

Field names in the Pydantic model do not match the keys in the LLM output JSON

✓ Fix

Align your Pydantic model field names exactly with the keys returned by the LLM, including case sensitivity.

4

Using a base LLM 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 import BaseOutputParser
from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int

parser = BaseOutputParser(pydantic_model=User)

llm_response = '```json\n{"name": "John", "age": "twenty"}\n```'

user = parser.parse(llm_response)  # This line raises OutputParserException
print(user)
Fixed - works correctly
python
import os
from langchain.output_parsers import JsonOutputParser
from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int

parser = JsonOutputParser(pydantic_model=User).with_retry(n=3)  # Changed to JsonOutputParser with retry

llm_response = '{"name": "John", "age": 20}'  # Ensure age is int and no markdown fences

user = parser.parse(llm_response)  # Now parses correctly
print(user)

# Note: Use os.environ for API keys in your actual LLM calls
Added JsonOutputParser() which handles markdown fences and retries on malformed JSON, ensuring the LLM output matches the Pydantic schema.
⚠

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
Verify ↗

Community Notes

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