BoundaryML/baml · error
C-style for loop condition must be boolean
Error message
C-style for loop condition must be boolean
What it means
Raised by the BAML interpreter while executing a C-style `for (init; cond; after)` loop: after evaluating the condition, the result must be Bool(true) to continue or Bool(false) to break. Any other value type (Int, String, null, etc.) makes the match fall through to this bail. BAML does not truthiness-coerce loop conditions.
Source
Thrown at engine/baml-compiler/src/thir/interpret.rs:1064
} => {
loop {
// Check condition (if present)
if let Some(cond_expr) = condition {
let cond_val = expect_value(
evaluate_expr(
cond_expr,
scopes,
thir,
run_llm_function,
watch_handler,
function_name,
)
.await?,
)?;
match cond_val {
BamlValueWithMeta::Bool(false, _) => break,
BamlValueWithMeta::Bool(true, _) => {}
_ => bail!("C-style for loop condition must be boolean"),
}
}
// Execute loop body
match evaluate_block_with_control_flow(
block,
scopes,
thir,
run_llm_function,
watch_handler,
function_name,
)
.await?
{
ControlFlow::Break => break,
ControlFlow::Continue => {
// Execute after statement if present
if let Some(after_stmt) = after {View on GitHub (pinned to bd85ce9dee)
Solutions
- Make the condition a real boolean expression, e.g. `i > 0` instead of `i`.
- Fix any helper function used in the condition to return Bool.
- Handle null-producing conditions explicitly (`cond != null && cond)` style).
Example fix
// before (BAML)
for (let i = 10; i; i -= 1) { ... } // error: condition must be boolean
// after
for (let i = 10; i > 0; i -= 1) { ... } Defensive patterns
Strategy: type-guard
Validate before calling
// ensure a loop condition is boolean before running the C-for
function assertBoolCondition(cond: unknown): void {
if (typeof cond !== "boolean") {
throw new Error("C-style for loop condition must be a Bool expression");
}
} Type guard
const isBool = (v: unknown): v is boolean => typeof v === "boolean";
Try / catch
try {
await runBaml(program);
} catch (e) {
if (String(e).includes("C-style for loop condition must be boolean")) {
// rewrite the condition as an explicit comparison (e.g. `i > 0`) and retry
} else { throw e; }
} Prevention
- Always write C-for conditions as explicit comparisons (`i < n`), never bare values.
- Don't port truthiness idioms (`while i`) from C/JS/Python into BAML.
- Verify any helper called in the condition returns Bool.
- Add a lint/test that flags non-comparison C-for conditions.
When it happens
Trigger: A C-for loop whose condition expression evaluates to a non-Bool, e.g. `for (let i = 0; i; i += 1)` (Int condition) or a condition returning a string/null from a function call.
Common situations: Porting C/JS/Python loops that rely on numeric truthiness (`while i`); calling a helper in the condition that returns Int/String instead of bool; using a nullable comparison result.
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
- unsupported types for += in C-for after clause
- unsupported types for %= operator
- bitwise ^= requires integer operands
- bitwise &= requires integer operands
- bitwise |= requires integer operands
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/c024f0c9b79ae743.
Report an issue: GitHub.