{"record":{"id":"5244a6eb91a23e89","repo":"BoundaryML/baml","slug":"instanceof-requires-a-class-name-on-the-right-side-at","errorCode":null,"errorMessage":"instanceof requires a class name on the right side at {:?}","messagePattern":"instanceof requires a class name on the right side at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2155,"sourceCode":"            } => {\n                // Special handling for instanceof: right operand is a type name, not a value\n                if matches!(operator, crate::hir::BinaryOperator::InstanceOf) {\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?,\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 {","sourceCodeStart":2137,"sourceCodeEnd":2173,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2137-L2173","documentation":"For an `instanceof` expression, the interpreter expects the right-hand side to be a bare variable expression (Expr::Var) naming a class. If the RHS is any other expression form (string literal, call, field access, etc.) it throws \"instanceof requires a class name on the right side at {:?}\".","triggerScenarios":"Writing `x instanceof <non-var>` — e.g. `x instanceof \"MyClass\"` (quoted name) or `x instanceof get_class()` — during instanceof handling in evaluate_expr_with_context.","commonSituations":"Quoting the class name out of habit from string-based type checks; computing the class name dynamically; copy-pasting patterns from languages where the RHS can be an arbitrary expression.","solutions":["Use the unquoted class identifier directly: `x instanceof MyClass`","Remove any dynamic expression from the RHS; instanceof does not evaluate computed class names","If you need dynamic type checking, compare the class name extracted from the value against a string yourself","Check syntax — parentheses or property access around the class name are not allowed"],"exampleFix":"// before\nif (v instanceof \"User\") { ... }\n// after\nif (v instanceof User) { ... }","handlingStrategy":"validation","validationCode":"// Validate instanceof RHS is a bare identifier before evaluation\nfunction validInstanceofRhs(exprText: string): boolean {\n  return /^[A-Za-z_][A-Za-z0-9_]*$/.test(exprText.trim());\n}","typeGuard":null,"tryCatchPattern":"try {\n  return evalBaml(instanceofExpr);\n} catch (e) {\n  if (String(e).includes(\"instanceof requires a class name\")) {\n    throw new Error(\"instanceof RHS must be an unquoted class identifier, e.g. x instanceof MyClass\");\n  }\n  throw e;\n}","preventionTips":["Always write the class name unquoted on the RHS of instanceof","Do not compute class names dynamically in instanceof checks","Avoid parentheses or field access around the RHS class name","Run the BAML syntax checker/LSP to catch malformed instanceof expressions early"],"tags":["interpreter","instanceof","syntax"],"backgroundTag":"invalid-argument-format","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"}