{"record":{"id":"8c980f14cdf57f17","repo":"hiyouga/LlamaFactory","slug":"invalid-index-type-type-index","errorCode":null,"errorMessage":"Invalid index type {type(index)}.","messagePattern":"Invalid index type (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/plugins/data_plugins/loader.py","lineNumber":108,"sourceCode":"\ndef select_data_sample(\n    data_index: list[tuple[str, int]], index: slice | list[int] | Any\n) -> tuple[str, int] | list[tuple[str, int]]:\n    \"\"\"Select dataset samples.\n\n    Args:\n        data_index (list[tuple[str, int]]): List of (dataset_name, sample_index).\n        index (Union[slice, list[int], Any]): Index of dataset samples.\n\n    Returns:\n        Union[tuple[str, int], list[tuple[str, int]]]: Selected dataset samples.\n    \"\"\"\n    if isinstance(index, slice):\n        return [data_index[i] for i in range(*index.indices(len(data_index)))]\n    elif isinstance(index, list):\n        return [data_index[i] for i in index]\n    else:\n        raise ValueError(f\"Invalid index type {type(index)}.\")\n","sourceCodeStart":90,"sourceCodeEnd":109,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/plugins/data_plugins/loader.py#L90-L109","documentation":"The concatenated dataset wrapper supports only slice and list[int] indexing on its data_index. Any other index type (int, numpy scalar, tensor, tuple) raises this ValueError from _get_subset_of_data.","triggerScenarios":"Calling dataset[0] with a plain int, dataset[np.int64(3)], or indexing with a torch tensor / tuple, instead of dataset[[0]] or dataset[0:1].","commonSituations":"Custom training loops or debugging code that uses int indexing out of habit; PyTorch DataLoader samplers that hand over numpy/tensor indices; iterating with random single indices.","solutions":["Wrap single indices in a list: dataset[[i]] instead of dataset[i].","Use slices for ranges: dataset[start:end].","Convert numpy/tensor indices to a Python list of ints before indexing.","For iteration, rely on the standard DataLoader which emits valid index lists."],"exampleFix":"# before\nsample = dataset[3]\n\n# after\nsample = dataset[[3]][0]","handlingStrategy":"type-guard","validationCode":"def norm_index(i):\n    if isinstance(i, slice):\n        return i\n    if hasattr(i, 'item'):\n        i = i.item()\n    if isinstance(i, int):\n        return [i]\n    if isinstance(i, (list, tuple)):\n        return [int(x) for x in i]\n    raise TypeError(f'unsupported index {type(i)}')","typeGuard":"def is_valid_index(i) -> bool:\n    \"\"\"True for slice or list[int] indices accepted by the v1 dataset.\"\"\"\n    return isinstance(i, slice) or (isinstance(i, list) and all(isinstance(x, int) for x in i))","tryCatchPattern":"try:\n    rows = dataset[idx]\nexcept ValueError as e:\n    if 'Invalid index type' in str(e):\n        rows = dataset[norm_index(idx)]\n    else:\n        raise","preventionTips":["Always index with list or slice; wrap ints in [i].","Convert numpy/tensor indices to plain Python ints at the boundary.","Encapsulate dataset access behind one helper in your codebase."],"tags":["data","indexing","api-misuse","v1-api"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}