{"record":{"id":"decc5185a0fd080d","repo":"huggingface/smolagents","slug":"import-from-expression-module-is-not-allowed-au","errorCode":null,"errorMessage":"Import from {expression.module} is not allowed. Authorized imports are: {str(authorized_imports)}","messagePattern":"Import from (.+?) is not allowed\\. Authorized imports are: (.+?)","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":1339,"sourceCode":"        if check_import_authorized(expression.module, authorized_imports):\n            raw_module = __import__(expression.module, fromlist=[alias.name for alias in expression.names])\n            module = get_safe_module(raw_module, authorized_imports)\n            if expression.names[0].name == \"*\":  # Handle \"from module import *\"\n                if hasattr(module, \"__all__\"):  # If module has __all__, import only those names\n                    for name in module.__all__:\n                        state[name] = getattr(module, name)\n                else:  # If no __all__, import all public names (those not starting with '_')\n                    for name in dir(module):\n                        if not name.startswith(\"_\"):\n                            state[name] = getattr(module, name)\n            else:  # regular from imports\n                for alias in expression.names:\n                    if hasattr(module, alias.name):\n                        state[alias.asname or alias.name] = getattr(module, alias.name)\n                    else:\n                        raise InterpreterError(f\"Module {expression.module} has no attribute {alias.name}\")\n        else:\n            raise InterpreterError(\n                f\"Import from {expression.module} is not allowed. Authorized imports are: {str(authorized_imports)}\"\n            )\n        return None\n\n\ndef evaluate_generatorexp(\n    genexp: ast.GeneratorExp,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> Generator[Any]:\n    def generator():\n        for gen in genexp.generators:\n            iter_value = evaluate_ast(gen.iter, state, static_tools, custom_tools, authorized_imports)\n            for value in iter_value:\n                new_state = state.copy()\n                set_value(","sourceCodeStart":1321,"sourceCodeEnd":1357,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L1321-L1357","documentation":"A `from module import ...` statement targeted a module not in authorized_imports. Same whitelist mechanism as plain import, applied to from-imports (including relative-looking ones inside packages).","triggerScenarios":"from os import path when 'os' is not authorized; from urllib.request import urlopen with only default imports allowed.","commonSituations":"Same as error 93: default safe-list too narrow for what agent code needs; stricter configs in production; prompt-injected code trying to import network/os modules.","solutions":["Add the top-level module to authorized_imports (e.g. ['urllib.request'] or its root) when constructing the CodeAgent","Expose the needed functionality as a tool instead of allowing the import","Use smolagents approved-import configuration helpers to allow coherent module groups"],"exampleFix":"# before\nagent = CodeAgent(tools=[], model=model)\n# code: from urllib.request import urlopen -> blocked\n# after\nagent = CodeAgent(tools=[], model=model, authorized_imports=['urllib.request'])","handlingStrategy":"validation","validationCode":"root = from_module.split('.')[0]\nassert root in AUTHORIZED, f'{root!r} not authorized for from-imports'","typeGuard":null,"tryCatchPattern":"try:\n    evaluate_python(code, authorized_imports=AUTHORIZED)\nexcept InterpreterError as e:\n    if 'Import from' in str(e):\n        # extend authorized_imports with the root module or swap to a tool call","preventionTips":["Authorize the root module when allowing submodules","Audit from-imports in generated code the same as plain imports"],"tags":["python-executor","import","sandbox","security","smolagents"],"backgroundTag":"module-import-not-authorized","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}