High severity intermediate · Fix: 5-15 min

OutputParserException

langchain_core.exceptions.OutputParserException

What this error means
LangChain's agent output parser received an LLM response that doesn't match the expected AgentFinish format or action/observation loop structure.

Stack trace

traceback
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `Agent returned invalid output format. Expected action with tool name and input, got: {raw_response}`

File "/usr/local/lib/python3.11/site-packages/langchain_core/agents/agent.py", line 234, in _parse_output
    raise OutputParserException(
File "/usr/local/lib/python3.11/site-packages/langchain_core/agents/react/output_parser.py", line 45, in parse
    raise OutputParserException(f"Could not parse LLM output: `{text}`")
QUICK FIX
Replace manual agent prompts with create_react_agent(llm, tools, prompt=hub.pull('hwchase17/react')) which has battle-tested formatting instructions, or switch to gpt-4o-mini which follows agent format reliably.

Why it happens

LangChain agents (especially ReAct agents) expect the LLM to respond in a strict format: either an immediate final Answer or an Action/Action Input pair that calls a tool. When the LLM returns unstructured text, markdown fences, preamble, or a malformed action format, the ReActOutputParser cannot map it to an AgentFinish or AgentAction object. This is common with non-instruction-tuned models, vague prompts, or when the LLM's internal reasoning leaks into the expected output structure.

Detection

Log the raw LLM output before parsing by wrapping the agent execution in try/except OutputParserException, then inspect the caught exception's message. Add print(intermediate_steps) in the agent loop to see which step produced malformed output.

Causes & fixes

1

LLM returned unstructured text or reasoning instead of 'Action: tool_name' format

✓ Fix

Add to your prompt: 'When using a tool, ALWAYS respond with exactly: Action: [tool_name]\nAction Input: [input]' and use gpt-4o-mini or claude-3-5-haiku-20241022 which follow format instructions reliably.

2

LLM wrapped the action in markdown code fences (```action: tool_name```)

✓ Fix

Use create_react_agent with a chat model that supports structured output, or add 'Return raw text only: no markdown, no backticks, no code fences' to the prompt.

3

Tool name in the action doesn't exactly match any registered tool

✓ Fix

Ensure the LLM knows the exact tool names. Pass tools to create_react_agent, then inject their names into the prompt explicitly with tools_str = '\n'.join([f'{tool.name}: {tool.description}' for tool in tools]).

4

Agent is returning 'Final Answer: ...' instead of 'Action: ...' but parser expects AgentFinish format

✓ Fix

Verify the prompt instructs the agent when to use 'Final Answer' vs 'Action'. Use the default prompt from create_react_agent which handles this automatically.

Code: broken vs fixed

Broken - triggers the error
python
from langchain_core.agents import AgentExecutor
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
import os

@tool
def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

tools = [multiply]
llm = ChatOpenAI(model="gpt-4o", api_key=os.environ.get("OPENAI_API_KEY"))

# BROKEN: Custom prompt with vague format instructions
custom_prompt = """You are a helpful assistant. Use tools when needed.
If you use a tool, explain what you're doing and then respond naturally."""

from langchain_core.agents import create_react_agent
agent = create_react_agent(llm, tools, prompt=custom_prompt)  # This will parse incorrectly
executor = AgentExecutor.from_agent_and_tools(agent, tools, verbose=True)

try:
    result = executor.invoke({"input": "multiply 5 by 3"})
except Exception as e:
    print(f"ERROR: {e}")  # OutputParserException: Could not parse LLM output
Fixed - works correctly
python
from langchain_core.agents import AgentExecutor, create_react_agent
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain import hub
import os

@tool
def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

tools = [multiply]
llm = ChatOpenAI(model="gpt-4o-mini", api_key=os.environ.get("OPENAI_API_KEY"))

# FIXED: Use the battle-tested ReAct prompt from hub instead of custom format
prompt = hub.pull("hwchase17/react")

agent = create_react_agent(llm, tools, prompt=prompt)
executor = AgentExecutor.from_agent_and_tools(agent, tools, verbose=True)

try:
    result = executor.invoke({"input": "multiply 5 by 3"})
    print(f"SUCCESS: {result['output']}")
except Exception as e:
    print(f"ERROR: {e}")
Replaced the vague custom prompt with hub.pull('hwchase17/react'), the official ReAct prompt with proven format instructions that the LLM follows correctly, and switched to gpt-4o-mini which is instruction-tuned for agent tasks.

Workaround

Wrap the agent executor call in a retry loop: catch OutputParserException, then retry up to 3 times with an explicit format instruction injected into the next call. Extract the LLM's raw response from the exception, manually parse it for 'Action:' keywords using regex, and construct an AgentAction manually if parsing fails.

Prevention

Build agents using LangGraph StateGraph for complex workflows: it provides explicit state transitions and error handling instead of relying on fragile output parsing. For simple agents, always use the official prompt from hub.pull() and gpt-4o-mini or claude-3-5-haiku-20241022. Consider OpenAI's function_calling or Anthropic's tool_use which return structured output directly, bypassing parsing fragility.

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

Community Notes

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