tracel-ai/burn · error

Index out of bounds for ComposedDataset: {index} >= {}

Error message

Index out of bounds for ComposedDataset: {index} >= {}

What it means

`ComposedDataset::get` walks its child datasets, subtracting accumulated lengths, and panics if the requested `index` exceeds the total length of all composed datasets. Like the other burn datasets, out-of-range access is a panic rather than an `Err`.

Source

Thrown at crates/burn-dataset/src/transform/composed.rs:24

pub struct ComposedDataset<D> {
    datasets: Vec<D>,
}

impl<D, I, E> Dataset<I, E> for ComposedDataset<D>
where
    D: Dataset<I, E>,
    I: Clone,
    E: Error + Send + Sync + 'static,
{
    fn get(&self, index: usize) -> Result<I, E> {
        let mut current_index = 0;
        for dataset in self.datasets.iter() {
            if index < dataset.len() + current_index {
                return dataset.get(index - current_index);
            }
            current_index += dataset.len();
        }
        panic!(
            "Index out of bounds for ComposedDataset: {index} >= {}",
            self.len()
        );
    }
    fn len(&self) -> usize {
        let mut total = 0;
        for dataset in self.datasets.iter() {
            total += dataset.len();
        }
        total
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::FakeDataset;

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Guard with `if index < composed.len()` or iterate `0..composed.len()`.
  2. Recompute lengths from the composition itself rather than caching/splitting sizes externally.
  3. Verify each child dataset is non-empty and loaded correctly (log child `len()`s).
  4. Use `DataSplit`/index-based selection APIs that compute valid ranges across composed datasets.

Example fix

// before
let total = train.len(); // wrong: composed also includes val
let item = composed.get(i).unwrap();

// after
let total = composed.len();
if i < total {
    let item = composed.get(i)?;
}
Defensive patterns

Strategy: validation

Validate before calling

if index >= composed.len() {
    return Err(DatasetError::InvalidArgument(format!("index {index} out of bounds for ComposedDataset (len={})", composed.len())));
}

Prevention

When it happens

Trigger: Calling `composed.get(index)` with `index >= composed.len()`; composing N datasets but iterating with bounds from one of them or from stale metadata; empty composition (`ComposedDataset::new([])`) accessed at any index.

Common situations: Splits composed of train/val datasets where a downstream consumer assumes a single dataset length; batch loops computing bounds from a partial list of datasets; a child dataset became empty (bad file) so the total shrank.

Related errors


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