How to Fix 'output parsing error during development' in LangChain (Python)
When LangChain throws OutputParserException or a plain output parsing error during development, it usually means the model returned text that does not match the structure your parser expected. This shows up most often when you wire an LLM into an OutputParser, StructuredOutputParser, or PydanticOutputParser and the model adds extra prose, misses a field, or changes the format.
The Most Common Cause
The #1 cause is asking the model for structured output, then not constraining it tightly enough. In practice, the LLM returns something like a friendly explanation instead of valid JSON, and LangChain fails while parsing.
Here’s the broken pattern:
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field
class Ticket(BaseModel):
category: str = Field(description="Issue category")
priority: str = Field(description="Low, Medium, High")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
parser = PydanticOutputParser(pydantic_object=Ticket)
prompt = f"""
Classify this support ticket:
Customer cannot log in after password reset.
Return JSON only.
"""
result = llm.invoke(prompt)
ticket = parser.parse(result.content) # OutputParserException likely here
The model may return:
The issue is likely authentication-related.
{"category": "login", "priority": "high"}
That extra sentence is enough to trigger:
langchain_core.exceptions.OutputParserException: Failed to parse Ticket from completion ...
The fix is to give the model explicit formatting instructions and use them in the prompt:
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field
class Ticket(BaseModel):
category: str = Field(description="Issue category")
priority: str = Field(description="Low, Medium, High")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
parser = PydanticOutputParser(pydantic_object=Ticket)
format_instructions = parser.get_format_instructions()
prompt = f"""
Classify this support ticket:
Customer cannot log in after password reset.
{format_instructions}
"""
response = llm.invoke(prompt)
ticket = parser.parse(response.content)
print(ticket)
Broken vs fixed
| Broken | Fixed |
|---|---|
Return JSON only. | parser.get_format_instructions() |
| Model freehands the response | Model gets exact schema rules |
parser.parse(result.content) on messy text | parser.parse(response.content) on constrained output |
If you are using chat models, this gets even more reliable when you use LangChain’s structured output helpers instead of raw parsing.
Other Possible Causes
1. Temperature is too high
High temperature increases format drift. If you need strict structure, keep it at 0.
# Risky
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.8)
# Better
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
2. Your schema is ambiguous
If your fields are vague, the model guesses. That leads to missing keys or wrong types.
# Weak schema
class Result(BaseModel):
status: str
value: str
# Better schema
class Result(BaseModel):
status: str = Field(description="One of: approved, rejected, needs_review")
value: int = Field(description="Numeric score from 0 to 100")
3. You are parsing the wrong object type
A common mistake is passing the full message object or a list of messages into a parser that expects plain text.
response = llm.invoke(prompt)
# Wrong if parser expects string content only in your setup
parser.parse(response)
# Right
parser.parse(response.content)
With some chains, you should not call .parse() manually at all. Let the chain handle it:
chain = prompt | llm | parser
output = chain.invoke({"input": "Customer cannot log in"})
4. The model output contains markdown fences
LLMs often wrap JSON in triple backticks. Some parsers tolerate this poorly depending on setup.
```json
{"category":"login","priority":"high"}
If this keeps happening, add a post-processing step or use a parser that handles fenced JSON better.
```python
content = response.content.strip()
content = content.removeprefix("```json").removesuffix("```").strip()
ticket = parser.parse(content)
How to Debug It
- •
Print the raw model output
- •Don’t inspect only the parsed result.
- •Log
response.contentexactly as returned by the model.
print(repr(response.content)) - •
Check which parser is failing
- •
StrOutputParserusually won’t fail. - •
JsonOutputParser,PydanticOutputParser, andStructuredOutputParserare common failure points. - •Look for exceptions like:
- •
langchain_core.exceptions.OutputParserException - •
pydantic.ValidationError
- •
- •
- •
Validate against your schema manually
- •If you use Pydantic, try loading the raw output directly.
Ticket.model_validate_json(response.content)If that fails, your issue is format or field mismatch, not LangChain itself.
- •
Reduce variables
- •Set temperature to
0. - •Remove tool calls.
- •Remove extra prompt text.
- •Use a minimal prompt and test again.
- •Set temperature to
Prevention
- •Use explicit format instructions from LangChain parsers instead of telling the model “return JSON.”
- •Keep schemas narrow and unambiguous; define allowed values with descriptions or enums.
- •Test structured outputs with deterministic settings first:
- •
temperature=0 - •one input example
- •no tool calling until parsing works
- •
If you want fewer production incidents, treat structured output as an interface contract. The prompt is part of that contract, and LangChain will fail fast when the contract is loose enough for the model to improvise.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit