{"record":{"id":"024c68049936ad10","repo":"huggingface/candle","slug":"index-index-is-too-large-for-tensor-dimension-d","errorCode":null,"errorMessage":"index {index} is too large for tensor dimension {dim}","messagePattern":"index (.+?) is too large for tensor dimension (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-pyo3/src/lib.rs","lineNumber":175,"sourceCode":"        }\n    };\n}\n\npydtype!(i64, |v| v);\npydtype!(u8, |v| v);\npydtype!(u32, |v| v);\npydtype!(f16, f32::from);\npydtype!(bf16, f32::from);\npydtype!(f32, |v| v);\npydtype!(f64, |v| v);\npydtype!(F8E4M3, f32::from);\n\nfn actual_index(t: &Tensor, dim: usize, index: i64) -> ::candle::Result<usize> {\n    let dim = t.dim(dim)?;\n    if 0 <= index {\n        let index = index as usize;\n        if dim <= index {\n            ::candle::bail!(\"index {index} is too large for tensor dimension {dim}\")\n        }\n        Ok(index)\n    } else {\n        if (dim as i64) < -index {\n            ::candle::bail!(\"index {index} is too low for tensor dimension {dim}\")\n        }\n        Ok((dim as i64 + index) as usize)\n    }\n}\n\nfn actual_dim(t: &Tensor, dim: i64) -> ::candle::Result<usize> {\n    let rank = t.rank();\n    if 0 <= dim {\n        let dim = dim as usize;\n        if rank <= dim {\n            ::candle::bail!(\"dimension index {dim} is too large for tensor rank {rank}\")\n        }\n        Ok(dim)","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-pyo3/src/lib.rs#L157-L193","documentation":"This error comes from candle-pyo3's Python binding helper `actual_index`, which normalizes a user-supplied index for a tensor dimension before calling Tensor::get or Tensor::narrow. It is thrown when a non-negative index is greater than or equal to the size of the selected dimension, i.e. out of bounds. Candle uses bail! to fail fast rather than silently clamping or wrapping.","triggerScenarios":"Calling tensor.get(dim, index) or tensor.narrow(dim, start, len) from Python with index >= t.dim(dim), e.g. indexing element 10 of a dimension of size 10.","commonSituations":"Off-by-one loops over tensor sizes, assuming 0-based vs 1-based indexing, using a shape computed from a different tensor, or batches smaller than expected after preprocessing.","solutions":["Print the tensor shape (t.dims()) and check the index against the actual dimension size before indexing","Clamp or modulo the index: index = min(index, dim_size - 1)","Use negative indexing (e.g. -1 for the last element) which actual_index supports","Fix upstream data pipeline so tensors have the expected shape"],"exampleFix":"// before\nelem = t.get(0, 10)  # dim size is 10\n// after\nassert 0 <= 10 < t.dim(0)\nelem = t.get(0, 9)  # or t.get(0, -1) for the last element","handlingStrategy":"validation","validationCode":"let dim_size = t.dim(0)?;\nif !(0..dim_size).contains(&index) { panic!(\"index {} out of bounds for dim size {}\", index, dim_size); }","typeGuard":"fn is_valid_index(dim_size: usize, index: i64) -> bool {\n    index >= 0 && (index as usize) < dim_size\n}","tryCatchPattern":"match t.get(0, index) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"too large\") || e.to_string().contains(\"too low\") => fallback_value(),\n    Err(e) => return Err(e),\n}","preventionTips":["Always check tensor dims before indexing","Prefer negative indexing (-1) for last-element access","Log tensor shapes when debugging indexing errors","Guard loops with `for i in 0..t.dim(0)?` instead of hard-coded sizes"],"tags":["rust","pyo3","tensor","index-out-of-bounds"],"backgroundTag":"tensor-index-out-of-bounds","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}