{"record":{"id":"f177503093f73ec2","repo":"huggingface/smolagents","slug":"unsupported-statement-in-class-body-stmt-class","errorCode":null,"errorMessage":"Unsupported statement in class body: {stmt.__class__.__name__}","messagePattern":"Unsupported statement in class body: (.+?)","errorType":"error_code","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":614,"sourceCode":"            value = evaluate_ast(stmt.value, state, static_tools, custom_tools, authorized_imports)\n            for target in stmt.targets:\n                if isinstance(target, ast.Name):\n                    class_dict[target.id] = value\n                elif isinstance(target, ast.Attribute):\n                    obj = evaluate_ast(target.value, class_dict, static_tools, custom_tools, authorized_imports)\n                    setattr(obj, target.attr, value)\n        elif isinstance(stmt, ast.Pass):\n            pass\n        elif (\n            isinstance(stmt, ast.Expr)\n            and stmt == class_def.body[0]\n            and isinstance(stmt.value, ast.Constant)\n            and isinstance(stmt.value.value, str)\n        ):\n            # Check if it is a docstring: first statement in class body which is a string literal expression\n            class_dict[\"__doc__\"] = stmt.value.value\n        else:\n            raise InterpreterError(f\"Unsupported statement in class body: {stmt.__class__.__name__}\")\n\n    new_class = metaclass(class_name, tuple(bases), class_dict)\n    state[class_name] = new_class\n    return new_class\n\n\ndef evaluate_annassign(\n    annassign: ast.AnnAssign,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> Any:\n    # If there's a value to assign, evaluate it\n    if annassign.value:\n        value = evaluate_ast(annassign.value, state, static_tools, custom_tools, authorized_imports)\n        # Set the value for the target\n        set_value(annassign.target, value, state, static_tools, custom_tools, authorized_imports)","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L596-L632","documentation":"evaluate_class_def only supports a fixed set of statements inside a class body (assignments, AnnAssign, FunctionDef, AsyncFunctionDef, Pass, Expr docstrings); anything else (if, for, while, try, with, Return, etc.) raises InterpreterError naming the statement class. Python itself forbids many of these in class bodies, but the executor is stricter.","triggerScenarios":"Class body contains an unsupported statement, e.g. `class A:\\n    if x: y = 1`, a for loop, a with block, or a raise, inside the executed snippet.","commonSituations":"LLM generates conditional initialization or loops inside a class; metaprogramming-style code pasted into the agent; code that is valid at module level pasted verbatim into a class body.","solutions":["Move conditional/loop logic out of the class body into __init__ or a classmethod","Replace class-body branching with precomputed values or default arguments","Simplify to plain attribute and method definitions inside the class"],"exampleFix":"# before\ncode = \"class Config:\\n    if debug:\\n        level = 'DEBUG'\"\n\n# after\ncode = \"class Config:\\n    level = 'DEBUG' if debug else 'INFO'\"","handlingStrategy":"validation","validationCode":"import ast\nALLOWED = (ast.FunctionDef, ast.AsyncFunctionDef, ast.Assign, ast.AnnAssign, ast.Pass)\nfor node in ast.walk(ast.parse(code)):\n    if isinstance(node, ast.ClassDef):\n        for stmt in node.body:\n            if not isinstance(stmt, ALLOWED) and not (isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Constant)):\n                raise ValueError(f'statement {type(stmt).__name__} not allowed in class body')","typeGuard":null,"tryCatchPattern":"from smolagents.local_python_executor import InterpreterError\ntry:\n    evaluate_python(code)\nexcept InterpreterError as e:\n    if 'Unsupported statement in class body' in str(e):\n        code = move_logic_to_init(code)","preventionTips":["Put branching/loops inside methods (__init__), never in the class body","Use conditional expressions (x = a if c else b) for conditional attributes","Lint generated code for non-trivial class bodies before execution"],"tags":["smolagents","class-definition","unsupported-statement","interpreter-error","sandbox"],"backgroundTag":"unsupported-language-construct","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}