{"record":{"id":"6b5f60f5e66a9bbe","repo":"huggingface/smolagents","slug":"cannot-add-non-list-value-value-to-add-to-a-list","errorCode":null,"errorMessage":"Cannot add non-list value {value_to_add} to a list.","messagePattern":"Cannot add non-list value (.+?) to a list\\.","errorType":"error_code","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":668,"sourceCode":"            key = evaluate_ast(target.slice, state, static_tools, custom_tools, authorized_imports)\n            return obj[key]\n        elif isinstance(target, ast.Attribute):\n            obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)\n            return getattr(obj, target.attr)\n        elif isinstance(target, ast.Tuple):\n            return tuple(get_current_value(elt) for elt in target.elts)\n        elif isinstance(target, ast.List):\n            return [get_current_value(elt) for elt in target.elts]\n        else:\n            raise InterpreterError(\"AugAssign not supported for {type(target)} targets.\")\n\n    current_value = get_current_value(expression.target)\n    value_to_add = evaluate_ast(expression.value, state, static_tools, custom_tools, authorized_imports)\n\n    if isinstance(expression.op, ast.Add):\n        if isinstance(current_value, list):\n            if not isinstance(value_to_add, list):\n                raise InterpreterError(f\"Cannot add non-list value {value_to_add} to a list.\")\n            current_value += value_to_add\n        else:\n            current_value += value_to_add\n    elif isinstance(expression.op, ast.Sub):\n        current_value -= value_to_add\n    elif isinstance(expression.op, ast.Mult):\n        current_value *= value_to_add\n    elif isinstance(expression.op, ast.Div):\n        current_value /= value_to_add\n    elif isinstance(expression.op, ast.Mod):\n        current_value %= value_to_add\n    elif isinstance(expression.op, ast.Pow):\n        current_value **= value_to_add\n    elif isinstance(expression.op, ast.FloorDiv):\n        current_value //= value_to_add\n    elif isinstance(expression.op, ast.BitAnd):\n        current_value &= value_to_add\n    elif isinstance(expression.op, ast.BitOr):","sourceCodeStart":650,"sourceCodeEnd":686,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L650-L686","documentation":"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.","triggerScenarios":"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()`.","commonSituations":"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.","solutions":["Use .append(item) for single elements","Use .extend(iterable) or list += list(iterable) for tuples/generators/strings-as-list","Wrap scalars: items += [item]"],"exampleFix":"# before\ncode = \"items = [1, 2]\\nitems += 3\"\n\n# after\ncode = \"items = [1, 2]\\nitems.append(3)\"","handlingStrategy":"type-guard","validationCode":"def safe_list_add(lst, other):\n    if isinstance(other, list):\n        return lst + other\n    if hasattr(other, '__iter__'):\n        return lst + list(other)\n    return lst + [other]  # append semantics","typeGuard":"def is_list_compatible_extender(v) -> bool:\n    return isinstance(v, list)","tryCatchPattern":"from smolagents.local_python_executor import InterpreterError\ntry:\n    evaluate_python(code)\nexcept InterpreterError as e:\n    if 'Cannot add non-list value' in str(e):\n        code = code.replace('items +=', 'items.extend([')  # or rewrite to .append","preventionTips":["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"],"tags":["smolagents","augmented-assignment","list","type-error","interpreter-error"],"backgroundTag":"type-mismatch-runtime","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}