{"record":{"id":"044ca1d1d26767ed","repo":"BoundaryML/baml","slug":"unsupported-types-for-operator-044ca1","errorCode":null,"errorMessage":"unsupported types for %= operator","messagePattern":"unsupported types for %= operator","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":964,"sourceCode":"                                (\n                                    BamlValueWithMeta::Float(a, meta),\n                                    BamlValueWithMeta::Int(b, _),\n                                ) => {\n                                    if b == 0 {\n                                        bail!(\"division by zero in /= operator\");\n                                    }\n                                    BamlValueWithMeta::Float(a / (b as f64), meta)\n                                }\n                                _ => bail!(\"unsupported types for /= operator\"),\n                            },\n                            AssignOp::ModAssign => match (current_val.clone(), rhs_val.clone()) {\n                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {\n                                    if b == 0 {\n                                        bail!(\"modulo by zero in %= operator\");\n                                    }\n                                    BamlValueWithMeta::Int(a % b, meta)\n                                }\n                                _ => bail!(\"unsupported types for %= operator\"),\n                            },\n                            AssignOp::BitXorAssign => {\n                                match (current_val.clone(), rhs_val.clone()) {\n                                    (\n                                        BamlValueWithMeta::Int(a, meta),\n                                        BamlValueWithMeta::Int(b, _),\n                                    ) => BamlValueWithMeta::Int(a ^ b, meta),\n                                    _ => bail!(\"bitwise ^= requires integer operands\"),\n                                }\n                            }\n                            AssignOp::BitAndAssign => {\n                                match (current_val.clone(), rhs_val.clone()) {\n                                    (\n                                        BamlValueWithMeta::Int(a, meta),\n                                        BamlValueWithMeta::Int(b, _),\n                                    ) => BamlValueWithMeta::Int(a & b, meta),\n                                    _ => bail!(\"bitwise &= requires integer operands\"),\n                                }","sourceCodeStart":946,"sourceCodeEnd":982,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L946-L982","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check both operands of `%=` are BAML Int values; cast/coerce floats to int first (e.g. use an int division/truncation helper before `%=`).","Verify the variable's declared/inferred type in the BAML schema matches Int and that LLM output is parsed as an int.","If you need float modulo, implement it manually with an explicit expression (e.g. `a - (a/b).floor()*b` pattern) instead of `%=`.","Confirm the value isn't wrapped in a meta/optional variant the interpreter sees as non-Int."],"exampleFix":"// before (BAML)\nlet ratio = 10.0;\nratio %= 3; // error: unsupported types for %= operator\n\n// after\nlet ratio = 10;\nratio %= 3; // Int operands only","handlingStrategy":"type-guard","validationCode":"// caller-side check before running the %= statement\nfunction assertIntsForModulo(a: unknown, b: unknown): void {\n  if (!Number.isInteger(a) || !Number.isInteger(b)) {\n    throw new Error(`%= requires Int operands, got ${typeof a} and ${typeof b}`);\n  }\n  if (b === 0) throw new Error(\"modulo by zero in %= operator\");\n}","typeGuard":"const isBamlInt = (v: unknown): v is number => typeof v === \"number\" && Number.isInteger(v);","tryCatchPattern":"try {\n  await runBaml(program);\n} catch (e) {\n  if (String(e).includes(\"unsupported types for %= operator\")) {\n    // coerce operands to Int and retry, or surface a type error to the author\n  } else { throw e; }\n}","preventionTips":["Declare counters and numeric fields as Int, never Float, when using %=.","Never feed LLM-parsed values directly into %= without validating they are integers.","Prefer explicit manual modulo expressions when floats are involved.","Add unit tests covering every compound-assignment operator with the exact operand types used."],"tags":["baml","interpreter","arithmetic","type-mismatch"],"backgroundTag":"unsupported-operation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}