huggingface/smolagents · error · InterpreterError
Cannot add non-list value {value_to_add} to a list.
Error message
Cannot add non-list value {value_to_add} to a list. What it means
For `list += value` inside the sandbox, the executor requires the right-hand side to also be a list; adding anything else raises InterpreterError. This deliberately diverges from CPython (where list += iterable does in-place extend) to keep semantics predictable.
Source
Thrown at src/smolagents/local_python_executor.py:668
key = evaluate_ast(target.slice, state, static_tools, custom_tools, authorized_imports)
return obj[key]
elif isinstance(target, ast.Attribute):
obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)
return getattr(obj, target.attr)
elif isinstance(target, ast.Tuple):
return tuple(get_current_value(elt) for elt in target.elts)
elif isinstance(target, ast.List):
return [get_current_value(elt) for elt in target.elts]
else:
raise InterpreterError("AugAssign not supported for {type(target)} targets.")
current_value = get_current_value(expression.target)
value_to_add = evaluate_ast(expression.value, state, static_tools, custom_tools, authorized_imports)
if isinstance(expression.op, ast.Add):
if isinstance(current_value, list):
if not isinstance(value_to_add, list):
raise InterpreterError(f"Cannot add non-list value {value_to_add} to a list.")
current_value += value_to_add
else:
current_value += value_to_add
elif isinstance(expression.op, ast.Sub):
current_value -= value_to_add
elif isinstance(expression.op, ast.Mult):
current_value *= value_to_add
elif isinstance(expression.op, ast.Div):
current_value /= value_to_add
elif isinstance(expression.op, ast.Mod):
current_value %= value_to_add
elif isinstance(expression.op, ast.Pow):
current_value **= value_to_add
elif isinstance(expression.op, ast.FloorDiv):
current_value //= value_to_add
elif isinstance(expression.op, ast.BitAnd):
current_value &= value_to_add
elif isinstance(expression.op, ast.BitOr):View on GitHub (pinned to 30bb116109)
Solutions
- Use .append(item) for single elements
- Use .extend(iterable) or list += list(iterable) for tuples/generators/strings-as-list
- Wrap scalars: items += [item]
Example fix
# before code = "items = [1, 2]\nitems += 3" # after code = "items = [1, 2]\nitems.append(3)"
Defensive patterns
Strategy: type-guard
Validate before calling
def safe_list_add(lst, other):
if isinstance(other, list):
return lst + other
if hasattr(other, '__iter__'):
return lst + list(other)
return lst + [other] # append semantics Type guard
def is_list_compatible_extender(v) -> bool:
return isinstance(v, list) Try / catch
from smolagents.local_python_executor import InterpreterError
try:
evaluate_python(code)
except InterpreterError as e:
if 'Cannot add non-list value' in str(e):
code = code.replace('items +=', 'items.extend([') # or rewrite to .append Prevention
- Use .append(x) for single elements, .extend(iter) for collections
- Never rely on CPython's list += str/generator behavior inside the sandbox
- Lint generated code for `+=` on lists with non-list right-hand sides
When it happens
Trigger: Executed code does `my_list += x` (ast.AugAssign with Add) where my_list is a list and x is a non-list: e.g. `items += 'abc'`, `items += 1`, `items += (1, 2)`, `items += generator()`.
Common situations: LLM uses += to append a single element (`lst += item`); string concatenation habits carried to lists; extending with a tuple or generator which CPython would accept.
Related errors
- Object is not iterable
- AugAssign not supported for {type(target)} targets.
- Operation {type(expression.op).__name__} is not supported.
- Cannot unpack non-tuple value
- {e}
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/6b5f60f5e66a9bbe.
Report an issue: GitHub.