tracel-ai/burn · error

Expected bool data type, got {value:?}

Error message

Expected bool data type, got {value:?}

What it means

`impl From<DType> for BoolDType` in burn-std only knows how to convert DTypes that represent boolean data (DType::Bool variants, plus U8/U32 as legacy compat aliases for the old BoolElem associated type). If the DType is a float, integer, or other non-bool type, the conversion is undefined and the code panics. It means you asked a DType to be interpreted as a boolean dtype when it is not one.

Source

Thrown at crates/burn-std/src/tensor/dtype.rs:304

/// Boolean dtype.
///
/// This is currently an alias to [`BoolStore`], since it only varies by the storage representation.
pub type BoolDType = BoolStore;

#[allow(deprecated)]
impl From<DType> for BoolDType {
    fn from(value: DType) -> Self {
        match value {
            DType::Bool(store) => match store {
                BoolStore::Native => BoolDType::Native,
                BoolStore::U8 => BoolDType::U8,
                BoolStore::U32 => BoolDType::U32,
            },
            // For compat BoolElem associated type
            DType::U8 => BoolDType::U8,
            DType::U32 => BoolDType::U32,
            _ => panic!("Expected bool data type, got {value:?}"),
        }
    }
}

impl From<BoolDType> for DType {
    fn from(value: BoolDType) -> Self {
        match value {
            BoolDType::Native => DType::Bool(BoolStore::Native),
            BoolDType::U8 => DType::Bool(BoolStore::U8),
            BoolDType::U32 => DType::Bool(BoolStore::U32),
        }
    }
}

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

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Check the tensor's dtype before converting; ensure it is DType::Bool (or U8/U32) before calling `BoolDType::from`.
  2. Cast the tensor to bool (e.g. `tensor.bool()` / `bool_mask` helpers) instead of converting the dtype directly.
  3. If this comes from library internals after an upgrade, verify storage types: bool tensors saved/loaded with a different BoolStore (Native/U8/U32) may arrive mis-typed; re-export/reload data with the current version.

Example fix

// before
let bdt: BoolDType = tensor.dtype().into(); // panics if dtype is F32
// after
let bdt = match tensor.dtype() {
    DType::Bool(_) | DType::U8 | DType::U32 => BoolDType::from(tensor.dtype()),
    _ => panic!("mask tensor must be bool, got {:?}", tensor.dtype()),
};
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_bool_compatible(dtype: DType) {
    match dtype {
        DType::Bool(_) | DType::U8 | DType::U32 => {}
        other => panic!("expected bool-like dtype, got {other:?}"),
    }
}

Type guard

fn is_bool_dtype(dtype: &DType) -> bool {
    matches!(dtype, DType::Bool(_) | DType::U8 | DType::U32)
}

Try / catch

// panic-based; guard the conversion instead
let bdt = if is_bool_dtype(&dtype) { BoolDType::from(dtype) } else { return Err("not a bool dtype"); };

Prevention

When it happens

Trigger: Calling `BoolDType::from(dtype)` (or `dtype.into()`) where `dtype` is e.g. DType::F32, DType::I64, etc. Typically reached through code paths that assume a tensor/operand has a bool dtype (mask handling, bool storage config) but receive a non-bool tensor.

Common situations: Passing a float/int tensor where a boolean mask is expected; mixing dtypes when constructing bool-backed tensors with U8/U32 storage; generic code that assumes `B::BoolElem`-compatible dtype but is fed another dtype after a backend or burn-version change.

Related errors


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