huggingface/candle · error

two elements have different len

Error message

two elements have different len {m} {}

What it means

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.

Solutions

  1. Pad all rows to the same length before constructing the tensor.
  2. Validate row lengths up front and report which row differs.
  3. Build a 1-D tensor and reshape manually if the data is genuinely ragged.

Example fix

// before
let t = Tensor::new(vec![&[1.0, 2.0][..], &[3.0][..]], &dev)?;
// after
let t = Tensor::new(vec![&[1.0, 2.0][..], &[3.0, 0.0][..]], &dev)?;
Defensive patterns

Strategy: validation

Validate before calling

let m = rows[0].len();
if rows.iter().any(|r| r.len() != m) {
    return Err(anyhow::anyhow!("rows must all have length {m}"));
}

Prevention

When it happens

Trigger: Tensor::new / from_slice with Vec<&[S]> where rows have differing lengths (e.g. [[1,2],[3]]).

Common situations: Tokenized variable-length sequences passed without padding; CSV/parsed rows of uneven width.

Related errors


AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02). Data as JSON: /api/errors/b68df835717be4ee. Report an issue: GitHub.

Appendix: source

Thrown at candle-core/src/device.rs:125

    fn shape(&self) -> Result<Shape> {
        Ok(Shape::from(self.len()))
    }

    fn to_cpu_storage(&self) -> CpuStorage {
        S::to_cpu_storage(self.as_slice())
    }
}

impl<S: WithDType> NdArray for Vec<&[S]> {
    fn shape(&self) -> Result<Shape> {
        if self.is_empty() {
            crate::bail!("empty array")
        }
        let n = self.len();
        let m = self[0].len();
        for v in self.iter() {
            if v.len() != m {
                crate::bail!("two elements have different len {m} {}", v.len())
            }
        }
        Ok(Shape::from((n, m)))
    }

    fn to_cpu_storage(&self) -> CpuStorage {
        let data = self.iter().copied().flatten().copied().collect::<Vec<_>>();
        S::to_cpu_storage_owned(data)
    }
}

impl<S: WithDType> NdArray for Vec<Vec<S>> {
    fn shape(&self) -> Result<Shape> {
        if self.is_empty() {
            crate::bail!("empty array")
        }
        let n = self.len();
        let m = self[0].len();

View on GitHub (pinned to d5fee525bf)