OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{...}`
File "/app/main.py", line 42, in run_chain
result = chain.invoke(inputs)
File "/usr/local/lib/python3.10/site-packages/langchain_core/chains/base.py", line 123, in invoke
parsed = self.output_parser.parse(result)
File "/usr/local/lib/python3.10/site-packages/langchain_core/schema/output_parser.py", line 78, in parse
raise OutputParserException(f"Could not parse LLM output: `{text}`") 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 markdown fences, preamble text, or a slightly different structure, the parser raises this exception. This is common with non-instruction-tuned models or when the prompt doesn't clearly specify the exact output format.
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.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.schema.output_parser import PydanticOutputParser
prompt = PromptTemplate(template="{input}", input_variables=["input"])
class MyOutputModel(BaseModel):
answer: str
parser = PydanticOutputParser(pydantic_object=MyOutputModel)
chain = LLMChain(llm=llm, prompt=prompt, output_parser=parser)
result = chain.invoke({"input": "What is 2+2?"}) # Raises OutputParserException here import os
from pydantic import BaseModel
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.schema.output_parser import JsonOutputParser
from langchain_openai import ChatOpenAI
prompt = PromptTemplate(template="{input}", input_variables=["input"])
class MyOutputModel(BaseModel):
answer: str
parser = JsonOutputParser() # Changed to JsonOutputParser to handle markdown fences
llm = ChatOpenAI(model="gpt-4o-mini") # Use instruction-tuned model
chain = LLMChain(llm=llm, prompt=prompt, output_parser=parser)
result = chain.invoke({"input": "What is 2+2?"}) # Fixed: no OutputParserException
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.