{"record":{"id":"f54d411efba9ae6b","repo":"BoundaryML/baml","slug":"assert-condition-must-be-boolean","errorCode":null,"errorMessage":"assert condition must be boolean","messagePattern":"assert condition must be boolean","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1287,"sourceCode":"                            }\n                        }\n                    }\n                    Statement::Assert { condition, .. } => {\n                        let cond_val = expect_value(\n                            evaluate_expr(\n                                condition,\n                                scopes,\n                                thir,\n                                run_llm_function,\n                                watch_handler,\n                                function_name,\n                            )\n                            .await?,\n                        )?;\n                        match cond_val {\n                            BamlValueWithMeta::Bool(true, _) => {}\n                            BamlValueWithMeta::Bool(false, _) => bail!(\"assertion failed\"),\n                            _ => bail!(\"assert condition must be boolean\"),\n                        }\n                    }\n                    Statement::WatchOptions {\n                        variable,\n                        channel,\n                        when,\n                        span,\n                    } => {\n                        // Find and update the watch variable for this variable\n                        // We need to find the watch variable by checking which one references the same value\n                        for scope in scopes.iter_mut().rev() {\n                            if let Some(var_ref) = scope.variables.get(variable) {\n                                // Find the watch variable that references this variable\n                                if let Some(watch_var) = scope\n                                    .watch_variables\n                                    .iter_mut()\n                                    .find(|wv| Arc::ptr_eq(&wv.value_ref, var_ref))\n                                {","sourceCodeStart":1269,"sourceCodeEnd":1305,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1269-L1305","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (BAML)\nassert count;\n// after\nassert count > 0;","handlingStrategy":"validation","validationCode":"// Ensure the assert condition is a boolean expression before running the BAML block:\nif (typeof condValue !== \"boolean\") {\n  throw new Error(\"assert condition must be boolean; use a comparison like `x > 0` instead of bare `x`\");\n}","typeGuard":"function isBool(v: unknown): v is boolean { return typeof v === \"boolean\"; }","tryCatchPattern":null,"preventionTips":["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."],"tags":["baml","assertion","type-mismatch"],"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"}