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 JSON schema or structured format.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{
  "name": "John Doe",
  "age": "twenty-five"
}`

ValueError: field 'age' expected type int but got str
QUICK FIX
Add JsonOutputParser() which strips markdown fences and retries parsing automatically on malformed JSON.

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

1

LLM returned JSON wrapped in markdown fences (```json ... ```) causing parsing failure

✓ Fix

Add explicit prompt instructions to return only raw JSON without markdown fences or use JsonOutputParser which automatically strips fences.

2

Pydantic schema field types do not match the actual LLM output types (e.g., string instead of int)

✓ Fix

Ensure your Pydantic model field types exactly match the expected output and add validation in your prompt to enforce correct types.

3

LLM output includes extra unexpected fields not defined in the schema

✓ Fix

Update your schema to allow extra fields with `extra = 'allow'` or modify the prompt to restrict output strictly to the schema fields.

4

Using a base model that ignores output format instructions and returns free-form text

✓ Fix

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

Broken - triggers the error
python
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
Fixed - works correctly
python
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
Replaced PydanticOutputParser with JsonOutputParser which strips markdown fences and switched to an instruction-tuned model to ensure strict JSON output.

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.

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.