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
- Ensure both operands are Int; convert bools with an explicit conditional (`if b { 1 } else { 0 }`) before `^=`.
- Replace `^=` on a boolean toggle with `flag = !flag`.
- 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
- Use Bool and `!`/`&&`/`||` for flag logic; reserve bitwise ops for Int bitfields.
- Convert bools to 0/1 explicitly if bit tricks are truly needed.
- Audit BAML sources for `^=` and confirm both operand types statically.
- Test bitwise code with non-int inputs to catch regressions.
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
- bitwise &= requires integer operands
- bitwise |= requires integer operands
- shift <<= requires integer operands
- shift >>= requires integer operands
- shift >> requires integer operands at {:?}
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/1f60a2c4e5e52c61.
Report an issue: GitHub.