BoundaryML/baml · error

modulo by zero at

Error message

modulo by zero at {:?}

What it means

The BAML `%` (modulo) operator supports only the (Int, Int) operand pair in this interpreter. A zero integer denominator also raises 'modulo by zero', and any non-Int operand pair reaches this catch-all bail. Modulo by zero is mathematically undefined, and float/string operands are not implemented for `%`.

Solutions

  1. Use integers on both sides: cast with `as int` if needed (for floats consider `a - (a / b) * b` or refactor)
  2. Check the divisor is non-zero before applying `%`
  3. Print both operands/types before the operation to identify the offending type
  4. If a string from LLM output, parse to int first

Example fix

// before (.baml)
let rem = value % 2.0; // float not supported
// after (.baml)
let rem = value as int % 2;
Defensive patterns

Strategy: validation

Validate before calling

// .baml — ints only, non-zero divisor
let bi = b as int;
if bi == 0 { return 0; }
let rem = (a as int) % bi;

Prevention

When it happens

Trigger: Evaluating `a % b` where: (1) both operands are Int but b == 0 (raises the 'modulo by zero' variant at the same site), or (2) either operand is Float/String/etc. (raises this 'unsupported types for % operator' message). Both surface from the same Mod arm of evaluate_binary_op.

Common situations: Applying `%` to floats (e.g. `3.5 % 2`), computing parity on a string-parsed number, modulo on values from a map or LLM output that arrived as strings, or `% 0` from an unset/defaulting variable.

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

Appendix: source

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

            }
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Float(b, _)) => {
                if b == 0.0 {
                    bail!("division by zero at {:?}", meta.0);
                }
                BamlValueWithMeta::Float((a as f64) / b, meta.clone())
            }
            (BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Int(b, _)) => {
                if b == 0 {
                    bail!("division by zero at {:?}", meta.0);
                }
                BamlValueWithMeta::Float(a / (b as f64), meta.clone())
            }
            _ => bail!("unsupported types for / operator at {:?}", meta.0),
        },
        BinaryOperator::Mod => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                if b == 0 {
                    bail!("modulo by zero at {:?}", meta.0);
                }
                BamlValueWithMeta::Int(a % b, meta.clone())
            }
            _ => bail!("unsupported types for % operator at {:?}", meta.0),
        },

        // Comparison operations
        BinaryOperator::Eq => {
            let equal = values_equal(&left_val.clone(), &right_val.clone());
            BamlValueWithMeta::Bool(equal, meta.clone())
        }
        BinaryOperator::Neq => {
            let not_equal = !values_equal(&left_val.clone(), &right_val.clone());
            BamlValueWithMeta::Bool(not_equal, meta.clone())
        }
        BinaryOperator::Lt => {
            let ord_opt = compare_values(&left_val.clone(), &right_val.clone())?;
            let less = ord_opt

View on GitHub (pinned to bd85ce9dee)