{"record":{"id":"c5b731395a64b2af","repo":"huggingface/candle","slug":"two-elements-have-different-shapes-shape-shap","errorCode":null,"errorMessage":"two elements have different shapes {shape:?} {shape0:?}","messagePattern":"two elements have different shapes (.+?) (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/device.rs","lineNumber":172,"sourceCode":"        let mut dst = Vec::with_capacity(len);\n        for v in self.iter() {\n            dst.extend(v.iter().copied());\n        }\n        S::to_cpu_storage_owned(dst)\n    }\n}\n\nimpl<S: WithDType> NdArray for Vec<Vec<Vec<S>>> {\n    fn shape(&self) -> Result<Shape> {\n        if self.is_empty() {\n            crate::bail!(\"empty array\")\n        }\n        let shape0 = self[0].shape()?;\n        let n = self.len();\n        for v in self.iter() {\n            let shape = v.shape()?;\n            if shape != shape0 {\n                crate::bail!(\"two elements have different shapes {shape:?} {shape0:?}\")\n            }\n        }\n        Ok(Shape::from([[n].as_slice(), shape0.dims()].concat()))\n    }\n\n    fn to_cpu_storage(&self) -> CpuStorage {\n        if self.is_empty() {\n            return S::to_cpu_storage_owned(vec![]);\n        }\n        let len: usize = self\n            .iter()\n            .map(|v| v.iter().map(|v| v.len()).sum::<usize>())\n            .sum();\n        let mut dst = Vec::with_capacity(len);\n        for v1 in self.iter() {\n            for v2 in v1.iter() {\n                dst.extend(v2.iter().copied());\n            }","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/device.rs#L154-L190","documentation":"When building a 3-D tensor from Vec<Vec<Vec<S>>>, every element's inner 2-D shape must match the first element's. NdArray::shape recursively computes each sub-shape and bails on the first mismatch, since the result must be rectangular.","triggerScenarios":"Tensor::new with Vec<Vec<Vec<_>>> where any sample has a different (rows, cols) shape than sample 0.","commonSituations":"Batches of images with differing resolutions stacked without resizing; mixed-size nested samples.","solutions":["Resize/pad all sub-elements to a common 2-D shape before construction.","Validate each element's shape equals the first before calling Tensor::new.","Group samples by shape and build one tensor per shape group."],"exampleFix":"// before\nlet t = Tensor::new(vec![vec![vec![1.0]], vec![vec![1.0, 2.0]]], &dev)?;\n// after\nlet t = Tensor::new(vec![vec![vec![1.0, 0.0]], vec![vec![1.0, 2.0]]], &dev)?;","handlingStrategy":"validation","validationCode":"let shape0 = (samples[0].len(), samples[0][0].len());\nif samples.iter().any(|s| (s.len(), s[0].len()) != shape0) {\n    return Err(anyhow::anyhow!(\"all samples must share the same 2d shape\"));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Resize/pad all samples to a fixed shape before batching.","Group mixed-shape samples into separate tensors.","Validate shapes at batch assembly time, not at Tensor::new."],"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"}