pathwaycom/pathway · error · ValueError

Table.with_schema() argument has to have the same column nam

Error message

Table.with_schema() argument has to have the same column names as in the table.

What it means

Raised by Table._with_schema() (backing with_schema) when the provided schema class's column-name set differs from the table's current column names. with_schema forces column properties (types, defaults) onto existing columns; it cannot add, remove, or reorder columns.

Source

Thrown at python/pathway/internals/table.py:2217

        for col in columns:
            if isinstance(col, expr.ColumnReference):
                new_columns.pop(col.name)
            else:
                assert isinstance(col, str)
                new_columns.pop(col)
        columns_wrapped = {
            name: self._wrap_column_in_context(self._rowwise_context, column, name)
            for name, column in new_columns.items()
        }
        return self._with_same_universe(*columns_wrapped.items())

    @trace_user_frame
    @contextualized_operator
    @check_arg_types
    def _with_schema(self, schema: type[Schema]) -> Table:
        """Returns updated table with a forced schema on it."""
        if schema.keys() != self.schema.keys():
            raise ValueError(
                "Table.with_schema() argument has to have the same column names as in the table."
            )
        context = clmn.SetSchemaContext(
            _id_column=self._id_column,
            _new_properties={
                self[name]._to_internal(): schema.column_properties(name)
                for name in self.column_names()
            },
            _id_column_props=schema.__universe_properties__,
        )
        return self._table_with_context(context)

    @trace_user_frame
    @check_arg_types
    def update_types(self, **kwargs: Any) -> Table:
        """Updates types in schema. Has no effect on the runtime."""

        for name in kwargs.keys():

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Align the schema class so its field names exactly match t.column_names()
  2. Or adjust the table first: select/rename/with_columns so names match the schema
  3. Regenerate the schema definition from the actual table if the source of truth drifted
  4. Prefer building the table with the schema at ingestion time (input with schema=MySchema) instead of patching afterwards

Example fix

# before
class NewSchema(pw.Schema):
    a: int
t2 = t.with_schema(NewSchema)  # t also has column 'b' -> ValueError

# after
class NewSchema(pw.Schema):
    a: int
    b: str
t2 = t.with_schema(NewSchema)
Defensive patterns

Strategy: validation

Validate before calling

def with_schema_safe(t, schema):
    assert schema.keys() == t.schema.keys(), (
        f'{set(schema.keys())} != {set(t.schema.keys())}'
    )
    return t.with_schema(schema)

Try / catch

try:
    t2 = t.with_schema(S)
except ValueError as e:
    if 'same column names' in str(e):
        raise ValueError(f'{t.column_names()} vs {list(S.keys())}') from e

Prevention

When it happens

Trigger: t.with_schema(MySchema) where MySchema's fields are not exactly t.schema.keys(); e.g. MySchema misses a column or adds one, or column was renamed before with_schema.

Common situations: Schema class out of sync with the connector's output (connector version added/removed a field); applying a schema meant for a different table; renaming columns then applying the original schema.

Related errors


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