High severity intermediate · Fix: 2-5 min

OutputParserException

langchain.schema.output_parser.OutputParserException

What this error means
LangChain's ContextualCompressionRetriever failed because the LLM output did not match the expected structured format required for reranking.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{"score": 0.87, "text": "Some compressed context"}`
  File "/usr/local/lib/python3.9/site-packages/langchain/retrievers/contextual_compression.py", line 123, in _compress
    parsed = self.output_parser.parse(output)
  File "/usr/local/lib/python3.9/site-packages/langchain/schema/output_parser.py", line 45, in parse
    raise OutputParserException(f"Could not parse LLM output: `{text}`")
QUICK FIX
Add a JsonOutputParser() to the ContextualCompressionRetriever to handle markdown fences and enforce strict JSON parsing.

Why it happens

The ContextualCompressionRetriever expects the LLM to return a response strictly matching the output parser's schema, often JSON with specific fields. If the LLM returns extra text, markdown fences, or a different structure, the parser raises OutputParserException. This often happens if the prompt does not enforce strict output formatting or if the model is not instruction-tuned.

Detection

Catch OutputParserException around the retriever call and log the raw LLM output to detect format mismatches before the retriever fails completely.

Causes & fixes

1

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

✓ Fix

Modify the prompt to instruct the model to return only raw JSON without markdown fences or use a JsonOutputParser that automatically strips fences.

2

Output parser schema fields do not match the keys returned by the LLM

✓ Fix

Ensure the Pydantic or JSON schema used by the output parser exactly matches the field names and types the LLM is instructed to return.

3

Using a base LLM model that ignores output format instructions

✓ 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.retrievers import ContextualCompressionRetriever
retriever = ContextualCompressionRetriever(llm=model, output_parser=None)
compressed = retriever.get_relevant_documents(query)  # Raises OutputParserException here
Fixed - works correctly
python
import os
from langchain.retrievers import ContextualCompressionRetriever
from langchain.output_parsers import JsonOutputParser

retriever = ContextualCompressionRetriever(
    llm=model,
    output_parser=JsonOutputParser()  # Added parser to handle JSON with fences
)
compressed = retriever.get_relevant_documents(query)
print(compressed)  # Works without parsing error
Added JsonOutputParser() which strips markdown fences and enforces strict JSON parsing, preventing OutputParserException.

Workaround

Wrap the retriever call in try/except OutputParserException, then extract JSON manually from the raw LLM output using regex and parse it with json.loads() as a fallback.

Prevention

Use structured output formats supported by the LLM API or instruction-tuned models and always specify strict output parsers like JsonOutputParser to guarantee schema compliance.

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.