{"record":{"id":"38d30f6b2878c9f1","repo":"apache/beam","slug":"the-tablerowjsoncoder-requires-a-table-schema-for-encoding","errorCode":null,"errorMessage":"The TableRowJsonCoder requires a table schema for encoding operations. Please specify a table_schema argument.","messagePattern":"The TableRowJsonCoder requires a table schema for encoding operations\\. Please specify a table_schema argument\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/gcp/bigquery.py","lineNumber":563,"sourceCode":"  \"\"\"A coder for a TableRow instance to/from a JSON string.\n\n  Note that the encoding operation (used when writing to sinks) requires the\n  table schema in order to obtain the ordered list of field names. Reading from\n  sources on the other hand does not need the table schema.\n  \"\"\"\n  def __init__(self, table_schema=None):\n    # The table schema is needed for encoding TableRows as JSON (writing to\n    # sinks) because the ordered list of field names is used in the JSON\n    # representation.\n    self.table_schema = table_schema\n    # Precompute field names since we need them for row encoding.\n    if self.table_schema:\n      self.field_names = tuple(fs.name for fs in self.table_schema.fields)\n      self.field_types = tuple(fs.type for fs in self.table_schema.fields)\n\n  def encode(self, table_row):\n    if self.table_schema is None:\n      raise AttributeError(\n          'The TableRowJsonCoder requires a table schema for '\n          'encoding operations. Please specify a table_schema argument.')\n    try:\n      return json.dumps(\n          collections.OrderedDict(\n              zip(\n                  self.field_names,\n                  [from_json_value(f.v) for f in table_row.f])),\n          allow_nan=False,\n          default=bigquery_tools.default_encoder)\n    except ValueError as e:\n      raise ValueError('%s. %s' % (e, bigquery_tools.JSON_COMPLIANCE_ERROR))\n\n  def decode(self, encoded_table_row):\n    od = json.loads(\n        encoded_table_row, object_pairs_hook=collections.OrderedDict)\n    return bigquery.TableRow(\n        f=[bigquery.TableCell(v=to_json_value(e)) for e in od.values()])","sourceCodeStart":545,"sourceCodeEnd":581,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/gcp/bigquery.py#L545-L581","documentation":"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.","triggerScenarios":"Using TableRowJsonCoder() without the table_schema argument and calling encode(); decoding may work but encoding will always fail.","commonSituations":"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.","solutions":["Construct the coder with a schema: TableRowJsonCoder(table_schema=table_schema)","Fetch the schema via bigquery_tools.get_table_schema(project, dataset, table) and pass it in","Use a different coder or default behavior if encoding is not needed"],"exampleFix":"// before\ncoder = TableRowJsonCoder()\nencoded = coder.encode(row)\n// after\ncoder = TableRowJsonCoder(table_schema=known_table_schema)\nencoded = coder.encode(row)","handlingStrategy":"type-guard","validationCode":"assert coder.table_schema is not None, 'TableRowJsonCoder needs table_schema for encoding'","typeGuard":"def can_encode(coder):\n    return getattr(coder, 'table_schema', None) is not None","tryCatchPattern":"try:\n    encoded = coder.encode(row)\nexcept AttributeError as e:\n    if 'requires a table schema' in str(e):\n        coder = TableRowJsonCoder(table_schema=fetch_schema())\n        encoded = coder.encode(row)\n    else:\n        raise","preventionTips":["Always construct TableRowJsonCoder with table_schema when encoding","Fetch the schema from BigQuery before building the coder","Keep separate coders for encode vs decode paths"],"tags":["python","apache-beam","bigquery","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}