huggingface/smolagents · error · InterpreterError

Import from {expression.module} is not allowed. Authorized i

Error message

Import from {expression.module} is not allowed. Authorized imports are: {str(authorized_imports)}

What it means

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).

Source

Thrown at src/smolagents/local_python_executor.py:1339

        if check_import_authorized(expression.module, authorized_imports):
            raw_module = __import__(expression.module, fromlist=[alias.name for alias in expression.names])
            module = get_safe_module(raw_module, authorized_imports)
            if expression.names[0].name == "*":  # Handle "from module import *"
                if hasattr(module, "__all__"):  # If module has __all__, import only those names
                    for name in module.__all__:
                        state[name] = getattr(module, name)
                else:  # If no __all__, import all public names (those not starting with '_')
                    for name in dir(module):
                        if not name.startswith("_"):
                            state[name] = getattr(module, name)
            else:  # regular from imports
                for alias in expression.names:
                    if hasattr(module, alias.name):
                        state[alias.asname or alias.name] = getattr(module, alias.name)
                    else:
                        raise InterpreterError(f"Module {expression.module} has no attribute {alias.name}")
        else:
            raise InterpreterError(
                f"Import from {expression.module} is not allowed. Authorized imports are: {str(authorized_imports)}"
            )
        return None


def evaluate_generatorexp(
    genexp: ast.GeneratorExp,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
) -> Generator[Any]:
    def generator():
        for gen in genexp.generators:
            iter_value = evaluate_ast(gen.iter, state, static_tools, custom_tools, authorized_imports)
            for value in iter_value:
                new_state = state.copy()
                set_value(

View on GitHub (pinned to 30bb116109)

Solutions

  1. Add the top-level module to authorized_imports (e.g. ['urllib.request'] or its root) when constructing the CodeAgent
  2. Expose the needed functionality as a tool instead of allowing the import
  3. Use smolagents approved-import configuration helpers to allow coherent module groups

Example fix

# before
agent = CodeAgent(tools=[], model=model)
# code: from urllib.request import urlopen -> blocked
# after
agent = CodeAgent(tools=[], model=model, authorized_imports=['urllib.request'])
Defensive patterns

Strategy: validation

Validate before calling

root = from_module.split('.')[0]
assert root in AUTHORIZED, f'{root!r} not authorized for from-imports'

Try / catch

try:
    evaluate_python(code, authorized_imports=AUTHORIZED)
except InterpreterError as e:
    if 'Import from' in str(e):
        # extend authorized_imports with the root module or swap to a tool call

Prevention

When it happens

Trigger: from os import path when 'os' is not authorized; from urllib.request import urlopen with only default imports allowed.

Common situations: 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.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/decc5185a0fd080d. Report an issue: GitHub.