OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
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}`") 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
Mock response JSON structure does not match the parser's expected schema fields or types
Update the mock response to exactly match the expected JSON schema, including field names, types, and nesting.
Mock response includes extra text or markdown fences around the JSON
Remove any markdown fences or extraneous text from the mock response so it contains only raw JSON.
Mock response uses single quotes or invalid JSON formatting
Ensure the mock response uses valid JSON with double quotes and proper syntax.
Code: broken vs fixed
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 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 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.