BoundaryML/baml · error
assert condition must be boolean
Error message
assert condition must be boolean
What it means
Raised when a BAML `assert` statement's condition does not evaluate to a boolean at all. The interpreter matches the evaluated condition value: Bool(true) passes, Bool(false) gives "assertion failed", and any other value (Int, String, null, array, etc.) triggers this error, since assert requires a Bool condition.
Solutions
- Make the assert condition explicitly boolean: `assert x != 0;` instead of `assert x;`.
- Check the return type of any function called in the condition and wrap with a comparison.
- Add a type annotation or type check on the variable used in the condition.
- Fix null-producing expressions so the condition can't be null.
Example fix
// before (BAML) assert count; // after assert count > 0;
Defensive patterns
Strategy: validation
Validate before calling
// Ensure the assert condition is a boolean expression before running the BAML block:
if (typeof condValue !== "boolean") {
throw new Error("assert condition must be boolean; use a comparison like `x > 0` instead of bare `x`");
} Type guard
function isBool(v: unknown): v is boolean { return typeof v === "boolean"; } Prevention
- Always write explicit comparisons in assert conditions (`x != 0`, `s != ""`).
- Never rely on truthiness of ints/strings/nulls in BAML asserts.
- Check the declared return type of functions used in assert conditions.
- Handle nullable values before asserting on them.
When it happens
Trigger: An assert whose expression yields a non-boolean value, e.g. `assert x;` where x is an Int, or `assert some_fn(...)` returning a string; also cases where implicit truthiness conversion is not performed by the interpreter.
Common situations: Porting Python/JS truthiness habits (`assert value` on an int or nullable) into BAML, which requires explicit boolean conditions; comparing with a function whose return type drifted to non-bool.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- array assignment on non-list value at
- assertion failed
- baml.fetch_as: expected baml.fetch_as arg to be a string or…
- baml.fetch_as: expected header value to be a string, got
- baml.fetch_as: expected type parameter to be a Baml type…
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/f54d411efba9ae6b.
Report an issue: GitHub.
Appendix: source
Thrown at engine/baml-compiler/src/thir/interpret.rs:1287
}
}
}
Statement::Assert { condition, .. } => {
let cond_val = expect_value(
evaluate_expr(
condition,
scopes,
thir,
run_llm_function,
watch_handler,
function_name,
)
.await?,
)?;
match cond_val {
BamlValueWithMeta::Bool(true, _) => {}
BamlValueWithMeta::Bool(false, _) => bail!("assertion failed"),
_ => bail!("assert condition must be boolean"),
}
}
Statement::WatchOptions {
variable,
channel,
when,
span,
} => {
// Find and update the watch variable for this variable
// We need to find the watch variable by checking which one references the same value
for scope in scopes.iter_mut().rev() {
if let Some(var_ref) = scope.variables.get(variable) {
// Find the watch variable that references this variable
if let Some(watch_var) = scope
.watch_variables
.iter_mut()
.find(|wv| Arc::ptr_eq(&wv.value_ref, var_ref))
{View on GitHub (pinned to bd85ce9dee)