BoundaryML/baml · error

unsupported types for / operator at

Error message

unsupported types for / operator at {:?}

What it means

The interpreter's `/` operator only supports numeric operand pairs (Int or Float in any combination). Any other operand type — string, bool, array, map, class instance, null, etc. — falls through to this catch-all bail in evaluate_binary_op. This indicates a type error in the BAML expression, not a numeric problem.

Solutions

  1. Cast operands to numbers first: `str_val as int` or `str_val as float` before dividing
  2. Print the types/values of both operands just before the division to see which is non-numeric
  3. If the value comes from LLM output, parse it (e.g. via a numeric-typed schema field or explicit cast) instead of using the raw string
  4. Ensure class fields/map values used in arithmetic are declared as int/float

Example fix

// before (.baml)
let half = input_value / 2; // input_value is a string
// after (.baml)
let half = input_value as int / 2;
Defensive patterns

Strategy: type-guard

Validate before calling

// .baml — ensure numeric operands before dividing
let a = input_value as float;
let b = divisor as float;
if b == 0.0 { return 0.0; }
let q = a / b;

Prevention

When it happens

Trigger: Evaluating a `/` expression where either operand is not Int/Float: e.g. `"10" / 2` (string numerator), dividing by a string, a bool used as a number, dividing arrays or class instances, or a variable whose declared/inferred type is not numeric.

Common situations: LLM output returned as a string that was never parsed to a number before division; mixing a numeric field from a class with a string field; using a value from a map (which may be untyped/string) in arithmetic; forgetting an `as int` / `as float` cast after parsing text.

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

Appendix: source

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

            (BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Float(b, _)) => {
                if b == 0.0 {
                    bail!("division by zero at {:?}", meta.0);
                }
                BamlValueWithMeta::Float(a / b, meta.clone())
            }
            (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());

View on GitHub (pinned to bd85ce9dee)