OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{
"name": "John Doe",
"age": "twenty-five"
}`
ValueError: field 'age' expected type int but got str Why it happens
LangChain's output parsers expect the LLM to respond in a specific structured format defined by your JSON or Pydantic schema. When the LLM returns data that violates the schema, such as wrong types or extra fields, the parser raises this exception. This often happens if the prompt does not enforce strict output formatting or the model outputs markdown fences or extra text.
Detection
Wrap your parser calls in try/except OutputParserException and log the raw LLM response to detect schema mismatches before retrying or failing.
Causes & fixes
LLM returned JSON wrapped in markdown fences (```json ... ```) causing parsing failure
Add explicit prompt instructions to return only raw JSON without markdown fences or use JsonOutputParser which automatically strips fences.
Pydantic schema field types do not match the actual LLM output types (e.g., string instead of int)
Ensure your Pydantic model field types exactly match the expected output and add validation in your prompt to enforce correct types.
LLM output includes extra unexpected fields not defined in the schema
Update your schema to allow extra fields with `extra = 'allow'` or modify the prompt to restrict output strictly to the schema fields.
Using a base model that ignores output format instructions and returns free-form text
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.output_parsers import PydanticOutputParser
from langchain_openai import ChatOpenAI
parser = PydanticOutputParser(pydantic_object=MySchema)
client = ChatOpenAI(model_name="gpt-4o")
response = client.call(messages=[{"role": "user", "content": "Generate JSON output"}])
parsed = parser.parse(response) # Raises OutputParserException due to markdown fences import os
from langchain.output_parsers import JsonOutputParser
from langchain_openai import ChatOpenAI
parser = JsonOutputParser() # Changed to JsonOutputParser to handle markdown fences
client = ChatOpenAI(model_name="gpt-4o-mini") # Use instruction-tuned model
response = client.call(messages=[{"role": "user", "content": "Return ONLY raw JSON, no markdown fences"}])
parsed = parser.parse(response)
print(parsed) # Works without OutputParserException Workaround
Catch OutputParserException, extract JSON substring from raw LLM output using regex, 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 Anthropic's tool use feature to guarantee schema-valid responses, avoiding parser errors.