{"record":{"id":"3df09408e258cf92","repo":"huggingface/smolagents","slug":"code-execution-failed-at-line-ast-get-source-seg","errorCode":null,"errorMessage":"Code execution failed at line '{ast.get_source_segment(code, node)}' due to: {type(e).__name__}: {e}","messagePattern":"Code execution failed at line '(.+?)' due to: (.+?): (.+?)","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":1659,"sourceCode":"        try:\n            for node in expression.body:\n                result = evaluate_ast(node, state, static_tools, custom_tools, authorized_imports)\n            state[\"_print_outputs\"].value = truncate_content(\n                str(state[\"_print_outputs\"]), max_length=max_print_outputs_length\n            )\n            is_final_answer = False\n            return result, is_final_answer\n        except FinalAnswerException as e:\n            state[\"_print_outputs\"].value = truncate_content(\n                str(state[\"_print_outputs\"]), max_length=max_print_outputs_length\n            )\n            is_final_answer = True\n            return e.value, is_final_answer\n        except Exception as e:\n            state[\"_print_outputs\"].value = truncate_content(\n                str(state[\"_print_outputs\"]), max_length=max_print_outputs_length\n            )\n            raise InterpreterError(\n                f\"Code execution failed at line '{ast.get_source_segment(code, node)}' due to: {type(e).__name__}: {e}\"\n            )\n\n    # Apply timeout if specified\n    if timeout_seconds is not None:\n        _execute_code = timeout(timeout_seconds)(_execute_code)\n\n    return _execute_code()\n\n\n@dataclass\nclass CodeOutput:\n    output: Any\n    logs: str\n    is_final_answer: bool\n\n\nclass PythonExecutor(ABC):","sourceCodeStart":1641,"sourceCodeEnd":1677,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L1641-L1677","documentation":"This is the executor's generic runtime-failure wrapper: any exception raised while executing a node that is not FinalAnswerException is re-raised as InterpreterError with the source segment of the failing line and the original exception type and message. The original exception is not chained by `from e`, so the message text is the primary diagnostic. It marks where in the agent's code action the failure occurred.","triggerScenarios":"Any runtime error inside executed code: NameError for undefined variables, TypeError on bad tool arguments, ZeroDivisionError, KeyError on subscripting, errors raised by called tools, or nested InterpreterErrors from unsupported nodes.","commonSituations":"LLM hallucinating variable or tool names; wrong argument types passed to tools; iterating over None returned by a previous step; errors inside imported authorized modules.","solutions":["Read the inner `TypeName: message` to identify the real cause and fix the offending line quoted in the error","For agent workflows, pass this error message back to the LLM for self-correction (step_number/output trimming is standard)","Add defensive checks (None guards, isinstance checks) around tool outputs used in subsequent code","If the inner error is itself InterpreterError about unsupported syntax, address that root cause instead"],"exampleFix":"# before\ncode = \"result = alphabet_split('hello')[0]\"  # tool returns str, not list\n\n# after\ncode = \"res = alphabet_split('hello')\\nresult = res[0] if isinstance(res, list) else res\"","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"from smolagents.local_python_executor import InterpreterError\ntry:\n    output, is_final = evaluate_python_code(code, state=state)\nexcept InterpreterError as e:\n    # parse 'due to: <Type>: <msg>' from the message\n    inner = str(e).split('due to: ', 1)[-1]\n    if inner.startswith(('NameError', 'TypeError')):\n        code = fix_with_llm(code, str(e))  # self-correction loop","preventionTips":["Include the error message in the next LLM prompt for self-correction","Guard tool outputs (None checks, isinstance) before use in generated code","Verify tool names/arguments exist before running the full action"],"tags":["smolagents","runtime-error","code-execution","llm-output"],"backgroundTag":"runtime-exception-in-generated-code","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}