BoundaryML/baml · error

unsupported types for % operator at

Error message

unsupported types for % operator at {:?}

What it means

The interpreter implements `%` (modulo) only for two Int operands; any other combination (float, string, bool, arrays, etc.) hits this catch-all bail in evaluate_binary_op's Mod arm. Use `/` for floats, or cast operands to Int if truncation is acceptable.

Solutions

  1. Cast operands to int: `a as int % b as int`
  2. Replace float modulo with an equivalent arithmetic expression or refactor to integer math
  3. Inspect operand types before the operation (print them) to find the non-Int value
  4. Parse string values to int before arithmetic

Example fix

// before (.baml)
let isOdd = count % 2; // count: float
// after (.baml)
let isOdd = count as int % 2;
Defensive patterns

Strategy: type-guard

Validate before calling

// .baml — coerce to int before modulo
let countInt = count as int;
let isOdd = countInt % 2;

Prevention

When it happens

Trigger: Evaluating `a % b` where a or b is a Float, String, Bool, or non-numeric value instead of Int — e.g. `x % 2` where x is a float field, or `count % divisor` where count came from a string.

Common situations: Float modulo attempted after float division; string values from LLM output used directly in `%`; numeric values stored in untyped containers (maps/arrays) and used in modulo without casting.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

                }
                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
                .map(|ord| matches!(ord, std::cmp::Ordering::Less))
                .ok_or_else(|| anyhow!("unsupported types for < operator at {:?}", meta.0))?;
            BamlValueWithMeta::Bool(less, meta.clone())
        }

View on GitHub (pinned to bd85ce9dee)