huggingface/candle · error
two elements have different len {m} {}
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.
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)
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.
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
- 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.
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
- two elements have different shapes {shape:?} {shape0:?}
- empty array
- backward not supported for non uniform upscaling factors
- in_channel mismatch between input ({c_in}) and kernel ({c_in
- in_channel {c_in} is not divisible by the number of groups
AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02).
Data as JSON: /api/errors/b68df835717be4ee.
Report an issue: GitHub.