BoundaryML/baml · error

condition not bool at

Error message

condition not bool at {:?}

What it means

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.

Solutions

  1. Make the condition an explicit boolean expression, e.g. `if x == 1` or `if x != null`
  2. Ensure any function used as a condition actually returns a bool
  3. Wrap string/int checks in explicit comparisons (`s == ""`, `n > 0`) instead of relying on truthiness
  4. Annotate/check the condition's type at authoring time so the checker catches it before runtime

Example fix

// before
if count { ... } // condition not bool
// after
if count > 0 { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

// Evaluate the condition and assert it is boolean before branching
function assertBoolCondition(v: unknown): boolean {
  if (typeof v !== "boolean") throw new Error(`Condition must be bool, got ${typeof v}`);
  return v;
}

Type guard

function isBool(v: unknown): v is boolean {
  return typeof v === "boolean";
}

Try / catch

try {
  return evalBaml(ifExpr);
} catch (e) {
  if (String(e).includes("condition not bool")) {
    throw new Error("If-condition evaluated to a non-boolean — add an explicit comparison");
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/0bd60782c4235d54. Report an issue: GitHub.

Appendix: source

Thrown at engine/baml-compiler/src/thir/interpret.rs:1970

                .await?;
                scopes.pop();
                EvalValue::Value(result)
            }
            Expr::If(cond, then, else_, meta) => {
                let cv = expect_value(
                    evaluate_expr(
                        cond,
                        scopes,
                        thir,
                        run_llm_function,
                        watch_handler,
                        function_name,
                    )
                    .await?,
                )?;
                let b = match cv {
                    BamlValueWithMeta::Bool(v, _) => v,
                    _ => bail!("condition not bool at {:?}", meta.0),
                };
                if b {
                    EvalValue::Value(expect_value(
                        evaluate_expr(
                            then,
                            scopes,
                            thir,
                            run_llm_function,
                            watch_handler,
                            function_name,
                        )
                        .await?,
                    )?)
                } else if let Some(e) = else_ {
                    EvalValue::Value(expect_value(
                        evaluate_expr(
                            e,
                            scopes,

View on GitHub (pinned to bd85ce9dee)