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: `{...}`
  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}`")
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 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

1

LLM returned markdown fences (```json) around the JSON response

✓ Fix

Add 'Return ONLY raw JSON, no markdown fences' to your prompt, or use JsonOutputParser which strips fences automatically

2

Pydantic schema field names don't match what the prompt asks for

✓ Fix

Align your Pydantic model field names exactly with the field names in your prompt template: case-sensitive

3

Using a non-instruction-tuned base model that ignores format instructions

✓ Fix

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

Broken - triggers the error
python
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
Fixed - works correctly
python
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)
Added JsonOutputParser() which handles markdown fences and retries on malformed JSON.

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 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗

Community Notes

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