pathwaycom/pathway · error · TypeError

First argument of pathway.if_else has to be bool, found {if_

Error message

First argument of pathway.if_else has to be bool, found {if_dtype.typehint}.

What it means

Raised when the first argument of pw.if_else is not of dtype bool. if_else(condition, then, else) requires a strict bool condition; passing an int, str, Optional[bool], or Any column as the condition fails immediately with the found typehint.

Source

Thrown at python/pathway/internals/type_interpreter.py:454

        expression: expr.IsNoneExpression,
        state: TypeInterpreterState | None = None,
        **kwargs,
    ) -> expr.IsNoneExpression:
        ret = super().eval_none(expression, state=state, **kwargs)
        self._check_for_disallowed_types("pathway.is_none", ret._expr._dtype)
        return _wrap(ret, dt.BOOL)

    def eval_ifelse(
        self,
        expression: expr.IfElseExpression,
        state: TypeInterpreterState | None = None,
        **kwargs,
    ) -> expr.IfElseExpression:
        assert state is not None
        if_ = self.eval_expression(expression._if, state=state, **kwargs)
        if_dtype = if_._dtype
        if if_dtype != dt.BOOL:
            raise TypeError(
                f"First argument of pathway.if_else has to be bool, found {if_dtype.typehint}."
            )

        if isinstance(if_, expr.IsNotNoneExpression) and isinstance(
            if_._expr, expr.ColumnReference
        ):
            then_ = self.eval_expression(
                expression._then, state=state.with_new_col([if_._expr]), **kwargs
            )
        else:
            then_ = self.eval_expression(expression._then, state=state, **kwargs)

        if isinstance(if_, expr.IsNoneExpression) and isinstance(
            if_._expr, expr.ColumnReference
        ):
            else_ = self.eval_expression(
                expression._else, state=state.with_new_col([if_._expr], **kwargs)
            )

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Make the condition boolean explicitly: pw.if_else(pw.this.count > 0, a, b) or pw.this.flag == 1
  2. For Optional[bool], handle nulls: pw.if_else(pw.this.flag.fill(True), a, b) or an is_not_none check first
  3. If the condition is Any-typed after casts, re-establish bool with a comparison or .astype(bool)

Example fix

# before
v = pw.if_else(pw.this.count, "many", "few")  # int condition -> TypeError

# after
v = pw.if_else(pw.this.count > 0, "many", "few")
Defensive patterns

Strategy: type-guard

Validate before calling

import pathway as pw

def bool_condition(dtype) -> bool:
    return dtype.equivalent_to(pw.typehints.Bool())

# build conditions as explicit comparisons
cond = pw.this.flag == 1  # not pw.this.flag

Type guard

def is_bool_dtype(dtype) -> bool:
    import pathway as pw
    return dtype.equivalent_to(pw.typehints.Bool())

Try / catch

try:
    v = pw.if_else(cond, a, b)
except TypeError:
    v = pw.if_else(cond == True, a, b)  # force bool via comparison

Prevention

When it happens

Trigger: pw.if_else(pw.this.count, a, b) where count is int; condition column typed as Optional(bool) after is_none-based logic; conditions built from comparisons whose type degraded to Any due to earlier casts; string conditions like pw.if_else("true", ...).

Common situations: Porting Python truthiness (where non-zero/ non-empty strings are truthy) to Pathway; conditions from connectors inferred as int (0/1 flags) instead of bool; conditions that are Optional because the source can be null.

Related errors


AI-assisted analysis of pathwaycom/pathway@fa2f74a464 (2026-08-15). Data as JSON: /api/errors/7b2daef96b04981d. Report an issue: GitHub.