OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{\n "name": "John Doe",\n "age": "twenty-five"\n}`
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app.py", line 42, in <module>
result = chain.invoke(inputs)
File "/usr/local/lib/python3.10/site-packages/langchain_core/chains/base.py", line 120, in invoke
output = self.output_parser.parse(result)
File "/usr/local/lib/python3.10/site-packages/langchain_core/schema/output_parser.py", line 85, 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, often a Pydantic schema. If the LLM returns data that violates the schema, such as wrong data types or unexpected fields, the parser raises this exception. This often happens when the prompt does not enforce strict output formatting or the LLM output contains unexpected values.
Detection
Wrap parser calls in try/except OutputParserException and log the raw LLM response to identify schema mismatches before retrying or failing.
Causes & fixes
LLM returned data with incorrect types that violate the Pydantic schema (e.g., string instead of int).
Ensure your prompt instructs the LLM to return data matching the schema types exactly, and validate your Pydantic model fields to match expected types.
The Pydantic schema field names do not match the keys returned by the LLM output.
Align your Pydantic model field names exactly with the field names in your prompt template, including case sensitivity.
The LLM output includes extra text or formatting (like markdown fences) around the structured data.
Use LangChain's JsonOutputParser or add instructions in your prompt to return only raw JSON without markdown fences.
Using a base model that does not reliably follow output format instructions.
Switch to an instruction-tuned model such as gpt-4o-mini or claude-3-5-haiku-20241022 that better respects output formatting.
Code: broken vs fixed
from langchain import LLMChain
from langchain.prompts import PromptTemplate
from langchain.output_parsers import PydanticOutputParser
class Person(BaseModel):
name: str
age: int
prompt = PromptTemplate(template="Provide a JSON with name and age.", input_variables=[])
parser = PydanticOutputParser(pydantic_object=Person)
chain = LLMChain(llm=llm, prompt=prompt, output_parser=parser)
result = chain.invoke({}) # Raises OutputParserException due to validation error import os
from langchain import LLMChain
from langchain.prompts import PromptTemplate
from langchain.output_parsers import JsonOutputParser
from pydantic import BaseModel
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") # Use environment variable for API key
class Person(BaseModel):
name: str
age: int
prompt = PromptTemplate(template="Provide ONLY raw JSON with name and age, no markdown fences.", input_variables=[])
parser = JsonOutputParser() # Changed to JsonOutputParser to handle markdown fences
chain = LLMChain(llm=llm, prompt=prompt, output_parser=parser)
result = chain.invoke({})
print(result) # Works without OutputParserException Workaround
Wrap the parser call in try/except OutputParserException, then extract JSON from the raw LLM output using regex and parse it manually 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.