tracel-ai/burn · error

Expected bool handle, got {}

Error message

Expected bool handle, got {}

What it means

`BackendIr::bool_tensor` for the Flex backend unwraps a `TensorHandle` expecting `HandleKind::Bool`. Any other handle kind triggers a panic with 'Expected bool handle, got <name>'. This keeps bool results (masks, comparisons) from being read from wrongly-typed handles.

Source

Thrown at crates/burn-flex/src/backend.rs:190

    fn float_tensor(handle: TensorHandle<Self::Handle>) -> FlexTensor {
        match handle.handle {
            HandleKind::Float(t) => t,
            _ => panic!("Expected float handle, got {}", handle.handle.name()),
        }
    }

    fn int_tensor(handle: TensorHandle<Self::Handle>) -> FlexTensor {
        match handle.handle {
            HandleKind::Int(t) => t,
            _ => panic!("Expected int handle, got {}", handle.handle.name()),
        }
    }

    fn bool_tensor(handle: TensorHandle<Self::Handle>) -> FlexTensor {
        match handle.handle {
            HandleKind::Bool(t) => t,
            _ => panic!("Expected bool handle, got {}", handle.handle.name()),
        }
    }

    fn quantized_tensor(handle: TensorHandle<Self::Handle>) -> FlexQTensor {
        match handle.handle {
            HandleKind::Quantized(t) => t,
            _ => panic!("Expected quantized handle, got {}", handle.handle.name()),
        }
    }

    fn float_tensor_handle(tensor: FlexTensor) -> Self::Handle {
        HandleKind::Float(tensor)
    }

    fn int_tensor_handle(tensor: FlexTensor) -> Self::Handle {
        HandleKind::Int(tensor)
    }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Make the producing kernel return `HandleKind::Bool` for boolean results.
  2. Match on the handle kind and convert explicitly (e.g. compare against zero to build a bool tensor) before use.
  3. Check output-slot selection so the correct handle is passed to `bool_tensor`.

Example fix

// before
let t = BackendIr::bool_tensor(handle); // panics: got int
// after
let t = match handle.handle {
    HandleKind::Bool(t) => t,
    HandleKind::Int(i) => i.not_equal_elem(0), // build bool from mask
    other => panic!("unexpected handle {}", other.name()),
};
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(handle.handle, HandleKind::Bool(_)) { /* route to correct converter */ }

Type guard

fn as_bool_handle(t: HandleKind<Flex>) -> Option<FlexTensor> {
    match t { HandleKind::Bool(b) => Some(b), _ => None }
}

Try / catch

let result = std::panic::catch_unwind(AssertUnwindSafe(|| Flex::bool_tensor(handle.clone())));
match result {
    Ok(t) => use_bool(t),
    Err(_) => eprintln!("handle was not bool"),
}

Prevention

When it happens

Trigger: Calling `bool_tensor()` on a handle that holds Float/Int/Quantized — commonly when a comparison or mask op's kernel returned a non-bool handle (e.g. int-encoded mask) or the caller fetched the wrong output.

Common situations: Custom Flex comparison kernels emitting int/float handles instead of bool, or consuming a handle from the wrong tensor slot after an op with multiple outputs.

Related errors


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