BoundaryML/baml · error

bitwise ^= requires integer operands

Error message

bitwise ^= requires integer operands

What it means

Raised by the BAML interpreter when evaluating `x ^= y`. Bitwise xor-assign is defined only for Int/Int operand pairs; when either side is another BAML value type the match falls through to this bail. It exists to keep BAML's bitwise operators strictly integer-only.

Source

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

                                }
                                _ => bail!("unsupported types for /= operator"),
                            },
                            AssignOp::ModAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    if b == 0 {
                                        bail!("modulo by zero in %= operator");
                                    }
                                    BamlValueWithMeta::Int(a % b, meta)
                                }
                                _ => bail!("unsupported types for %= operator"),
                            },
                            AssignOp::BitXorAssign => {
                                match (current_val.clone(), rhs_val.clone()) {
                                    (
                                        BamlValueWithMeta::Int(a, meta),
                                        BamlValueWithMeta::Int(b, _),
                                    ) => BamlValueWithMeta::Int(a ^ b, meta),
                                    _ => bail!("bitwise ^= requires integer operands"),
                                }
                            }
                            AssignOp::BitAndAssign => {
                                match (current_val.clone(), rhs_val.clone()) {
                                    (
                                        BamlValueWithMeta::Int(a, meta),
                                        BamlValueWithMeta::Int(b, _),
                                    ) => BamlValueWithMeta::Int(a & b, meta),
                                    _ => bail!("bitwise &= requires integer operands"),
                                }
                            }
                            AssignOp::BitOrAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    BamlValueWithMeta::Int(a | b, meta)
                                }
                                _ => bail!("bitwise |= requires integer operands"),
                            },
                            AssignOp::ShlAssign => match (current_val.clone(), rhs_val.clone()) {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Ensure both operands are Int; convert bools with an explicit conditional (`if b { 1 } else { 0 }`) before `^=`.
  2. Replace `^=` on a boolean toggle with `flag = !flag`.
  3. Coerce numeric strings/floats to Int explicitly before the assignment.

Example fix

// before (BAML)
let flag = true;
flag ^= other; // error: bitwise ^= requires integer operands

// after
let flag = 1;
flag ^= otherAsInt; // both Int
Defensive patterns

Strategy: type-guard

Validate before calling

// before xor-assign, verify both operands are integers
function assertXorOperands(a: unknown, b: unknown): void {
  if (!Number.isInteger(a) || !Number.isInteger(b)) {
    throw new Error("bitwise ^= requires integer operands in BAML");
  }
}

Type guard

const isInt = (v: unknown): v is number => typeof v === "number" && Number.isInteger(v);

Try / catch

try {
  await runBaml(program);
} catch (e) {
  if (String(e).includes("bitwise ^= requires integer operands")) {
    // replace ^= with bool negation or convert operands to Int and retry
  } else { throw e; }
}

Prevention

When it happens

Trigger: Running `a ^= b` where `a` or `b` is a Float, String, Bool, or other non-Int BamlValue at runtime — e.g. `flag ^= true` treating bools as bits.

Common situations: Developers coming from C/JS expect bools or floats to coerce to ints for bitwise ops; in BAML they do not, so xor-assign on a bool flag or a float counter fails here.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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