BoundaryML/baml · error
instanceof requires a class name on the right side at
Error message
instanceof requires a class name on the right side at {:?} What it means
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 {:?}".
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
Example fix
// before
if (v instanceof "User") { ... }
// after
if (v instanceof User) { ... } Defensive patterns
Strategy: validation
Validate before calling
// Validate instanceof RHS is a bare identifier before evaluation
function validInstanceofRhs(exprText: string): boolean {
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(exprText.trim());
} Try / catch
try {
return evalBaml(instanceofExpr);
} catch (e) {
if (String(e).includes("instanceof requires a class name")) {
throw new Error("instanceof RHS must be an unquoted class identifier, e.g. x instanceof MyClass");
}
throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- instanceof requires a class instance on the left side at
- instanceof requires class operands at
- method name must be an identifier at
- {0:?}
- An element at was a node when it should have been a token.
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/5244a6eb91a23e89.
Report an issue: GitHub.
Appendix: source
Thrown at engine/baml-compiler/src/thir/interpret.rs:2155
} => {
// Special handling for instanceof: right operand is a type name, not a value
if matches!(operator, crate::hir::BinaryOperator::InstanceOf) {
let left_val = expect_value(
evaluate_expr(
left,
scopes,
thir,
run_llm_function,
watch_handler,
function_name,
)
.await?,
)?;
// Extract class name from right side (should be Expr::Var)
let class_name = match right.as_ref() {
Expr::Var(name, _) => name.clone(),
_ => bail!(
"instanceof requires a class name on the right side at {:?}",
meta.0
),
};
// Check if left value is a class instance matching the class name
let result = match left_val {
BamlValueWithMeta::Class(ref left_class, ..) => {
BamlValueWithMeta::Bool(left_class == &class_name, meta.clone())
}
_ => bail!(
"instanceof requires a class instance on the left side at {:?}",
meta.0
),
};
EvalValue::Value(result)
} else {View on GitHub (pinned to bd85ce9dee)