BoundaryML/baml · error
unsupported types for %= operator
Error message
unsupported types for %= operator
What it means
This error comes from the BAML interpreter when evaluating a compound assignment `x %= y`. The runtime only supports modulo-assign when both the current value and the right-hand side are Int; any other operand type (string, float, bool, etc.) makes the match arm fall through to this bail. The library throws it because the BAML language defines `%=` only for integer operands.
Source
Thrown at engine/baml-compiler/src/thir/interpret.rs:964
(
BamlValueWithMeta::Float(a, meta),
BamlValueWithMeta::Int(b, _),
) => {
if b == 0 {
bail!("division by zero in /= operator");
}
BamlValueWithMeta::Float(a / (b as f64), meta)
}
_ => 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"),
}View on GitHub (pinned to bd85ce9dee)
Solutions
- Check both operands of `%=` are BAML Int values; cast/coerce floats to int first (e.g. use an int division/truncation helper before `%=`).
- Verify the variable's declared/inferred type in the BAML schema matches Int and that LLM output is parsed as an int.
- If you need float modulo, implement it manually with an explicit expression (e.g. `a - (a/b).floor()*b` pattern) instead of `%=`.
- Confirm the value isn't wrapped in a meta/optional variant the interpreter sees as non-Int.
Example fix
// before (BAML) let ratio = 10.0; ratio %= 3; // error: unsupported types for %= operator // after let ratio = 10; ratio %= 3; // Int operands only
Defensive patterns
Strategy: type-guard
Validate before calling
// caller-side check before running the %= statement
function assertIntsForModulo(a: unknown, b: unknown): void {
if (!Number.isInteger(a) || !Number.isInteger(b)) {
throw new Error(`%= requires Int operands, got ${typeof a} and ${typeof b}`);
}
if (b === 0) throw new Error("modulo by zero in %= operator");
} Type guard
const isBamlInt = (v: unknown): v is number => typeof v === "number" && Number.isInteger(v);
Try / catch
try {
await runBaml(program);
} catch (e) {
if (String(e).includes("unsupported types for %= operator")) {
// coerce operands to Int and retry, or surface a type error to the author
} else { throw e; }
} Prevention
- Declare counters and numeric fields as Int, never Float, when using %=.
- Never feed LLM-parsed values directly into %= without validating they are integers.
- Prefer explicit manual modulo expressions when floats are involved.
- Add unit tests covering every compound-assignment operator with the exact operand types used.
When it happens
Trigger: Executing a BAML statement like `x %= y` where `x` or `y` is not an Int at runtime — e.g. `x` was assigned a Float (`x = 5.5; x %= 2`) or a String value, or `y` came from a prompt/LLM-parsed value typed as something other than Int.
Common situations: Developers assume BAML `%= behaves like C/Python and apply it to floats or to values parsed from LLM output whose type is not what they expect; a variable initialized as float then modulo-assigned triggers this immediately.
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
- unsupported types for += in C-for after clause
- bitwise ^= requires integer operands
- bitwise &= requires integer operands
- bitwise |= requires integer operands
- shift <<= requires integer operands
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/044ca1d1d26767ed.
Report an issue: GitHub.