{"record":{"id":"796e29ac0754405e","repo":"huggingface/smolagents","slug":"check-check-function-name-failed-with-error","errorCode":null,"errorMessage":"Check {check_function.__name__} failed with error: {e}","messagePattern":"Check (.+?) failed with error: (.+?)","errorType":"exception","errorClass":"AgentError","httpStatus":null,"severity":"error","filePath":"src/smolagents/agents.py","lineNumber":618,"sourceCode":"            finally:\n                self._finalize_step(action_step)\n                self.memory.steps.append(action_step)\n                yield action_step\n                self.step_number += 1\n\n        if not returned_final_answer and self.step_number == max_steps + 1:\n            final_answer = self._handle_max_steps_reached(task)\n            yield action_step\n        final_answer_step = FinalAnswerStep(handle_agent_output_types(final_answer))\n        self._finalize_step(final_answer_step)\n        yield final_answer_step\n\n    def _validate_final_answer(self, final_answer: Any):\n        for check_function in self.final_answer_checks:\n            try:\n                assert check_function(final_answer, self.memory, agent=self)\n            except Exception as e:\n                raise AgentError(f\"Check {check_function.__name__} failed with error: {e}\", self.logger)\n\n    def _finalize_step(self, memory_step: ActionStep | PlanningStep | FinalAnswerStep):\n        if not isinstance(memory_step, FinalAnswerStep):\n            memory_step.timing.end_time = time.time()\n        self.step_callbacks.callback(memory_step, agent=self)\n\n    def _handle_max_steps_reached(self, task: str) -> Any:\n        action_step_start_time = time.time()\n        final_answer = self.provide_final_answer(task)\n        final_memory_step = ActionStep(\n            step_number=self.step_number,\n            error=AgentMaxStepsError(\"Reached max steps.\", self.logger),\n            timing=Timing(start_time=action_step_start_time, end_time=time.time()),\n            token_usage=final_answer.token_usage,\n        )\n        final_memory_step.action_output = final_answer.content\n        self._finalize_step(final_memory_step)\n        self.memory.steps.append(final_memory_step)","sourceCodeStart":600,"sourceCodeEnd":636,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/agents.py#L600-L636","documentation":"final_answer_checks are user-supplied validator functions run against the agent's final answer before it's accepted. If a check raises or its assert fails, _validate_final_answer wraps the failure as AgentError('Check <name> failed with error: ...') and the run aborts.","triggerScenarios":"Passing final_answer_checks=[my_check] to an agent where my_check raises (KeyError on the answer structure, assertion failure, TypeError on unexpected types) when the agent produces its final answer during run().","commonSituations":"Checks that expect a dict/JSON answer but the LLM returns plain text; checks calling external APIs (URL validation) that fail; brittle assertions on formatting that the model doesn't reliably satisfy.","solutions":["Make the check defensive: validate the answer's type/shape first and raise a clear message","If the answer format is unreliable, tighten the prompt or an answer schema so checks receive the expected type","Re-run the task; if the check failure is transient (network inside the check), add retry inside the check itself"],"exampleFix":"# before\ndef check_json(answer, memory, agent=None):\n    assert \"result\" in answer  # fails with TypeError/KeyError if answer is a plain string\n\n# after\ndef check_json(answer, memory, agent=None):\n    if not isinstance(answer, dict):\n        raise ValueError(\"final answer must be a dict, got: \" + repr(answer)[:200])\n    assert \"result\" in answer","handlingStrategy":"try-catch","validationCode":"def robust_check(answer, memory, agent=None):\n    if not isinstance(answer, (str, dict)):\n        return False  # return False instead of raising so check failure is explicit\n    ...","typeGuard":null,"tryCatchPattern":"from smolagents import AgentError\ntry:\n    agent.run(task)\nexcept AgentError as e:\n    if str(e).startswith(\"Check \"):\n        logger.warning(\"final answer failed validation: %s\", e)\n        # optionally re-run with the failure appended to the task","preventionTips":["Make checks total functions: handle every expected answer type without raising","Don't do network I/O inside final_answer_checks without try/except and timeout"],"tags":["smolagents","final-answer-checks","validation","agenterror"],"backgroundTag":"output-validation-failed","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}