{"record":{"id":"93a3be9d05c8d3bc","repo":"BoundaryML/baml","slug":"while-condition-must-be-boolean","errorCode":null,"errorMessage":"while condition must be boolean","messagePattern":"while condition must be boolean","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":765,"sourceCode":"                                    scopes,\n                                    thir,\n                                    run_llm_function,\n                                    watch_handler,\n                                    function_name,\n                                )\n                                .await?\n                                {\n                                    ControlFlow::Break => break,\n                                    ControlFlow::Continue => continue,\n                                    ControlFlow::Normal(_) => {}\n                                    ControlFlow::Return(val) => {\n                                        scopes.pop();\n                                        return Ok(Some(ControlFlow::Return(val)));\n                                    }\n                                }\n                            }\n                            BamlValueWithMeta::Bool(false, _) => break,\n                            _ => bail!(\"while condition must be boolean\"),\n                        }\n                    },\n                    Statement::ForLoop {\n                        identifier,\n                        iterator,\n                        block,\n                        ..\n                    } => {\n                        let iterable_val = expect_value(\n                            evaluate_expr(\n                                iterator,\n                                scopes,\n                                thir,\n                                run_llm_function,\n                                watch_handler,\n                                function_name,\n                            )\n                            .await?,","sourceCodeStart":747,"sourceCodeEnd":783,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L747-L783","documentation":"A while loop's condition expression must evaluate to a boolean on every iteration. The interpreter matches the evaluated condition against Bool(true) to continue and Bool(false) to break; any other value type is rejected with this error rather than being truthiness-coerced.","triggerScenarios":"A while condition expression that evaluates to a non-boolean value — e.g. while count, while list, while some_string, or a condition whose branches can return null/int on some iteration (often the first or a later iteration, not necessarily the first).","commonSituations":"Porting truthy-conditional loops from Python/JS into BAML; conditions returning optional values that become null mid-loop; forgetting a comparison operator in the condition.","solutions":["Wrap the condition in an explicit boolean comparison, e.g. while count > 0 instead of while count","Check each iteration's value path (including early returns and null branches) so the condition always yields a bool","If the condition involves a function call, ensure that function returns a boolean on all paths"],"exampleFix":"// before\nwhile remaining {\n  ...\n}\n// after\nwhile remaining > 0 {\n  ...\n}","handlingStrategy":"type-guard","validationCode":"fn validate_while_condition(v: &BamlValue) -> Result<(), String> {\n    match v { BamlValue::Bool(_) => Ok(()), other => Err(format!(\"while condition must be bool, got {:?}\", other)) }\n}","typeGuard":"fn is_boolean(v: &BamlValue) -> bool { matches!(v, BamlValue::Bool(_)) }","tryCatchPattern":null,"preventionTips":["Always write while conditions as explicit boolean comparisons (>, <, ==, !=)","Never rely on truthiness of ints, strings, lists, or nulls in loop conditions","Ensure condition-evaluating functions return bool on all code paths"],"tags":["interpreter","while-loop","type-mismatch","boolean","control-flow"],"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"}