huggingface/smolagents · error · AgentParsingError
Error while parsing tool call from model output: {e}
Error message
Error while parsing tool call from model output: {e} What it means
When the model returns a message without native tool_calls, ToolCallingAgent._step_stream calls model.parse_tool_calls() to convert the text into tool calls; any exception there is wrapped in AgentParsingError. This usually means the model produced output that doesn't conform to the expected tool-call format (bad JSON arguments, malformed function-call syntax).
Source
Thrown at src/smolagents/agents.py:1331
)
self.logger.log_markdown(
content=str(chat_message.content or chat_message.raw or ""),
title="Output message of the LLM:",
level=LogLevel.DEBUG,
)
# Record model output
memory_step.model_output_message = chat_message
memory_step.model_output = chat_message.content
memory_step.token_usage = chat_message.token_usage
except Exception as e:
raise AgentGenerationError(f"Error while generating output:\n{e}", self.logger) from e
if chat_message.tool_calls is None or len(chat_message.tool_calls) == 0:
try:
chat_message = self.model.parse_tool_calls(chat_message)
except Exception as e:
raise AgentParsingError(f"Error while parsing tool call from model output: {e}", self.logger)
else:
for tool_call in chat_message.tool_calls:
tool_call.function.arguments = parse_json_if_needed(tool_call.function.arguments)
final_answer, got_final_answer = None, False
for output in self.process_tool_calls(chat_message, memory_step):
yield output
if isinstance(output, ToolOutput):
if output.is_final_answer:
if len(chat_message.tool_calls) > 1:
raise AgentExecutionError(
"If you want to return an answer, please do not perform any other tool calls than the final answer tool call!",
self.logger,
)
if got_final_answer:
raise AgentToolExecutionError(
"You returned multiple final answers. Please return only one single final answer!",
self.logger,
)View on GitHub (pinned to 30bb116109)
Solutions
- Switch to a stronger or tool-calling-native model (the model returns proper tool_calls, bypassing parse_tool_calls).
- Retry the run — a resampled completion often parses cleanly; add retry-on-AgentParsingError logic.
- Upgrade smolagents to pick up parser fixes for common malformed-JSON patterns.
Example fix
from smolagents.exceptions import AgentParsingError
try:
result = agent.run(task)
except AgentParsingError:
result = agent.run(task) # resample Defensive patterns
Strategy: retry
Try / catch
from smolagents.exceptions import AgentParsingError
for attempt in range(3):
try:
result = agent.run(task)
break
except AgentParsingError:
if attempt == 2:
raise Prevention
- Prefer models with native tool calling to avoid text-based tool-call parsing.
- Keep tool argument schemas simple (flat JSON with primitive types).
- Retain parsing errors in memory so the model self-corrects on retry.
When it happens
Trigger: agent.run()/step() where the provider returns tool calls as text that fails parsing — e.g. JSON arguments with single quotes/trailing commas, or a weaker model that free-forms the tool-call schema.
Common situations: Using small/local models that emit slightly malformed JSON; provider API changes altering the tool-call payload shape; prompt templates that don't constrain output format.
Related errors
- JSON is invalid: you probably tried to provide multiple tool
- If you want to return an answer, please do not perform any o
- You returned multiple final answers. Please return only one
- Unknown tool {tool_name}, should be one of: {', '.join(avail
- Argument {key} has type '{actual_type}' but should be '{tool
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/19b937998ec3b40a.
Report an issue: GitHub.