tracel-ai/burn · error

Expected array of length {}, got {}

Error message

Expected array of length {}, got {}

What it means

Length-mismatch guard when visiting/mapping a fixed-size `Module<const N>` array: code expects the module's element count to equal the compile-time `N`, and the formatted message reports the expected N versus the actual runtime length. It fires when a deserialized or manually constructed container has a different number of elements than the static array type declares — the input at fault is the wrongly sized module data.

Source

Thrown at crates/burn-core/src/module/param/primitive.rs:192

            let index_str = alloc::format!("{}", i);
            visitor.enter_module(&index_str, "Array");
            module.visit(visitor);
            visitor.exit_module(&index_str, "Array");
        }
    }

    fn map<M: ModuleMapper>(self, mapper: &mut M) -> Self {
        let mut result = Vec::with_capacity(N);
        for (i, module) in IntoIterator::into_iter(self).enumerate() {
            let index_str = alloc::format!("{}", i);
            mapper.enter_module(&index_str, "Array");
            let mapped = module.map(mapper);
            mapper.exit_module(&index_str, "Array");
            result.push(mapped);
        }
        result
            .try_into()
            .unwrap_or_else(|v: Vec<T>| panic!("Expected array of length {}, got {}", N, v.len()))
    }

    fn to_device(self, device: &Device) -> Self {
        self.map(|module| module.to_device(device))
    }

    fn fork(self, device: &Device) -> Self {
        self.map(|module| module.fork(device))
    }
}

impl<const N: usize, T: ModuleDisplay> ModuleDisplayDefault for [T; N] {
    fn content(&self, content: Content) -> Option<Content> {
        self.iter()
            .enumerate()
            .fold(content, |acc, (i, module)| {
                let index = format!("{i}");
                acc.add(&index, module)

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Ensure the record/state being loaded into the array contains exactly N elements matching the const generic.
  2. When loading checkpoints from a changed model, migrate or reshape the parameter list to N before `map`/`to_device`/`fork`.
  3. Add a pre-load check comparing element count with N to fail with a clearer message.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/burn-core/src/module/param/primitive.rs:192 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/00afbdcc168b4cce. Report an issue: GitHub.