High severity intermediate · Fix: 2-5 min

OutputParserException

langchain.schema.output_parser.OutputParserException

What this error means
LangChain's AgentFinish output parser failed because the LLM response did not match the expected structured format or schema.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{"reason": "some text", "action": "some action"}`
  File "/usr/local/lib/python3.9/site-packages/langchain/agents/agent.py", line 512, in parse
    raise OutputParserException(f"Could not parse LLM output: `{text}`")
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{...}`
QUICK FIX
Add JsonOutputParser() to your AgentFinish parser to handle markdown fences and retry parsing automatically.

Why it happens

LangChain's AgentFinish parser expects the LLM to return output strictly following a defined schema or format, often a JSON object with specific fields. If the LLM returns extra text, markdown fences, or deviates from the schema, the parser throws this exception. This often happens when prompt instructions are ambiguous or the model is not instruction-tuned.

Detection

Wrap calls to the AgentFinish parser in try/except OutputParserException and log the raw LLM output to detect format mismatches before the error crashes your app.

Causes & fixes

1

LLM output includes markdown fences or extra explanatory text around the JSON response

✓ Fix

Modify the prompt to instruct the model to return ONLY the raw JSON without markdown fences or extra text, or use a parser like JsonOutputParser that strips fences automatically.

2

The expected output schema fields do not match the actual keys returned by the LLM

✓ Fix

Ensure the Pydantic model or output parser schema field names exactly match the keys the LLM is instructed to produce, including case sensitivity.

3

Using a base model that ignores or poorly follows output format instructions

✓ Fix

Switch to an instruction-tuned model such as 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.agents import AgentExecutor

agent = AgentExecutor(...)
response = agent.run(input_text)  # Raises OutputParserException due to invalid format
Fixed - works correctly
python
import os
from langchain.agents import AgentExecutor
from langchain.output_parsers import JsonOutputParser

os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")  # Use env var for API key

agent = AgentExecutor(..., output_parser=JsonOutputParser())  # Added JsonOutputParser to handle format
response = agent.run(input_text)
print(response)  # Works without parsing error
Added JsonOutputParser() which automatically strips markdown fences and retries parsing, ensuring the LLM output matches the expected JSON format.

Workaround

Catch OutputParserException, extract the JSON substring from the raw LLM output using regex, then parse it manually with json.loads() as a fallback.

Prevention

Use structured output features like OpenAI's response_format or Anthropic's tool use to enforce schema-valid responses at the API level, avoiding parser errors.

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.