{"record":{"id":"3595bac98adf6e1b","repo":"huggingface/smolagents","slug":"error-in-code-parsing-e-make-sure-to-provide-co","errorCode":null,"errorMessage":"Error in code parsing:\n{e}\nMake sure to provide correct code blobs.","messagePattern":"Error in code parsing:\n(.+?)\nMake sure to provide correct code blobs\\.","errorType":"exception","errorClass":"AgentParsingError","httpStatus":null,"severity":"error","filePath":"src/smolagents/agents.py","lineNumber":1713,"sourceCode":"                    memory_step.model_output_message.content = output_text\n\n            memory_step.token_usage = chat_message.token_usage\n            memory_step.model_output = output_text\n        except Exception as e:\n            raise AgentGenerationError(f\"Error in generating model output:\\n{e}\", self.logger) from e\n\n        ### Parse output ###\n        try:\n            if self._use_structured_outputs_internally:\n                code_action = json.loads(output_text)[\"code\"]\n                code_action = extract_code_from_text(code_action, self.code_block_tags) or code_action\n            else:\n                code_action = parse_code_blobs(output_text, self.code_block_tags)\n            code_action = fix_final_answer_code(code_action)\n            memory_step.code_action = code_action\n        except Exception as e:\n            error_msg = f\"Error in code parsing:\\n{e}\\nMake sure to provide correct code blobs.\"\n            raise AgentParsingError(error_msg, self.logger)\n\n        tool_call = ToolCall(\n            name=\"python_interpreter\",\n            arguments=code_action,\n            id=f\"call_{len(self.memory.steps)}\",\n        )\n        yield tool_call\n        memory_step.tool_calls = [tool_call]\n\n        ### Execute action ###\n        self.logger.log_code(title=\"Executing parsed code:\", content=code_action, level=LogLevel.INFO)\n        try:\n            code_output = self.python_executor(code_action)\n            execution_outputs_console = []\n            if len(code_output.logs) > 0:\n                execution_outputs_console += [\n                    Text(\"Execution logs:\", style=\"bold\"),\n                    Text(code_output.logs),","sourceCodeStart":1695,"sourceCodeEnd":1731,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/agents.py#L1695-L1731","documentation":"After generation, CodeAgent._step_stream extracts executable Python from the model output — via json.loads for structured outputs or parse_code_blobs for tagged code blocks — and wraps any failure in AgentParsingError with guidance to provide correct code blobs. It means the model's textual output was not in the expected code format.","triggerScenarios":"The model returns prose or malformed code blocks so parse_code_blobs finds no valid code between the configured tags; or with structured outputs, output_text is not valid JSON with a 'code' key.","commonSituations":"Small/local models ignoring the code-format prompt; code_block_tags mismatch between what the prompt tells the model and what the parser looks for; custom prompt templates that don't instruct ```python fencing when tags are 'markdown'.","solutions":["Ensure your prompt templates instruct code in the same tags configured via code_block_tags (use 'markdown' with the default code-agent.yaml templates).","Retry the run — the parsing error is fed back and models usually correct the format.","Use a stronger model or enable structured outputs so the code field is guaranteed."],"exampleFix":"# before\nagent = CodeAgent(model=weak_model, tools=[], code_block_tags='markdown')\n# weak model outputs plain prose -> AgentParsingError\n\n# after\nagent = CodeAgent(model=stronger_model, tools=[], code_block_tags='markdown')\n# or retry loop:\nfrom smolagents.exceptions import AgentParsingError\nfor _ in range(3):\n    try:\n        result = agent.run(task); break\n    except AgentParsingError:\n        continue","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 as e:\n        if 'code parsing' not in str(e):\n            raise\n        if attempt == 2:\n            raise","preventionTips":["Keep code_block_tags consistent with your prompt template's code fencing instructions.","Use 'markdown' tags with the stock code_agent.yaml templates.","Prefer stronger models or structured outputs for reliable code formatting."],"tags":["smolagents","codeagent","code-parsing","output-format"],"backgroundTag":"llm-malformed-code-output","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}