{"record":{"id":"58e296d288e0398f","repo":"apache/beam","slug":"writetotfrecord-requires-an-input-schema-with-exactly-one-58e296","errorCode":null,"errorMessage":"WriteToTFRecord requires an input schema with exactly one field,got %s","messagePattern":"WriteToTFRecord requires an input schema with exactly one field,got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/yaml/yaml_io.py","lineNumber":763,"sourceCode":"      is '-SSSSS-of-NNNNN' if None is passed as the shard_name_template.\n    compression_type: Used to handle compressed output files. Typical value\n      is CompressionTypes.AUTO, in which case the file_path's extension will\n      be used to detect the compression.\n\n  Returns:\n    A WriteToTFRecord transform object.\n  \"\"\"\n  try:\n    field_names = [\n        name for name, _ in schemas.named_fields_from_element_type(\n            pcoll.element_type)\n    ]\n  except Exception as exn:\n    raise ValueError(\n        \"WriteToTFRecord requires an input schema with exactly one field.\"\n    ) from exn\n  if len(field_names) != 1:\n    raise ValueError(\n        \"WriteToTFRecord requires an input schema with exactly one field,got %s\"\n        % field_names)\n  sole_field_name, = field_names\n\n  return pcoll | beam.Map(\n      lambda x: getattr(x, sole_field_name)) | WriteToTFRecord(\n          file_path_prefix=file_path_prefix,\n          coder=coder,\n          file_name_suffix=file_name_suffix,\n          num_shards=num_shards,\n          shard_name_template=shard_name_template,\n          compression_type=getattr(CompressionTypes, compression_type))\n\n\n@beam.ptransform_fn\n@yaml_errors.maybe_with_exception_handling_transform_fn\ndef read_from_mongodb(\n    root,","sourceCodeStart":745,"sourceCodeEnd":781,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/yaml/yaml_io.py#L745-L781","documentation":"Like 4127 but for the case where the schema resolved successfully yet contains more (or zero) than one field. TFRecord holds a single raw value per record, so the wrapper raises listing the offending field names.","triggerScenarios":"write_to_tfrecord given a schema with 2+ named fields, e.g. beam.Row(a=..., b=...).","commonSituations":"Users expect TFRecord to serialize whole rows; it only writes the sole field's value. Multi-field rows need JSON/protobuf-style sinks instead.","solutions":["Reduce input schema to one field containing exactly the bytes to write","Serialize the row yourself (e.g. to JSON string) into a single field before writing","Choose WriteToJson or another multi-field-capable sink"],"exampleFix":"// before\nbeam.Row(name='x', value=1) -> write_to_tfrecord\n// after\nbeam.Row(data=json.dumps({'name':'x','value':1}).encode()) -> write_to_tfrecord","handlingStrategy":"validation","validationCode":"names = [n for n, _ in schemas.named_fields_from_element_type(pcoll.element_type)]\nif len(names) != 1:\n    raise ValueError(f'TFRecord needs exactly one field, got {names}')","typeGuard":null,"tryCatchPattern":"try:\n    pcoll | yaml_io.write_to_tfrecord(...)\nexcept ValueError as e:\n    if 'exactly one field' in str(e):\n        pcoll = pcoll | beam.Map(lambda r: beam.Row(data=json.dumps(r._as_dict()).encode()))","preventionTips":["Serialize multi-field rows into one bytes/str field first","Choose a different sink when data has multiple columns","Validate schema cardinality in pipeline construction tests"],"tags":["python","apache-beam","tfrecord","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}