{"record":{"id":"d9d4a695ed0bbd8f","repo":"numpy/numpy","slug":"block-format-index-parent-index-is-a-tuple-on","errorCode":null,"errorMessage":"{_block_format_index(parent_index)} is a tuple. Only lists can be used to arrange blocks, and np.block does not allow implicit conversion from tuple to ndarray.","messagePattern":"(.+?) is a tuple\\. Only lists can be used to arrange blocks, and np\\.block does not allow implicit conversion from tuple to ndarray\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"numpy/_core/shape_base.py","lineNumber":592,"sourceCode":"    -------\n    first_index : list of int\n        The full index of an element from the bottom of the nesting in\n        `arrays`. If any element at the bottom is an empty list, this will\n        refer to it, and the last index along the empty axis will be None.\n    max_arr_ndim : int\n        The maximum of the ndims of the arrays nested in `arrays`.\n    final_size: int\n        The number of elements in the final array. This is used the motivate\n        the choice of algorithm used using benchmarking wisdom.\n\n    \"\"\"\n    if isinstance(arrays, tuple):\n        # not strictly necessary, but saves us from:\n        #  - more than one way to do things - no point treating tuples like\n        #    lists\n        #  - horribly confusing behaviour that results when tuples are\n        #    treated like ndarray\n        raise TypeError(\n            f'{_block_format_index(parent_index)} is a tuple. '\n            'Only lists can be used to arrange blocks, and np.block does '\n            'not allow implicit conversion from tuple to ndarray.'\n        )\n    elif isinstance(arrays, list) and len(arrays) > 0:\n        idxs_ndims = (_block_check_depths_match(arr, parent_index + [i])\n                      for i, arr in enumerate(arrays))\n\n        first_index, max_arr_ndim, final_size = next(idxs_ndims)\n        for index, ndim, size in idxs_ndims:\n            final_size += size\n            if ndim > max_arr_ndim:\n                max_arr_ndim = ndim\n            if len(index) != len(first_index):\n                raise ValueError(\n                    \"List depths are mismatched. First element was at \"\n                    f\"depth {len(first_index)}, but there is an element at \"\n                    f\"depth {len(index)} ({_block_format_index(index)})\"","sourceCodeStart":574,"sourceCodeEnd":610,"githubUrl":"https://github.com/numpy/numpy/blob/e117b3ca4edacf581f440dccfd7f3242f0312afa/numpy/_core/shape_base.py#L574-L610","documentation":"Raised by np.block's depth-checker when it encounters a tuple nested inside the block structure. np.block deliberately only accepts lists for arranging blocks and refuses to implicitly convert a tuple into an ndarray, because treating a tuple as data would be ambiguous.","triggerScenarios":"Calling np.block([...]) where one of the nested elements is a tuple instead of a list or array, e.g. np.block([[a, b], (c, d)]).","commonSituations":"Using parentheses out of habit for grouping instead of brackets; a function returning a tuple that is fed directly into block; converting a list literal to a tuple inadvertently.","solutions":["Replace any tuple grouping with a list: use [...] not (...) for block rows.","Convert tuple inputs to lists: [list(t) for t in rows].","If a tuple is meant to be a single array operand, wrap it with np.array(t) first."],"exampleFix":"// before\nnp.block([[a, b], (c, d)])\n// after\nnp.block([[a, b], [c, d]])","handlingStrategy":"type-guard","validationCode":"def no_tuples(node):\n    if isinstance(node, tuple):\n        raise TypeError('np.block expects lists, not tuples')\n    if isinstance(node, list):\n        for x in node: no_tuples(x)\nno_tuples(arrays)","typeGuard":"def block_is_list_only(node):\n    if isinstance(node, tuple): return False\n    if isinstance(node, list):\n        return all(block_is_list_only(x) for x in node)\n    return True","tryCatchPattern":null,"preventionTips":["Use square brackets exclusively when building np.block layouts.","Convert tuple rows to lists before passing: [list(r) for r in rows].","Wrap any tuple meant as data with np.array()."],"tags":["numpy","block","type-error","tuple"],"analyzedSha":"e117b3ca4edacf581f440dccfd7f3242f0312afa","analyzedAt":"2026-08-07T01:25:31.049Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}