pathwaycom/pathway · error · TypeError

Default must be of type {Json | None}, found {default_dtype.

Error message

Default must be of type {Json | None}, found {default_dtype.typehint}.

What it means

Raised by the type interpreter when a get() (indexing) operation on a Json column is given a default whose dtype is not a subtype of Json | None. On JSON objects, json_col.get(key, default=...) requires the default to be Json-typed (a JSON value) or None; passing an int/str/bool literal default fails because the JSON accessor's default slot must be Json.

Source

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

        dtypes = tuple(arg._dtype for arg in expression._args)
        self._check_for_disallowed_types("pathway.make_tuple", *dtypes)
        return _wrap(expression, dt.Tuple(*dtypes))

    def eval_get(
        self,
        expression: expr.GetExpression,
        state: TypeInterpreterState | None = None,
        **kwargs,
    ) -> expr.GetExpression:
        expression = super().eval_get(expression, state=state, **kwargs)
        object_dtype = expression._object._dtype
        index_dtype = expression._index._dtype
        default_dtype = expression._default._dtype

        if object_dtype == dt.JSON:
            # json
            if not dt.dtype_issubclass(default_dtype, dt.Optional(dt.JSON)):
                raise TypeError(
                    f"Default must be of type {Json | None}, found {default_dtype.typehint}."
                )
            if not expression._check_if_exists or default_dtype == dt.JSON:
                return _wrap(expression, dt.JSON)
            else:
                return _wrap(expression, dt.Optional(dt.JSON))
        elif object_dtype.equivalent_to(dt.Optional(dt.JSON)):
            # optional json
            raise TypeError(f"Cannot get from {Json | None}.")
        else:
            # sequence
            if (
                not isinstance(object_dtype, (dt.Array, dt.Tuple, dt.List))
                and object_dtype != dt.ANY
            ):
                raise TypeError(
                    f"Object in {expression!r} has to be a JSON or sequence."
                )

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Wrap the default in pw.Json: pw.this.col.get('key', default=pw.Json(0)) so its dtype is Json
  2. Or use None default and post-process: pw.this.col.get('key', optional=True) then fill/unwrap with the desired scalar
  3. Do custom fallback logic in .apply(lambda d: d.get('key', 0)) where plain Python semantics apply

Example fix

# before
v = pw.this.payload.get("count", default=0)  # TypeError: default must be Json | None

# after
v = pw.this.payload.get("count", default=pw.Json(0))
Defensive patterns

Strategy: type-guard

Validate before calling

import pathway as pw

def json_default_ok(default) -> bool:
    return default is None or isinstance(default, pw.Json)

# wrap plain scalars before use
def safe_default(v):
    return v if json_default_ok(v) else pw.Json(v)

Type guard

def is_json_or_none(v) -> bool:
    import pathway as pw
    return v is None or isinstance(v, pw.Json)

Try / catch

try:
    v = pw.this.payload.get("k", default=0)
except TypeError:
    v = pw.this.payload.get("k", default=pw.Json(0))

Prevention

When it happens

Trigger: pw.this.json_col.get('key', default=0) or default="fallback" on a Json column (plain Python scalars are rejected); defaults coming from another non-Json column; get with check_if_exists semantics where users expect Python dict.get behavior with arbitrary defaults.

Common situations: Expecting dict.get(key, 'n/a') semantics with arbitrary defaults; JSON payloads where missing keys should yield a scalar fallback; migrating code that used apply(lambda d: d.get(k, 0)).

Related errors


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