{"record":{"id":"ba91c0e0e3b78dfa","repo":"pathwaycom/pathway","slug":"incompatible-types-in-for-a-coalesce-expression-n","errorCode":null,"errorMessage":"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.","messagePattern":"Incompatible types in for a coalesce expression\\.\\\\nThe types are: (.+?)\\. You might try casting the expressions to Any type to circumvent this, but this is most probably an error\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/type_interpreter.py","lineNumber":387,"sourceCode":"            )\n        return _wrap(expression, expression._return_type)\n\n    def eval_coalesce(\n        self,\n        expression: expr.CoalesceExpression,\n        state: TypeInterpreterState | None = None,\n        **kwargs,\n    ) -> expr.CoalesceExpression:\n        expression = super().eval_coalesce(expression, state=state, **kwargs)\n        dtypes = [arg._dtype for arg in expression._args]\n        self._check_for_disallowed_types(\"pathway.coalesce\", *dtypes)\n        ret_type = dtypes[0]\n        non_optional_arg = False\n        for dtype in dtypes:\n            try:\n                ret_type = dt.types_lca(dtype, ret_type, raising=True)\n            except TypeError:\n                raise TypeError(\n                    \"Incompatible types in for a coalesce expression.\\n\"\n                    + f\"The types are: {dtypes}. \"\n                    + \"You might try casting the expressions to Any type to circumvent this, \"\n                    + \"but this is most probably an error.\"\n                )\n            if not isinstance(dtype, dt.Optional):\n                # FIXME: do we want to be more radical and return now?\n                # Maybe with a warning that some args are skipped?\n                non_optional_arg = True\n        if ret_type is dt.ANY and any(dtype is not dt.ANY for dtype in dtypes):\n            raise TypeError(\n                f\"Cannot perform pathway.coalesce on columns of types {[dtype.typehint for dtype in dtypes]}.\"\n            )\n        ret_type = dt.unoptionalize(ret_type) if non_optional_arg else ret_type\n        return _wrap(expression, ret_type)\n\n    def eval_require(\n        self,","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/type_interpreter.py#L369-L405","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Align types explicitly before coalescing: pw.coalesce(pw.this.a.astype(str), pw.this.b.astype(str))","Fix the input schemas so the same logical column has one dtype across sources","If mixing is intentional and you accept losing type safety, cast all args to Any as the message notes (last resort)","Check for accidental wrong column (same name, wrong table) among the coalesce arguments"],"exampleFix":"# before\nv = pw.coalesce(pw.this.a, pw.this.b)  # int vs str -> TypeError\n\n# after\nv = pw.coalesce(pw.this.a.astype(str), pw.this.b.astype(str))","handlingStrategy":"type-guard","validationCode":"import pathway as pw\n\ndef coalesce_compatible(dtypes) -> bool:\n    try:\n        ret = dtypes[0]\n        for d in dtypes[1:]:\n            ret = pw.typehints.lca(d, ret)\n        return ret is not None and not ret.equivalent_to(pw.typehints.Any())\n    except TypeError:\n        return False","typeGuard":"def same_dtype_family(a, b) -> bool:\n    return a.equivalent_to(b) or (\n        {str(a.typehint()), str(b.typehint())} <= {\"<class 'int'>\", \"<class 'float'>\"}\n    )","tryCatchPattern":"try:\n    v = pw.coalesce(pw.this.a, pw.this.b)\nexcept TypeError:\n    v = pw.coalesce(pw.this.a.astype(str), pw.this.b.astype(str))","preventionTips":["Align dtypes across sources in schemas before coalescing","Cast all coalesce args to one target type explicitly","Treat coalesce type errors as schema drift signals, not noise"],"tags":["pathway","type-checking","coalesce","dtype","typeerror"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}