langchain-ai/langchain · error · OutputParserException
Function arguments: are not valid JSON. Received…
Error message
Function {raw_tool_call['function']['name']} arguments:
{arguments}
are not valid JSON. Received JSONDecodeError {e} What it means
Raised by parse_tool_call in openai_tools.py when the raw tool call's 'arguments' string fails json.loads (non-partial mode). The message includes the function name, the raw arguments text, and the underlying JSONDecodeError so you can see exactly what the model emitted that was invalid JSON.
Solutions
- Raise max_tokens so tool call arguments are not cut off
- Use strict=False in the parser (or parse_partial_json-based streaming) to tolerate non-JSON-compliant strings
- Catch OutputParserException, log the raw arguments, and retry the model call or repair the JSON
Example fix
# before tool_calls = parse_tool_call(raw_tool_call, strict=True) # after tool_calls = parse_tool_call(raw_tool_call, strict=False) # tolerate control chars/newlines
Defensive patterns
Strategy: try-catch
Validate before calling
import json
args = raw_tool_call["function"]["arguments"]
if args:
try:
json.loads(args)
except json.JSONDecodeError as e:
... # repair or drop before calling parse_tool_call Try / catch
from langchain_core.exceptions import OutputParserException
try:
tc = parse_tool_call(raw_tool_call, strict=False)
except OutputParserException as e:
invalid = make_invalid_tool_call(raw_tool_call, str(e)) # keep going with an InvalidToolCall Prevention
- Use strict=False for lenient JSON parsing
- Budget enough max_tokens for full tool arguments
When it happens
Trigger: Model returns tool_call arguments that are malformed JSON (truncated by max_tokens, unquoted keys, Python-repr dicts); arguments contain raw newlines/control characters while strict=True. Note: None or empty-string arguments are treated as {} and do NOT raise; the partial=True path swallows errors instead.
Common situations: Tight max_tokens truncating tool arguments mid-JSON; smaller/local models emitting invalid JSON; strict json.loads rejecting otherwise-usable output with control characters.
Related errors
- {exceptions joined with '\n\n'}
- Could not parse function call data
- This output parser can only be used with a chat generation.
- Tool arguments must be specified as a dict, received
- Arguments 'observation' & 'llm_output' are required if…
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/fa51a58c5b81199c.
Report an issue: GitHub.
Appendix: source
Thrown at libs/core/langchain_core/output_parsers/openai_tools.py:69
if partial:
try:
function_args = parse_partial_json(arguments, strict=strict)
except (JSONDecodeError, TypeError): # None args raise TypeError
return None
# Handle None or empty string arguments for parameter-less tools
elif not arguments:
function_args = {}
else:
try:
function_args = json.loads(arguments, strict=strict)
except JSONDecodeError as e:
msg = (
f"Function {raw_tool_call['function']['name']} arguments:\n\n"
f"{arguments}\n\nare not valid JSON. "
f"Received JSONDecodeError {e}"
)
raise OutputParserException(msg) from e
parsed = {
"name": raw_tool_call["function"]["name"] or "",
"args": function_args or {},
}
if return_id:
parsed["id"] = raw_tool_call.get("id")
parsed = create_tool_call(**parsed) # type: ignore[assignment,arg-type]
return parsed
def make_invalid_tool_call(
raw_tool_call: dict[str, Any],
error_msg: str | None,
) -> InvalidToolCall:
"""Create an `InvalidToolCall` from a raw tool call.
Args:
raw_tool_call: The raw tool call.View on GitHub (pinned to e32fa9a52e)