{"record":{"id":"0bd60782c4235d54","repo":"BoundaryML/baml","slug":"condition-not-bool-at","errorCode":null,"errorMessage":"condition not bool at {:?}","messagePattern":"condition not bool at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1970,"sourceCode":"                .await?;\n                scopes.pop();\n                EvalValue::Value(result)\n            }\n            Expr::If(cond, then, else_, meta) => {\n                let cv = expect_value(\n                    evaluate_expr(\n                        cond,\n                        scopes,\n                        thir,\n                        run_llm_function,\n                        watch_handler,\n                        function_name,\n                    )\n                    .await?,\n                )?;\n                let b = match cv {\n                    BamlValueWithMeta::Bool(v, _) => v,\n                    _ => bail!(\"condition not bool at {:?}\", meta.0),\n                };\n                if b {\n                    EvalValue::Value(expect_value(\n                        evaluate_expr(\n                            then,\n                            scopes,\n                            thir,\n                            run_llm_function,\n                            watch_handler,\n                            function_name,\n                        )\n                        .await?,\n                    )?)\n                } else if let Some(e) = else_ {\n                    EvalValue::Value(expect_value(\n                        evaluate_expr(\n                            e,\n                            scopes,","sourceCodeStart":1952,"sourceCodeEnd":1988,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1952-L1988","documentation":"In an if-expression, the interpreter requires the evaluated condition to be a BamlValueWithMeta::Bool. Any other type (string, int, null, etc.) throws \"condition not bool at {:?}\". Unlike some languages, BAML's interpreter does not truthiness-coerce values in conditions.","triggerScenarios":"An `if` condition that evaluates to a non-boolean value — e.g. `if 1 {}`, `if \"x\" {}`, or a function call returning a non-bool used directly as a condition.","commonSituations":"Porting code from truthy-language conventions (JS/Python) into BAML; a helper function whose return type changed from bool to something else; comparing values without an explicit `==` operator.","solutions":["Make the condition an explicit boolean expression, e.g. `if x == 1` or `if x != null`","Ensure any function used as a condition actually returns a bool","Wrap string/int checks in explicit comparisons (`s == \"\"`, `n > 0`) instead of relying on truthiness","Annotate/check the condition's type at authoring time so the checker catches it before runtime"],"exampleFix":"// before\nif count { ... } // condition not bool\n// after\nif count > 0 { ... }","handlingStrategy":"type-guard","validationCode":"// Evaluate the condition and assert it is boolean before branching\nfunction assertBoolCondition(v: unknown): boolean {\n  if (typeof v !== \"boolean\") throw new Error(`Condition must be bool, got ${typeof v}`);\n  return v;\n}","typeGuard":"function isBool(v: unknown): v is boolean {\n  return typeof v === \"boolean\";\n}","tryCatchPattern":"try {\n  return evalBaml(ifExpr);\n} catch (e) {\n  if (String(e).includes(\"condition not bool\")) {\n    throw new Error(\"If-condition evaluated to a non-boolean — add an explicit comparison\");\n  }\n  throw e;\n}","preventionTips":["Always write explicit comparisons (`==`, `!=`, `>`) in conditions, never bare values","Ensure helper functions used as conditions have bool return types","Do not port truthiness idioms from JS/Python into BAML","Let the type checker validate condition types at authoring time"],"tags":["interpreter","type-mismatch","condition"],"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"}