pathwaycom/pathway · error · ValueError

Cannot convert Json {self.value} to {type}

Error message

Cannot convert Json {self.value} to {type}

What it means

Pathway's Json wrapper stores a value parsed from JSON; conversion helpers such as as_int()/as_str()/as_dict() call _as_type, which only succeeds when the wrapped Python value is already an instance of the requested type. If the JSON value has a different shape (e.g. you call as_int() on a string or as_dict() on a list), Python's isinstance check fails and this ValueError is raised. The error message embeds the actual value so you can see the mismatch.

Source

Thrown at python/pathway/internals/json.py:245

        ...     data: pw.Json
        ...
        >>> @pw.udf
        ... def extract(data: pw.Json) -> tuple:
        ...     return tuple(data["value"].as_dict().values())
        ...
        >>> table = pw.debug.table_from_rows(schema=InputSchema, rows=[({"value": {"inner": 42}},)])
        >>> result = table.select(result=extract(pw.this.data))
        >>> pw.debug.compute_and_print(result, include_id=False)
        result
        (42,)
        """
        return self._as_type(dict)

    def _as_type(self, type: type[J]) -> Any:
        if isinstance(self.value, type):
            return self.value
        else:
            raise ValueError(f"Cannot convert Json {self.value} to {type}")


JsonValue = (
    int | float | str | bool | list["JsonValue"] | dict[str, "JsonValue"] | None | Json
)

J = TypeVar("J", bound=JsonValue)

Json.NULL = Json(None)

__all__ = ["Json", "JsonValue"]

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Check the actual type first: use isinstance(pw_json.value, dict) or match on the value before calling as_*().
  2. Use the untyped accessor: Json.value gives you the raw Python object; index it with ['key'] / iterate it directly instead of converting.
  3. Normalize the source data (e.g. cast string digits with int(...) in a lambda) before wrapping in Json, or use pw.Json.convert with a converter that handles multiple shapes.
  4. For numbers that may arrive as strings, write json.as_str() then int()/float() conversion in the expression.

Example fix

// before
result = table.select(value=pw.this.data.as_int())  # data is Json("42")

// after
result = table.select(value=pw.this.data.as_int() if isinstance(pw.this.data.value, int) else int(pw.this.data.as_str()))
Defensive patterns

Strategy: type-guard

Validate before calling

from pathway import Json

def json_type_ok(j: Json, target: type) -> bool:
    return isinstance(j.value, target)

Type guard

from pathway import Json
from typing import TypeGuard

def json_is_dict(j: Json) -> TypeGuard[Json]:
    return isinstance(j.value, dict)

def json_is_int(j: Json) -> bool:
    return isinstance(j.value, int) and not isinstance(j.value, bool)

Try / catch

try:
    v = pw_json.as_int()
except ValueError:
    v = int(pw_json.as_str())  # or log and skip the row

Prevention

When it happens

Trigger: Calling Json.as_int(), as_float(), as_str(), as_bool(), as_list() or as_dict() (all delegate to _as_type at json.py:245) on a Json value whose underlying Python type does not match — e.g. pw.Json("42").as_int(), or as_dict() on a JSON array [1,2]. Typically inside table.select(...) after io.www read or json parsing.

Common situations: REST APIs returning numbers as strings, JSON fields whose type varies between records (one has {"a": {...}}, next has {"a": 3}), or using as_list() on a dict / as_dict() on a list after an API contract change.

Related errors


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