{"record":{"id":"d203026007b05954","repo":"BoundaryML/baml","slug":"operator-requires-numeric-operand-at","errorCode":null,"errorMessage":"- operator requires numeric operand at {:?}","messagePattern":"- operator requires numeric operand at (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2551,"sourceCode":"        },\n    })\n}\n\nfn evaluate_unary_op(\n    operator: &crate::hir::UnaryOperator,\n    val: &BamlValueWithMeta<ExprMetadata>,\n    meta: &ExprMetadata,\n) -> Result<BamlValueWithMeta<ExprMetadata>> {\n    use crate::hir::UnaryOperator;\n    Ok(match operator {\n        UnaryOperator::Not => match val.clone() {\n            BamlValueWithMeta::Bool(b, _) => BamlValueWithMeta::Bool(!b, meta.clone()),\n            _ => bail!(\"! operator requires boolean operand at {:?}\", meta.0),\n        },\n        UnaryOperator::Neg => match val.clone() {\n            BamlValueWithMeta::Int(i, _) => BamlValueWithMeta::Int(-i, meta.clone()),\n            BamlValueWithMeta::Float(f, _) => BamlValueWithMeta::Float(-f, meta.clone()),\n            _ => bail!(\"- operator requires numeric operand at {:?}\", meta.0),\n        },\n    })\n}\n\nfn values_equal(\n    left: &BamlValueWithMeta<ExprMetadata>,\n    right: &BamlValueWithMeta<ExprMetadata>,\n) -> bool {\n    match (left, right) {\n        (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => a == b,\n        (BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Float(b, _)) => a == b,\n        (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Float(b, _)) => *a as f64 == *b,\n        (BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Int(b, _)) => *a == *b as f64,\n        (BamlValueWithMeta::String(a, _), BamlValueWithMeta::String(b, _)) => a == b,\n        (BamlValueWithMeta::Null(_), BamlValueWithMeta::Null(_)) => true,\n        _ => false,\n    }\n}","sourceCodeStart":2533,"sourceCodeEnd":2569,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2533-L2569","documentation":"Thrown when the unary minus `-` operator is applied to a value that is neither an Int nor a Float at runtime. The interpreter implements Neg only for numeric BamlValue variants; strings, bools, nulls, and collections hit the bail. There is no implicit numeric coercion in BAML expressions.","triggerScenarios":"Evaluating `-x` where `x` is a string (e.g. a numeric string from JSON or user input), a bool, null, or a media/class value. Frequently when a number arrives as a string via JSON deserialization into a String-typed field.","commonSituations":"Negating a value parsed from text/json where the declared type is string, applying negation to a config value read as string, or a typo where the operand was reassigned to a non-numeric type.","solutions":["Ensure the operand is numeric; convert string numbers at the source (parse to int/float when deserializing)","Annotate the variable as int or float so the typechecker rejects non-numeric values","Guard with a null/type check before negating optional values","If the string holds a number, change the field's declared type from string to int/float in the schema"],"exampleFix":"// before (BAML)\nlet offset = -threshold;  // threshold: string\n// after\nlet offset = -(threshold as int);  // or declare threshold as int","handlingStrategy":"type-guard","validationCode":"// ensure numeric operand before unary minus\nif (typeof v !== 'number') {\n  throw new Error(`- requires numeric operand, got ${typeof v}`);\n}","typeGuard":"const isNumber = (v: unknown): v is number => typeof v === 'number' && !Number.isNaN(v);","tryCatchPattern":"try {\n  result = evaluate(expr);\n} catch (e) {\n  if (String(e).includes('- operator requires numeric operand')) {\n    // parse string to number at the source\n  }\n  throw e;\n}","preventionTips":["Type numeric config fields as int/float, not string","Parse JSON numbers into numeric-typed fields","Check for null/undefined before negation"],"tags":["baml","interpreter","unary-operator","numeric"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}