BoundaryML/baml · error

bitwise | requires integer operands at

Error message

bitwise | requires integer operands at {:?}

What it means

THIR interpreter runtime error: the bitwise `|` operator was applied to non-integer operands. Bitwise operations in BAML are defined only for Int/Int; a float, string, bool, or null operand on either side reaches this bail during evaluation of the binary expression.

Solutions

  1. Use `||` for logical OR of booleans; reserve `|` for integer bitmasks.
  2. Convert floats to ints explicitly before bitwise OR.
  3. Ensure flag values from config/LLM output are parsed as integers.

Example fix

// before (BAML)
let merged = allowRead | allowWrite;
// after
let merged = allowRead || allowWrite;
Defensive patterns

Strategy: validation

Validate before calling

// check integer operands before |
if (!Number.isInteger(a) || !Number.isInteger(b)) throw new Error('bitwise | requires integer operands');

Type guard

const isInt = (v) => typeof v === 'number' && Number.isInteger(v);

Try / catch

try {
  result = evaluate(expr);
} catch (e) {
  if (String(e).includes('bitwise | requires integer operands')) {
    result = evaluate(expr.replace(' | ', ' || '));
  } else throw e;
}

Prevention

When it happens

Trigger: Evaluating `a | b` with a bool, float, string, or null operand — e.g. combining flag enums stored as bools, or `mask | 4` where mask is a float.

Common situations: Using single `|` where `||` was intended for boolean OR; enum-like flag values decoded from JSON as floats; string masks from configuration.

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/ce05e667ee6d1156. Report an issue: GitHub.

Appendix: source

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

            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()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                BamlValueWithMeta::Int(a | b, meta.clone())
            }
            _ => bail!("bitwise | requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::BitXor => 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::Shl => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                if b < 0 {
                    bail!("negative shift amount at {:?}", meta.0);
                }
                BamlValueWithMeta::Int(a << b, meta.clone())
            }
            _ => bail!("shift << requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::Shr => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {

View on GitHub (pinned to bd85ce9dee)