OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
langchain_core.exceptions.OutputParserException: Could not parse LLM output: ```json
{"name": "John", "age": 30}
```
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app.py", line 42, in <module>
result = parser.parse(llm_response) # <-- raises OutputParserException
File "/usr/local/lib/python3.9/site-packages/langchain_core/schema/output_parser.py", line 123, in parse
raise OutputParserException(f"Could not parse LLM output: {text}")
langchain_core.exceptions.OutputParserException: Could not parse LLM output: ```json
{"name": "John", "age": 30}
``` Why it happens
LangChain's output parsers expect the LLM to respond in a strict JSON format without markdown fences or extra text. When the OpenAI model returns JSON wrapped in markdown code blocks or with unexpected formatting, the parser fails to decode it, raising this exception.
Detection
Wrap calls to the output parser in try/except OutputParserException and log the raw LLM output to detect formatting issues before the app crashes.
Causes & fixes
LLM returned JSON wrapped in markdown fences (```json ... ```)
Modify the prompt to instruct the model to return raw JSON only, or use LangChain's JsonOutputParser which automatically strips markdown fences.
Pydantic schema field names do not match the JSON keys returned by the LLM
Ensure your Pydantic model field names exactly match the expected JSON keys, including case sensitivity.
Using a base OpenAI model that ignores output format instructions
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
from langchain.schema.output_parser import PydanticOutputParser
parser = PydanticOutputParser(pydantic_object=MyModel)
llm_response = '''```json
{"name": "John", "age": 30}
```'''
result = parser.parse(llm_response) # Raises OutputParserException due to markdown fences import os
from langchain.schema.output_parser import JsonOutputParser
parser = JsonOutputParser()
llm_response = '''```json
{"name": "John", "age": 30}
```'''
result = parser.parse(llm_response) # Works: JsonOutputParser strips fences and parses JSON
print(result)
# Note: API keys should be set in environment variables like os.environ['OPENAI_API_KEY'] Workaround
Catch OutputParserException, extract JSON substring using regex from the raw LLM output, 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 instruction-tuned models that reliably follow output format instructions to avoid parser errors.