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: `{\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"
}`
QUICK FIX
Add JsonOutputParser() to your chain to handle markdown fences and enforce JSON parsing with retries.

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

1

LLM returned data with incorrect types that violate the Pydantic schema (e.g., string instead of int).

✓ Fix

Ensure your prompt instructs the LLM to return data matching the schema types exactly, and validate your Pydantic model fields to match expected types.

2

The Pydantic schema field names do not match the keys returned by the LLM output.

✓ Fix

Align your Pydantic model field names exactly with the field names in your prompt template, including case sensitivity.

3

The LLM output includes extra text or formatting (like markdown fences) around the structured data.

✓ Fix

Use LangChain's JsonOutputParser or add instructions in your prompt to return only raw JSON without markdown fences.

4

Using a base model that does not reliably follow output format instructions.

✓ Fix

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

Broken - triggers the error
python
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
Fixed - works correctly
python
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
Replaced PydanticOutputParser with JsonOutputParser and updated prompt to enforce raw JSON output, preventing schema validation errors from markdown fences or formatting.

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.

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.