huggingface/smolagents · error · InterpreterError
Import of {alias.name} is not allowed. Authorized imports ar
Error message
Import of {alias.name} is not allowed. Authorized imports are: {str(authorized_imports)} What it means
An `import X` statement referenced a module not in the executor's authorized_imports whitelist. The sandbox only imports modules explicitly permitted (default: a small safe set; '*' allows all).
Source
Thrown at src/smolagents/local_python_executor.py:1316
)
continue
# Recursively process nested modules, passing visited set
if isinstance(attr_value, ModuleType):
attr_value = get_safe_module(attr_value, authorized_imports, visited=visited)
setattr(safe_module, attr_name, attr_value)
return safe_module
def evaluate_import(expression, state, authorized_imports):
if isinstance(expression, ast.Import):
for alias in expression.names:
if check_import_authorized(alias.name, authorized_imports):
raw_module = import_module(alias.name)
state[alias.asname or alias.name] = get_safe_module(raw_module, authorized_imports)
else:
raise InterpreterError(
f"Import of {alias.name} is not allowed. Authorized imports are: {str(authorized_imports)}"
)
return None
elif isinstance(expression, ast.ImportFrom):
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):View on GitHub (pinned to 30bb116109)
Solutions
- Pass the module in authorized_imports when creating the agent: ToolCallingAgent(..., authorized_imports=['requests', 'pandas']) or use smolagents's helper for standard subsets
- Replace the import with an existing tool (e.g. HTTP fetch via a provided tool instead of requests)
- Use AdditionalResources/approved-imports patterns from smolagents docs to scope what agents may import
Example fix
# before agent = CodeAgent(tools=[], model=model) # default imports only # code: import requests -> blocked # after agent = CodeAgent(tools=[], model=model, authorized_imports=['requests'])
Defensive patterns
Strategy: validation
Validate before calling
from smolagents.utils import get_module_imports_allowed # or construct list explicitly AUTHORIZED = ['math', 'time', 'random', 'requests'] assert all(m in AUTHORIZED for m in required_modules), 'code needs unapproved imports'
Try / catch
try:
evaluate_python(code, static_tools=base_python_tools, authorized_imports=AUTHORIZED)
except InterpreterError as e:
if 'not allowed' in str(e) and 'Import of' in str(e):
# either extend authorized_imports or rewrite code to use tools Prevention
- Set authorized_imports explicitly per task scope
- Review agent-generated imports before execution in high-stakes settings
- Expose capabilities as tools instead of granting broad imports
When it happens
Trigger: import requests inside agent code when authorized_imports=['time','math']; importing subprocess, os, socket, or any third-party package not whitelisted.
Common situations: Default smolagents configuration only allows a few safe modules; agent-generated code tries to pip-install/import pandas, numpy, requests; deploying with stricter authorized_imports than dev environment.
Related errors
- Import from {expression.module} is not allowed. Authorized i
- Invoking a builtin function that has not been explicitly add
- Forbidden call to dunder function: {func.__name__}
- Forbidden access to module: {result.__name__}
- Forbidden access to module: {result['__name__']}
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/e9dcb7b3dfe20a37.
Report an issue: GitHub.