apache/beam · error · AttributeError

The TableRowJsonCoder requires a table schema for encoding o

Error message

The TableRowJsonCoder requires a table schema for encoding operations. Please specify a table_schema argument.

What it means

TableRowJsonCoder.encode requires a table schema to know field names/types when serializing a TableRow to JSON; if the coder was constructed without table_schema, encoding is impossible and an AttributeError is raised.

Source

Thrown at sdks/python/apache_beam/io/gcp/bigquery.py:563

  """A coder for a TableRow instance to/from a JSON string.

  Note that the encoding operation (used when writing to sinks) requires the
  table schema in order to obtain the ordered list of field names. Reading from
  sources on the other hand does not need the table schema.
  """
  def __init__(self, table_schema=None):
    # The table schema is needed for encoding TableRows as JSON (writing to
    # sinks) because the ordered list of field names is used in the JSON
    # representation.
    self.table_schema = table_schema
    # Precompute field names since we need them for row encoding.
    if self.table_schema:
      self.field_names = tuple(fs.name for fs in self.table_schema.fields)
      self.field_types = tuple(fs.type for fs in self.table_schema.fields)

  def encode(self, table_row):
    if self.table_schema is None:
      raise AttributeError(
          'The TableRowJsonCoder requires a table schema for '
          'encoding operations. Please specify a table_schema argument.')
    try:
      return json.dumps(
          collections.OrderedDict(
              zip(
                  self.field_names,
                  [from_json_value(f.v) for f in table_row.f])),
          allow_nan=False,
          default=bigquery_tools.default_encoder)
    except ValueError as e:
      raise ValueError('%s. %s' % (e, bigquery_tools.JSON_COMPLIANCE_ERROR))

  def decode(self, encoded_table_row):
    od = json.loads(
        encoded_table_row, object_pairs_hook=collections.OrderedDict)
    return bigquery.TableRow(
        f=[bigquery.TableCell(v=to_json_value(e)) for e in od.values()])

View on GitHub (pinned to 12126d8942)

Solutions

  1. Construct the coder with a schema: TableRowJsonCoder(table_schema=table_schema)
  2. Fetch the schema via bigquery_tools.get_table_schema(project, dataset, table) and pass it in
  3. Use a different coder or default behavior if encoding is not needed

Example fix

// before
coder = TableRowJsonCoder()
encoded = coder.encode(row)
// after
coder = TableRowJsonCoder(table_schema=known_table_schema)
encoded = coder.encode(row)
Defensive patterns

Strategy: type-guard

Validate before calling

assert coder.table_schema is not None, 'TableRowJsonCoder needs table_schema for encoding'

Type guard

def can_encode(coder):
    return getattr(coder, 'table_schema', None) is not None

Try / catch

try:
    encoded = coder.encode(row)
except AttributeError as e:
    if 'requires a table schema' in str(e):
        coder = TableRowJsonCoder(table_schema=fetch_schema())
        encoded = coder.encode(row)
    else:
        raise

Prevention

When it happens

Trigger: Using TableRowJsonCoder() without the table_schema argument and calling encode(); decoding may work but encoding will always fail.

Common situations: Writing to BigQuery with a custom coder in WRITE truncation/file loads where the schema is known only later; copy-pasting a coder used for decoding into an encode path.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/38d30f6b2878c9f1. Report an issue: GitHub.