pathwaycom/pathway · error · TypeError

Incompatible types in for a coalesce expression.\nThe types

Error message

Incompatible types in for a coalesce expression.\nThe types are: {dtypes}. You might try casting the expressions to Any type to circumvent this, but this is most probably an error.

What it means

Raised when pw.coalesce is given arguments whose dtypes have no least common ancestor: the interpreter computes types_lca over all arguments with raising=True, and any pair that cannot be unified (e.g. int vs str, DateTime vs float) aborts with a TypeError listing the dtypes. Casting all args to Any would technically circumvent it, but that is flagged as almost certainly masking a bug.

Source

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

            )
        return _wrap(expression, expression._return_type)

    def eval_coalesce(
        self,
        expression: expr.CoalesceExpression,
        state: TypeInterpreterState | None = None,
        **kwargs,
    ) -> expr.CoalesceExpression:
        expression = super().eval_coalesce(expression, state=state, **kwargs)
        dtypes = [arg._dtype for arg in expression._args]
        self._check_for_disallowed_types("pathway.coalesce", *dtypes)
        ret_type = dtypes[0]
        non_optional_arg = False
        for dtype in dtypes:
            try:
                ret_type = dt.types_lca(dtype, ret_type, raising=True)
            except TypeError:
                raise TypeError(
                    "Incompatible types in for a coalesce expression.\n"
                    + f"The types are: {dtypes}. "
                    + "You might try casting the expressions to Any type to circumvent this, "
                    + "but this is most probably an error."
                )
            if not isinstance(dtype, dt.Optional):
                # FIXME: do we want to be more radical and return now?
                # Maybe with a warning that some args are skipped?
                non_optional_arg = True
        if ret_type is dt.ANY and any(dtype is not dt.ANY for dtype in dtypes):
            raise TypeError(
                f"Cannot perform pathway.coalesce on columns of types {[dtype.typehint for dtype in dtypes]}."
            )
        ret_type = dt.unoptionalize(ret_type) if non_optional_arg else ret_type
        return _wrap(expression, ret_type)

    def eval_require(
        self,

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Align types explicitly before coalescing: pw.coalesce(pw.this.a.astype(str), pw.this.b.astype(str))
  2. Fix the input schemas so the same logical column has one dtype across sources
  3. If mixing is intentional and you accept losing type safety, cast all args to Any as the message notes (last resort)
  4. Check for accidental wrong column (same name, wrong table) among the coalesce arguments

Example fix

# before
v = pw.coalesce(pw.this.a, pw.this.b)  # int vs str -> TypeError

# after
v = pw.coalesce(pw.this.a.astype(str), pw.this.b.astype(str))
Defensive patterns

Strategy: type-guard

Validate before calling

import pathway as pw

def coalesce_compatible(dtypes) -> bool:
    try:
        ret = dtypes[0]
        for d in dtypes[1:]:
            ret = pw.typehints.lca(d, ret)
        return ret is not None and not ret.equivalent_to(pw.typehints.Any())
    except TypeError:
        return False

Type guard

def same_dtype_family(a, b) -> bool:
    return a.equivalent_to(b) or (
        {str(a.typehint()), str(b.typehint())} <= {"<class 'int'>", "<class 'float'>"}
    )

Try / catch

try:
    v = pw.coalesce(pw.this.a, pw.this.b)
except TypeError:
    v = pw.coalesce(pw.this.a.astype(str), pw.this.b.astype(str))

Prevention

When it happens

Trigger: pw.coalesce(pw.this.a, pw.this.b) with a:int and b:str; mixing DateTime with str columns as fallbacks; coalescing columns coming from different connectors with divergent inferred types (str vs int for the same logical field).

Common situations: Filling missing values from a fallback column that was parsed with a different schema; CSV ingestion where one file yields str and another int for the same field; joining tables whose columns have mismatched dtypes before coalesce.

Related errors


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