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

  1. Check both operands of `%=` are BAML Int values; cast/coerce floats to int first (e.g. use an int division/truncation helper before `%=`).
  2. Verify the variable's declared/inferred type in the BAML schema matches Int and that LLM output is parsed as an int.
  3. If you need float modulo, implement it manually with an explicit expression (e.g. `a - (a/b).floor()*b` pattern) instead of `%=`.
  4. 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

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


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