High severity intermediate · Fix: 2-5 min

OutputParserException

langchain.schema.output_parser.OutputParserException

What this error means
LangChain's output parser fails because the AI test mock OpenAI response does not match the expected structured format or schema.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{"result": "success"}`
  File "/app/main.py", line 42, in run_test
    parsed = parser.parse(response)
  File "/usr/local/lib/python3.9/site-packages/langchain/schema/output_parser.py", line 123, in parse
    raise OutputParserException(f"Could not parse LLM output: `{text}`")
QUICK FIX
Adjust the mock OpenAI response to exactly match the expected JSON schema without extra formatting or invalid syntax.

Why it happens

When using AI test mocks for OpenAI responses, the mock data often lacks the exact formatting or fields expected by LangChain's output parser. This mismatch causes the parser to raise an OutputParserException because it cannot validate or deserialize the mock response into the defined schema.

Detection

Catch OutputParserException during test runs and log the raw mock response to verify if it matches the expected output format before parsing.

Causes & fixes

1

Mock response JSON structure does not match the parser's expected schema fields or types

✓ Fix

Update the mock response to exactly match the expected JSON schema, including field names, types, and nesting.

2

Mock response includes extra text or markdown fences around the JSON

✓ Fix

Remove any markdown fences or extraneous text from the mock response so it contains only raw JSON.

3

Mock response uses single quotes or invalid JSON formatting

✓ Fix

Ensure the mock response uses valid JSON with double quotes and proper syntax.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.schema.output_parser import JsonOutputParser

mock_response = "'{\'result\': \'success\'}'"  # Invalid JSON with single quotes
parser = JsonOutputParser()
parsed = parser.parse(mock_response)  # Raises OutputParserException here
Fixed - works correctly
python
import os
from langchain.schema.output_parser import JsonOutputParser

mock_response = '{"result": "success"}'  # Fixed valid JSON string
parser = JsonOutputParser()
parsed = parser.parse(mock_response)  # Works correctly
print(parsed)

# Used os.environ for API keys if needed elsewhere
Fixed the mock response string to valid JSON format with double quotes, so JsonOutputParser can parse it without errors.

Workaround

Wrap the parser call in try/except OutputParserException, then manually strip markdown fences or parse JSON with json.loads() as a fallback for malformed mock responses.

Prevention

Use strict schema validation on mock responses and generate mocks programmatically from the same schema definitions used in production to ensure format consistency.

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.