RetryOutputParserException
langchain.schema.output_parser.RetryOutputParserException
Stack trace
langchain.schema.output_parser.RetryOutputParserException: Max retries exceeded while attempting to parse LLM output: Could not parse LLM output: `{...}` Why it happens
LangChain's RetryOutputParserException occurs when the output parser repeatedly fails to parse the LLM response into the expected structured format within the allowed retry attempts. This usually happens because the LLM output does not conform to the strict schema or format defined by the parser, often due to ambiguous prompts, markdown fences, or unexpected text.
Detection
Wrap parser calls in try/except RetryOutputParserException and log the raw LLM output to detect repeated parsing failures before the exception crashes the app.
Causes & fixes
LLM output includes markdown fences or extra text around the expected JSON or structured data
Add explicit prompt instructions to return only raw JSON without markdown fences or use LangChain's JsonOutputParser which automatically strips markdown fences.
The Pydantic or JSON schema used by the output parser does not match the actual LLM output field names or structure
Ensure the parser schema field names and types exactly match the expected LLM output format, including case sensitivity and nested structures.
Using a base LLM model that is not instruction-tuned and ignores output format instructions
Switch to an instruction-tuned model such as gpt-4o-mini or claude-3-5-haiku-20241022 that reliably follows output format instructions.
Retry count is too low to handle occasional LLM output variations or transient parsing issues
Increase the retry count in the RetryOutputParser configuration to allow more attempts before failing.
Code: broken vs fixed
from langchain.schema.output_parser import JsonOutputParser
parser = JsonOutputParser()
response = llm(prompt)
parsed = parser.parse(response) # Raises RetryOutputParserException after max retries import os
from langchain.schema.output_parser import JsonOutputParser
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(model='gpt-4o-mini')
parser = JsonOutputParser().with_retry(n=3) # Added retry with fence handling
response = llm(prompt)
parsed = parser.parse(response)
print(parsed) # Works without RetryOutputParserException Workaround
Catch RetryOutputParserException, extract the raw LLM output string, manually remove markdown fences using regex, then parse the JSON 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, eliminating parser fragility and retry failures.