OutputParserException
langchain.schema.output_parser.OutputParserException
Stack trace
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `{"url": "https://example.com", "content": "<html>...</html>"}`
File "/app/browser_use_action.py", line 45, in parse_action
result = parser.parse(llm_response) # <-- triggers OutputParserException
File "/usr/local/lib/python3.9/site-packages/langchain/schema/output_parser.py", line 112, in parse
raise OutputParserException(f"Could not parse LLM output: `{text}`") Why it happens
Browser Use actions rely on the LLM to return output in a strict JSON or structured format. If the LLM returns extra text, markdown fences, or deviates from the expected schema, the output parser raises OutputParserException. This often happens when the prompt does not enforce strict formatting or when the model is not instruction-tuned.
Detection
Catch OutputParserException around the parsing call and log the raw LLM output to detect unexpected formatting before the error crashes the app.
Causes & fixes
LLM output includes markdown fences or extra explanatory text around the JSON response
Update the prompt to instruct the LLM to return only raw JSON without markdown fences or extra text, or use a parser like JsonOutputParser that automatically strips fences.
The LLM returns fields or keys that do not match the expected schema exactly
Ensure the prompt and the Pydantic or JSON schema used for parsing have matching field names and types, including case sensitivity.
Using a base LLM model that ignores output format instructions
Switch to an instruction-tuned model such as 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 OutputParserException
llm_response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
# This line triggers OutputParserException if output format is wrong
result = parser.parse(llm_response.choices[0].message.content)
print(result) import os
from langchain.schema.output_parser import OutputParserException
from langchain.output_parsers import JsonOutputParser
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
llm_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
parser = JsonOutputParser() # Changed to JsonOutputParser to handle markdown fences
try:
result = parser.parse(llm_response.choices[0].message.content)
print(result) # Successfully parsed output
except OutputParserException as e:
print(f"Parsing failed: {e}") # Log and handle parse failure Workaround
Wrap the parse call in try/except OutputParserException, then extract JSON manually from the raw LLM output 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 responses at the API level, avoiding fragile post-processing parsing.