cocoindex-io/cocoindex · error · ValueError

Columns {invalid_cols} not found in row_type fields: {field_

Error message

Columns {invalid_cols} not found in row_type fields: {field_names}

What it means

When row_type is a record type and explicit columns are supplied, every column name must correspond to a field of the record type; otherwise the connector could not map query results onto the record. Mismatched names raise ValueError.

Source

Thrown at python/cocoindex/connectors/postgres/_source.py:223

            raise ValueError("Cannot specify both row_factory and row_type")

        # Determine columns based on row_type
        resolved_columns: Sequence[str] | None = columns
        if row_type is not None:
            if not is_record_type(row_type):
                raise TypeError(
                    f"row_type must be a record type (dataclass, NamedTuple, or Pydantic model), "
                    f"got {row_type}"
                )
            record_info = RecordType(row_type)
            field_names = [f.name for f in record_info.fields]
            field_set = frozenset(field_names)

            if columns is not None:
                # Validate that all specified columns exist in the record type
                invalid_cols = [c for c in columns if c not in field_set]
                if invalid_cols:
                    raise ValueError(
                        f"Columns {invalid_cols} not found in row_type fields: {field_names}"
                    )
            else:
                # Use record type fields as columns
                resolved_columns = field_names

            row_factory = _create_row_factory(row_type, field_set)

        self._pool = pool
        self._spec = PgSourceSpec(
            table_name=table_name,
            columns=resolved_columns,
            pg_schema_name=pg_schema_name,
        )
        self._row_factory = row_factory

    def fetch_rows(self) -> RowFetcher[RowT]:
        """

View on GitHub (pinned to e84aa99b32)

Solutions

  1. Correct the column names so each matches a field of row_type.
  2. Remove the columns argument to auto-derive columns from all record fields.
  3. Rename the record fields to match the database columns.

Example fix

// before
@dataclass
class User:
    id: int
    email: str
src = PostgresSource(table="u", row_type=User, columns=["id", "emial"])
// after
src = PostgresSource(table="u", row_type=User, columns=["id", "email"])
Defensive patterns

Strategy: validation

Validate before calling

import dataclasses
field_names = {f.name for f in dataclasses.fields(MyRow)}
bad = [c for c in columns if c not in field_names]
assert not bad, f"Unknown columns: {bad}"

Type guard

def columns_valid(row_type: type, columns: list[str]) -> bool:
    names = {f.name for f in dataclasses.fields(row_type)}
    return all(c in names for c in columns)

Try / catch

try:
    src = PostgresSource(table="t", row_type=R, columns=cols)
except ValueError as e:
    if "not found in row_type fields" in str(e):
        src = PostgresSource(table="t", row_type=R)  # derive columns from fields

Prevention

When it happens

Trigger: PostgresSource(..., row_type=MyRecord, columns=['id','emial']) where 'emial' is not a field of MyRecord (typo or renamed field).

Common situations: Typos in column lists, renaming a dataclass field without updating columns, or listing DB column names that differ from the record field names.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of cocoindex-io/cocoindex@e84aa99b32 (2026-09-08). Data as JSON: /api/errors/d69debd95f40d8c5. Report an issue: GitHub.