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 or-assign supports only Int/Int operand pairs; anything else falls through to this bail. It enforces strict integer-only semantics for bitwise assignment in BAML.

Source

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

                                        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()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    if b < 0 {
                                        bail!("negative shift amount in <<= operator");
                                    }
                                    BamlValueWithMeta::Int(a << b, meta)
                                }
                                _ => bail!("shift <<= requires integer operands"),
                            },
                            AssignOp::ShrAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    if b < 0 {
                                        bail!("negative shift amount in >>= operator");
                                    }
                                    BamlValueWithMeta::Int(a >> b, meta)
                                }
                                _ => bail!("shift >>= requires integer operands"),

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Initialize flag variables as Int (0/1), not Bool, when using `|=`.
  2. Convert bool operands explicitly to 0/1 before or-assigning.
  3. Verify the right-hand side value's type from its producer (config, LLM parse) is Int.

Example fix

// before (BAML)
let opts = false;
opts |= needsFlag; // error: bitwise |= requires integer operands

// after
let opts = 0;
opts |= if needsFlag { 1 } else { 0 };
Defensive patterns

Strategy: type-guard

Validate before calling

// ensure flag-accumulation operands are ints before |= runs
function assertOrOperands(acc: unknown, bit: unknown): void {
  if (!Number.isInteger(acc) || !Number.isInteger(bit)) {
    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")) {
    // re-initialize flags as Int (0/1) or map bools to 0/1 and retry
  } else { throw e; }
}

Prevention

When it happens

Trigger: Executing `flags |= bit` where `flags` or `bit` is a Float, Bool, String, or other non-Int value at runtime, e.g. `enabled |= true`.

Common situations: Flag-accumulation code where a default was set to `false` (Bool) instead of `0` (Int), or option values coming from LLM output typed as strings.

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