{"record":{"id":"2c0630ebc35d0a17","repo":"huggingface/candle","slug":"empty-array","errorCode":null,"errorMessage":"empty array","messagePattern":"empty array","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/device.rs","lineNumber":119,"sourceCode":"        }\n        S::to_cpu_storage_owned(vec)\n    }\n}\n\nimpl<S: WithDType> NdArray for Vec<S> {\n    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>> {","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/device.rs#L101-L137","documentation":"When building a tensor from Vec<&[S]> (2-D slice rows), NdArray::shape returns an error if the outer vector is empty, since no shape can be derived. This check runs when converting the nested array into a Shape.","triggerScenarios":"Calling Tensor::new(vec![]), Tensor::from_slice of an empty slice-of-slices, or Tensor::from_vecs with an empty Vec<&[S]> on CPU.","commonSituations":"Building tensors from dynamically collected data that ended up empty (no rows loaded, empty batch).","solutions":["Check the collection is non-empty before constructing the tensor.","Return a meaningful application-level error for empty input instead of constructing a tensor.","Construct with an explicit shape/dtype if a placeholder tensor is needed."],"exampleFix":"// before\nlet t = Tensor::new(rows, &Device::Cpu)?;\n// after\nanyhow::ensure!(!rows.is_empty(), \"need at least one row\");\nlet t = Tensor::new(rows, &Device::Cpu)?;","handlingStrategy":"validation","validationCode":"if rows.is_empty() {\n    return Err(anyhow::anyhow!(\"cannot build tensor from empty rows\"));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate input collections before Tensor::new.","Handle empty batches explicitly in data-loading code.","Add unit tests for empty-input paths."],"tags":["cpu","tensor-construction","empty-input"],"backgroundTag":"empty-input-array","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}