{"record":{"id":"19b937998ec3b40a","repo":"huggingface/smolagents","slug":"error-while-parsing-tool-call-from-model-output","errorCode":null,"errorMessage":"Error while parsing tool call from model output: {e}","messagePattern":"Error while parsing tool call from model output: (.+?)","errorType":"exception","errorClass":"AgentParsingError","httpStatus":null,"severity":"error","filePath":"src/smolagents/agents.py","lineNumber":1331,"sourceCode":"                )\n                self.logger.log_markdown(\n                    content=str(chat_message.content or chat_message.raw or \"\"),\n                    title=\"Output message of the LLM:\",\n                    level=LogLevel.DEBUG,\n                )\n\n            # Record model output\n            memory_step.model_output_message = chat_message\n            memory_step.model_output = chat_message.content\n            memory_step.token_usage = chat_message.token_usage\n        except Exception as e:\n            raise AgentGenerationError(f\"Error while generating output:\\n{e}\", self.logger) from e\n\n        if chat_message.tool_calls is None or len(chat_message.tool_calls) == 0:\n            try:\n                chat_message = self.model.parse_tool_calls(chat_message)\n            except Exception as e:\n                raise AgentParsingError(f\"Error while parsing tool call from model output: {e}\", self.logger)\n        else:\n            for tool_call in chat_message.tool_calls:\n                tool_call.function.arguments = parse_json_if_needed(tool_call.function.arguments)\n        final_answer, got_final_answer = None, False\n        for output in self.process_tool_calls(chat_message, memory_step):\n            yield output\n            if isinstance(output, ToolOutput):\n                if output.is_final_answer:\n                    if len(chat_message.tool_calls) > 1:\n                        raise AgentExecutionError(\n                            \"If you want to return an answer, please do not perform any other tool calls than the final answer tool call!\",\n                            self.logger,\n                        )\n                    if got_final_answer:\n                        raise AgentToolExecutionError(\n                            \"You returned multiple final answers. Please return only one single final answer!\",\n                            self.logger,\n                        )","sourceCodeStart":1313,"sourceCodeEnd":1349,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/agents.py#L1313-L1349","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"from smolagents.exceptions import AgentParsingError\n\ntry:\n    result = agent.run(task)\nexcept AgentParsingError:\n    result = agent.run(task)  # resample","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"from smolagents.exceptions import AgentParsingError\n\nfor attempt in range(3):\n    try:\n        result = agent.run(task)\n        break\n    except AgentParsingError:\n        if attempt == 2:\n            raise","preventionTips":["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."],"tags":["smolagents","tool-calling","output-parsing","json"],"backgroundTag":"llm-malformed-tool-call","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}