{"record":{"id":"b68df835717be4ee","repo":"huggingface/candle","slug":"two-elements-have-different-len-m","errorCode":null,"errorMessage":"two elements have different len {m} {}","messagePattern":"two elements have different len (.+?) (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/device.rs","lineNumber":125,"sourceCode":"    fn shape(&self) -> Result<Shape> {\n        Ok(Shape::from(self.len()))\n    }\n\n    fn to_cpu_storage(&self) -> CpuStorage {\n        S::to_cpu_storage(self.as_slice())\n    }\n}\n\nimpl<S: WithDType> NdArray for Vec<&[S]> {\n    fn shape(&self) -> Result<Shape> {\n        if self.is_empty() {\n            crate::bail!(\"empty array\")\n        }\n        let n = self.len();\n        let m = self[0].len();\n        for v in self.iter() {\n            if v.len() != m {\n                crate::bail!(\"two elements have different len {m} {}\", v.len())\n            }\n        }\n        Ok(Shape::from((n, m)))\n    }\n\n    fn to_cpu_storage(&self) -> CpuStorage {\n        let data = self.iter().copied().flatten().copied().collect::<Vec<_>>();\n        S::to_cpu_storage_owned(data)\n    }\n}\n\nimpl<S: WithDType> NdArray for Vec<Vec<S>> {\n    fn shape(&self) -> Result<Shape> {\n        if self.is_empty() {\n            crate::bail!(\"empty array\")\n        }\n        let n = self.len();\n        let m = self[0].len();","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/device.rs#L107-L143","documentation":"When building a 2-D tensor from Vec<&[S]>, all rows must have the same length. NdArray::shape compares each row's length to the first row's and bails on mismatch, since ragged data cannot form a rectangular Shape.","triggerScenarios":"Tensor::new / from_slice with Vec<&[S]> where rows have differing lengths (e.g. [[1,2],[3]]).","commonSituations":"Tokenized variable-length sequences passed without padding; CSV/parsed rows of uneven width.","solutions":["Pad all rows to the same length before constructing the tensor.","Validate row lengths up front and report which row differs.","Build a 1-D tensor and reshape manually if the data is genuinely ragged."],"exampleFix":"// before\nlet t = Tensor::new(vec![&[1.0, 2.0][..], &[3.0][..]], &dev)?;\n// after\nlet t = Tensor::new(vec![&[1.0, 2.0][..], &[3.0, 0.0][..]], &dev)?;","handlingStrategy":"validation","validationCode":"let m = rows[0].len();\nif rows.iter().any(|r| r.len() != m) {\n    return Err(anyhow::anyhow!(\"rows must all have length {m}\"));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pad variable-length rows before tensor construction.","Validate uniform row lengths right after data parsing.","Keep a pad-to-length helper in shared preprocessing code."],"tags":["cpu","tensor-construction","shape-mismatch"],"backgroundTag":"ragged-nested-array","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}