huggingface/smolagents · error · InterpreterError
Module {expression.module} has no attribute {alias.name}
Error message
Module {expression.module} has no attribute {alias.name} What it means
A `from module import name` succeeded in importing the module, but the module has no attribute with that name — the interpreter's equivalent of ImportError: cannot import name.
Source
Thrown at src/smolagents/local_python_executor.py:1337
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):
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:View on GitHub (pinned to 30bb116109)
Solutions
- Check the module's actual attributes (dir(module) outside sandbox) and fix the name
- For hallucinated names, guide the model with better tool/prompt descriptions or few-shot examples
- If the name exists in another module, import from the correct one
Example fix
# before from math import pie # after from math import pi
Defensive patterns
Strategy: validation
Validate before calling
import importlib
mod = importlib.import_module('math')
assert hasattr(mod, name_to_import), f'{name_to_import!r} missing in math' Try / catch
try:
evaluate_python(code, ...)
except InterpreterError as e:
if 'has no attribute' in str(e):
# check dir(module), fix the imported name, retry Prevention
- Verify names exist in the target module before importing
- Guard against model-hallucinated APIs with few-shot examples
When it happens
Trigger: from math import pie (typo), from collections import OrderedDict in a version where it moved, from json import loads_extra.
Common situations: Agent hallucinates a function/class name that doesn't exist in the module; version differences where a name was removed or moved; typo in the imported name.
Related errors
- Import of {alias.name} is not allowed. Authorized imports ar
- Import from {expression.module} is not allowed. Authorized i
- Object {obj} has no attribute {func_name}
- Cannot unpack non-dict value in **kwargs: {type(starred_dict
- super() needs at least one argument
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/39e003fb409e733f.
Report an issue: GitHub.