{"record":{"id":"fd7a34039ff53e6c","repo":"BoundaryML/baml","slug":"instanceof-requires-a-class-instance-on-the-left-side-at","errorCode":null,"errorMessage":"instanceof requires a class instance on the left side at {:?}","messagePattern":"instanceof requires a class instance on the left side at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2166,"sourceCode":"                        )\n                        .await?,\n                    )?;\n\n                    // Extract class name from right side (should be Expr::Var)\n                    let class_name = match right.as_ref() {\n                        Expr::Var(name, _) => name.clone(),\n                        _ => bail!(\n                            \"instanceof requires a class name on the right side at {:?}\",\n                            meta.0\n                        ),\n                    };\n\n                    // Check if left value is a class instance matching the class name\n                    let result = match left_val {\n                        BamlValueWithMeta::Class(ref left_class, ..) => {\n                            BamlValueWithMeta::Bool(left_class == &class_name, meta.clone())\n                        }\n                        _ => bail!(\n                            \"instanceof requires a class instance on the left side at {:?}\",\n                            meta.0\n                        ),\n                    };\n\n                    EvalValue::Value(result)\n                } else {\n                    // Normal binary operation: evaluate both sides\n                    let left_val = expect_value(\n                        evaluate_expr(\n                            left,\n                            scopes,\n                            thir,\n                            run_llm_function,\n                            watch_handler,\n                            function_name,\n                        )\n                        .await?,","sourceCodeStart":2148,"sourceCodeEnd":2184,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2148-L2184","documentation":"The BAML interpreter's `instanceof` operator only works when the left-hand operand evaluates to a class instance (a Class value). This error is thrown when the left side is any other value type (int, string, list, null, etc.). It is a runtime type check inside expression evaluation.","triggerScenarios":"Evaluating an `x instanceof ClassName` expression where `x` holds a non-class value: a primitive, a list, a map, null, or an unevaluated variable holding those.","commonSituations":"Checking a value that came from an LLM response or a function return that the developer assumed was a class instance but is actually null (e.g. failed/partial parse) or a raw primitive; typo'd variable shadowing so the left side is a scalar instead of the constructed object.","solutions":["Ensure the left operand is a value constructed as a class instance (e.g. via a class constructor) before using instanceof","Check for null/undefined values first; instanceof on a possibly-null value throws","Use a different type check (equality, field access) for primitives instead of instanceof","Inspect the expression metadata position printed in the message to find the offending operand"],"exampleFix":"// before\nif (maybeUser instanceof User) { ... }   // maybeUser is null or a string\n// after\nif (maybeUser != null && maybeUser instanceof User) { ... }","handlingStrategy":"type-guard","validationCode":"// before: x instanceof ClassName\n// guard that x is a class instance at all\nif (x == null) { throw new Error(\"left side of instanceof is null\"); }","typeGuard":"function isClassInstance(v) { return v != null && typeof v === 'object' && v.__baml_class != null; }","tryCatchPattern":"try { evalExpr(expr); } catch (e) { if (String(e).includes('instanceof requires a class instance')) { /* treat as non-instance */ } else { throw e; } }","preventionTips":["Only use instanceof on values produced by class constructors","Null-check operands before instanceof","Avoid instanceof on values returned directly from LLM output without validation"],"tags":["runtime","type-check","interpreter","instanceof"],"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"}