{"record":{"id":"a587c600ae3333da","repo":"BoundaryML/baml","slug":"instanceof-requires-class-operands-at","errorCode":null,"errorMessage":"instanceof requires class operands at {:?}","messagePattern":"instanceof requires class operands at (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2532,"sourceCode":"                }\n                BamlValueWithMeta::Int(a << b, meta.clone())\n            }\n            _ => bail!(\"shift << requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::Shr => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                if b < 0 {\n                    bail!(\"negative shift amount at {:?}\", meta.0);\n                }\n                BamlValueWithMeta::Int(a >> b, meta.clone())\n            }\n            _ => bail!(\"shift >> requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::InstanceOf => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Class(class, ..), BamlValueWithMeta::Class(right_class, ..)) => {\n                BamlValueWithMeta::Bool(class == right_class, meta.clone())\n            }\n            _ => bail!(\"instanceof requires class operands at {:?}\", meta.0),\n        },\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()),","sourceCodeStart":2514,"sourceCodeEnd":2550,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2514-L2550","documentation":"Thrown when BAML's `instanceof` binary operator is evaluated with operands that are not both Class values. instanceof is only defined for checking whether a value's class matches another class; primitives, lists, maps, media, etc. are unsupported and hit the bail. The interpreter deliberately restricts instanceof to class identity comparison.","triggerScenarios":"Writing `x instanceof Y` in BAML where `x` evaluates to a non-class value (int, string, list, enum, media) or where the right-hand side is not a class reference. Also fires when a value expected to be a class instance is actually a primitive (e.g. deserialized from JSON).","commonSituations":"Using instanceof for type checks on primitives or enums (which are not classes in BAML), testing values returned from function calls whose static type was unknown, or mistaking a class name for an instance on either side.","solutions":["Ensure both sides of instanceof are class values; use equality (==) for primitives/enums instead","Check the actual runtime type of the left operand before applying instanceof","If checking a subtype relationship, verify the class is instantiated (Class value) rather than holding a raw payload","Refactor to use a discriminating field or enum match instead of instanceof for non-class types"],"exampleFix":"// before (BAML)\nif (result instanceof MyResult) { ... }  // result is a string\n// after\nif (result == \"expected\") { ... }  // or ensure result is constructed as a MyResult class instance","handlingStrategy":"type-guard","validationCode":"// ensure both operands are class instances before instanceof\nif (typeof x !== 'object' || x === null || typeof Y !== 'function') {\n  throw new Error('instanceof needs class operands');\n}","typeGuard":"const isClassValue = (v: unknown): boolean => v !== null && typeof v === 'object' && v.__kind === 'class';","tryCatchPattern":"try {\n  result = evaluate(expr);\n} catch (e) {\n  if (String(e).includes('instanceof requires class operands')) {\n    // switch to equality check or fix operand construction\n  }\n  throw e;\n}","preventionTips":["Only use instanceof on values constructed as BAML classes","Use == for primitives and enums","Verify runtime type of loosely-typed function results before instanceof"],"tags":["baml","interpreter","instanceof","type-mismatch"],"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"}