BoundaryML/baml · error

left operand of && must be bool at

Error message

left operand of && must be bool at {:?}

What it means

In evaluate_binary_op, the `&&` operator requires both operands to be BamlValueWithMeta::Bool. If the left operand is anything other than Bool (and did not short-circuit as false), the interpreter bails with this message. BAML does not coerce values to booleans.

Solutions

  1. Add an explicit boolean expression as the left operand: `items != null && items.length > 0`.
  2. Verify the left operand's type in the schema; if it is nullable, test `x != null` explicitly.
  3. If short-circuiting for side effects/defaulting, use an if/else expression instead of `&&`.

Example fix

// before (BAML)
let ok = items && items.length > 0;
// after
let ok = items != null && items.length > 0;
Defensive patterns

Strategy: type-guard

Validate before calling

// validate left operand before &&
if (typeof leftOperand !== 'boolean') throw new Error('left operand of && must be bool');

Type guard

const isBool = (v) => typeof v === 'boolean';

Try / catch

try {
  result = evaluate(expr);
} catch (e) {
  if (String(e).includes('left operand of && must be bool')) {
    result = evaluate(expr.replace(leftSrc, `(${leftSrc} != null)`));
  } else throw e;
}

Prevention

When it happens

Trigger: Evaluating `a && b` where `a` is a non-bool value: e.g. `items && items.length > 0`, `42 && flag`, `null && x`. Any string, int, null, list, or map used as the left operand of `&&`.

Common situations: JavaScript-style truthiness idioms copied into BAML; a variable that was expected to be bool but is actually an optional/nullable check result; refactoring where a comparison was accidentally dropped.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/6744267565d10987. Report an issue: GitHub.

Appendix: source

Thrown at engine/baml-compiler/src/thir/interpret.rs:2480

                .ok_or_else(|| anyhow!("unsupported types for > operator at {:?}", meta.0))?;
            BamlValueWithMeta::Bool(greater, meta.clone())
        }
        BinaryOperator::GtEq => {
            let ord_opt = compare_values(&left_val.clone(), &right_val.clone())?;
            let greater_eq = ord_opt
                .map(|ord| matches!(ord, std::cmp::Ordering::Greater | std::cmp::Ordering::Equal))
                .ok_or_else(|| anyhow!("unsupported types for >= operator at {:?}", meta.0))?;
            BamlValueWithMeta::Bool(greater_eq, meta.clone())
        }

        // Logical operations
        BinaryOperator::And => match left_val.clone() {
            BamlValueWithMeta::Bool(false, _) => BamlValueWithMeta::Bool(false, meta.clone()),
            BamlValueWithMeta::Bool(true, _) => match right_val.clone() {
                BamlValueWithMeta::Bool(b, _) => BamlValueWithMeta::Bool(b, meta.clone()),
                _ => bail!("right operand of && must be bool at {:?}", meta.0),
            },
            _ => bail!("left operand of && must be bool at {:?}", meta.0),
        },
        BinaryOperator::Or => match left_val.clone() {
            BamlValueWithMeta::Bool(true, _) => BamlValueWithMeta::Bool(true, meta.clone()),
            BamlValueWithMeta::Bool(false, _) => match right_val.clone() {
                BamlValueWithMeta::Bool(b, _) => BamlValueWithMeta::Bool(b, meta.clone()),
                _ => bail!("right operand of || must be bool at {:?}", meta.0),
            },
            _ => bail!("left operand of || must be bool at {:?}", meta.0),
        },

        // Bitwise operations (integer only)
        BinaryOperator::BitAnd => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                BamlValueWithMeta::Int(a & b, meta.clone())
            }
            _ => bail!("bitwise & requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::BitOr => match (left_val.clone(), right_val.clone()) {

View on GitHub (pinned to bd85ce9dee)