{"record":{"id":"98fd5b05545d6df4","repo":"huggingface/smolagents","slug":"cannot-unpack-tuple-of-wrong-size","errorCode":null,"errorMessage":"Cannot unpack tuple of wrong size","messagePattern":"Cannot unpack tuple of wrong size","errorType":"error_code","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":813,"sourceCode":"    target: ast.AST,\n    value: Any,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> None:\n    if isinstance(target, ast.Name):\n        if target.id in static_tools:\n            raise InterpreterError(f\"Cannot assign to name '{target.id}': doing this would erase the existing tool!\")\n        state[target.id] = value\n    elif isinstance(target, ast.Tuple):\n        if not isinstance(value, tuple):\n            if hasattr(value, \"__iter__\") and not isinstance(value, (str, bytes)):\n                value = tuple(value)\n            else:\n                raise InterpreterError(\"Cannot unpack non-tuple value\")\n        if len(target.elts) != len(value):\n            raise InterpreterError(\"Cannot unpack tuple of wrong size\")\n        for i, elem in enumerate(target.elts):\n            set_value(elem, value[i], state, static_tools, custom_tools, authorized_imports)\n    elif isinstance(target, ast.Subscript):\n        obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)\n        key = evaluate_ast(target.slice, state, static_tools, custom_tools, authorized_imports)\n        obj[key] = value\n    elif isinstance(target, ast.Attribute):\n        obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)\n        setattr(obj, target.attr, value)\n\n\ndef evaluate_call(\n    call: ast.Call,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> Any:","sourceCodeStart":795,"sourceCodeEnd":831,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L795-L831","documentation":"After confirming the value is tuple-like, set_value checks len(target.elts) == len(value); a mismatch raises InterpreterError('Cannot unpack tuple of wrong size'). This is the sandbox's equivalent of ValueError: not enough/too many values to unpack.","triggerScenarios":"`a, b = (1, 2, 3)`, `a, b, c = (1, 2)`, or unpacking a tool result whose length differs from the target names; also applies to for-loop targets (for a, b in pairs) when some element has the wrong length.","commonSituations":"API/tool returns a variable-length list and the model hardcodes two names; JSON payload shape changed between runs; iterating rows where some rows have extra fields.","solutions":["Match the number of targets to the data or use star-capture: `a, *rest = value`","Validate length first: `assert len(v) == 2` or `if len(v) == 2: a, b = v else: ...`","Index explicitly when only some fields are needed: a = v[0]"],"exampleFix":"# before\ncode = \"a, b = (1, 2, 3)\"\n\n# after\ncode = \"a, *rest = (1, 2, 3)\"","handlingStrategy":"validation","validationCode":"def unpackable_len(v) -> int | None:\n    if isinstance(v, tuple):\n        return len(v)\n    if hasattr(v, '__iter__') and not isinstance(v, (str, bytes)):\n        try:\n            return len(list(v))\n        except TypeError:\n            return None\n    return None\n# before executing: check expected arity at each unpack site","typeGuard":null,"tryCatchPattern":"from smolagents.local_python_executor import InterpreterError\ntry:\n    evaluate_python(code)\nexcept InterpreterError as e:\n    if 'wrong size' in str(e):\n        code = code.replace('a, b = v', 'a, *rest = v')","preventionTips":["Use star-capture (a, *rest = v) for variable-length data","Validate len() before multi-name unpacking","Index explicitly when only some elements are needed"],"tags":["smolagents","unpacking","arity-mismatch","interpreter-error","sandbox"],"backgroundTag":"unpacking-type-mismatch","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}