BoundaryML/baml · error

unsupported types for += operator

Error message

unsupported types for += operator

What it means

The += compound assignment operator in the BAML interpreter only supports int+=int, float+=int, int+=float (to float), and string+=string concatenation. Any other operand type combination makes handle_statement bail with this error.

Solutions

  1. Cast operands to matching types before += (e.g. use string interpolation or explicit conversion).
  2. Verify the variable being accumulated is initialized to 0, 0.0, or "" as appropriate.
  3. Use explicit addition with correct types instead of compound assignment for mixed types.

Example fix

// before
count += 0.5 // count is Int, no float+=float
// after
let count = count as float; count += 0.5
Defensive patterns

Strategy: validation

Validate before calling

if (!isNumber(acc) && typeof acc !== 'string') throw new Error('+= requires numeric or string operands'); if (isNumber(acc) !== isNumber(incr) && typeof acc === 'string') throw new Error('cannot += number to string');

Type guard

function isNumber(v) { return typeof v === 'number'; }

Try / catch

try { applyAssignOp('AddAssign', lhs, rhs); } catch (e) { if (e.message.includes('unsupported types for +=')) { /* coerce operands or report type error */ } else { throw e; } }

Prevention

When it happens

Trigger: Executing `x += y` where current x and y are not both numeric or both strings (e.g. bool, null, list, map operands).

Common situations: Accidentally concatenating a string with a number, or += on a variable initialized to null/bool instead of a numeric/string value.

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

Appendix: source

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

                                    BamlValueWithMeta::Int(a + b, meta)
                                }
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => BamlValueWithMeta::Float(a + b, meta),
                                (
                                    BamlValueWithMeta::Int(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => BamlValueWithMeta::Float(a as f64 + b, meta),
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Int(b, _),
                                ) => BamlValueWithMeta::Float(a + (b as f64), meta),
                                (
                                    BamlValueWithMeta::String(a, meta),
                                    BamlValueWithMeta::String(b, _),
                                ) => BamlValueWithMeta::String(format!("{a}{b}"), meta),
                                _ => bail!("unsupported types for += operator"),
                            },
                            AssignOp::SubAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    BamlValueWithMeta::Int(a - b, meta)
                                }
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => BamlValueWithMeta::Float(a - b, meta),
                                (
                                    BamlValueWithMeta::Int(a, meta),
                                    BamlValueWithMeta::Float(b, _),
                                ) => BamlValueWithMeta::Float((a as f64) - b, meta),
                                (
                                    BamlValueWithMeta::Float(a, meta),
                                    BamlValueWithMeta::Int(b, _),
                                ) => BamlValueWithMeta::Float(a - (b as f64), meta),
                                _ => bail!("unsupported types for -= operator"),

View on GitHub (pinned to bd85ce9dee)