High severity intermediate · Fix: 5-10 min

OutputParserException

langchain_core.exceptions.OutputParserException

What this error means
LlamaIndex tool calls fail because the LLM response is not valid JSON or is wrapped in unexpected formatting, causing JSON parsing errors.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: ```json\n{"key": "value"}\n```
    at llamaindex.tools.base_tool.BaseTool._parse_response (base_tool.py:123)
    at llamaindex.tools.base_tool.BaseTool._call (base_tool.py:98)
    at llamaindex.tools.base_tool.BaseTool.__call__ (base_tool.py:75)
    at main.py:45
QUICK FIX
Add a JSON output parser that strips markdown fences or update the prompt to return raw JSON without fences.

Why it happens

LlamaIndex expects the LLM to return a strictly formatted JSON string without markdown fences or extra text. When the LLM returns JSON wrapped in markdown code blocks or with additional commentary, the JSON parser fails, raising OutputParserException.

Detection

Catch OutputParserException around tool calls and log the raw LLM output to detect unexpected formatting or markdown fences before parsing.

Causes & fixes

1

LLM response includes markdown fences (e.g., ```json) around the JSON output

✓ Fix

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

2

The prompt does not clearly specify the exact JSON schema expected by LlamaIndex

✓ Fix

Update the prompt to explicitly define the JSON structure and fields expected, ensuring the LLM outputs matching JSON.

3

Using a base LLM model that ignores output format instructions and returns free-form text

✓ 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 llama_index import LLMPredictor, ServiceContext, Tool

llm_predictor = LLMPredictor(model_name="gpt-4o")
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
tool = Tool(service_context=service_context)

response = tool("Get data in JSON format")  # Raises OutputParserException due to markdown fences
print(response)
Fixed - works correctly
python
import os
from llama_index import LLMPredictor, ServiceContext, Tool
from langchain.output_parsers import JsonOutputParser

llm_predictor = LLMPredictor(model_name="gpt-4o-mini")
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
tool = Tool(service_context=service_context, output_parser=JsonOutputParser())  # Added JsonOutputParser to strip fences

response = tool("Return ONLY raw JSON, no markdown fences")  # Prompt updated
print(response)
Added JsonOutputParser to automatically handle and strip markdown fences from LLM output and updated prompt to request raw JSON only.

Workaround

Wrap the tool call in try/except OutputParserException, then extract JSON substring manually using regex and parse it 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 JSON responses at the API level, avoiding parser errors.

Python 3.9+ · langchain-core >=0.1.0 · tested on 0.2.x
Verified 2026-04 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.