{"record":{"id":"89e4933e4012d3ca","repo":"apache/beam","slug":"cannot-convert-element-of-type-type-element-to-beam-row-for","errorCode":null,"errorMessage":"Cannot convert element of type {type(element)} to beam.Row for validation in {label}. Element: {element}","messagePattern":"Cannot convert element of type (.+?) to beam\\.Row for validation in (.+?)\\. Element: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/yaml/yaml_transform.py","lineNumber":780,"sourceCode":"        \"PCollection for %s has no schema (element_type=Any). \"\n        \"Converting elements to beam.Row based on provided output_schema.\",\n        label)\n    try:\n      # Attempt to confer the schemaless elements into schema-aware beam.Row\n      # objects\n      beam_schema = json_utils.json_schema_to_beam_schema(clean_schema)\n      row_type_constraint = schemas.named_tuple_from_schema(beam_schema)\n\n      def to_row(element):\n        \"\"\"\n        Convert a single element into the row type constraint type.\n        \"\"\"\n        if isinstance(element, dict):\n          return row_type_constraint(**element)\n        elif hasattr(element, '_asdict'):  # Handle NamedTuple, beam.Row\n          return row_type_constraint(**element._asdict())\n        else:\n          raise TypeError(\n              f\"Cannot convert element of type {type(element)} to beam.Row \"\n              f\"for validation in {label}. Element: {element}\")\n\n      pcoll = pcoll | f'{label}_ConvertToRow' >> beam.Map(\n          to_row).with_output_types(row_type_constraint)\n    except Exception as e:\n      raise ValueError(\n          f\"Failed to prepare schemaless PCollection for \\\n            validation in {label}: {e}\") from e\n\n  # Add Validation step downstream of current transform\n  return pcoll | label >> Validate(\n      schema=clean_schema, error_handling=error_handling_spec)\n\n\ndef expand_composite_transform(spec, scope):\n  spec = normalize_inputs_outputs(normalize_source_sink(spec))\n","sourceCodeStart":762,"sourceCodeEnd":798,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/yaml/yaml_transform.py#L762-L798","documentation":"During output_schema validation, elements of a schemaless PCollection must be converted to beam.Row to match the schema. to_row handles dicts and NamedTuple-like objects; anything else raises this TypeError naming the element type and label.","triggerScenarios":"Applying output_schema to a PCollection whose elements are plain objects, strings, ints, or custom classes that are neither dict nor have _asdict (NamedTuple/beam.Row), when no explicit schema allows direct validation.","commonSituations":"Reading untyped/JSON-less data (e.g. raw strings from text) and attaching an output_schema; custom transforms emitting plain Python objects instead of dicts or Rows.","solutions":["Have the producing transform emit dicts or beam.Row objects (e.g. use MapToFields to reshape elements).","If elements are NamedTuples, they are supported — check that the type actually defines _asdict.","Insert an explicit MapToFields step before the transform whose output_schema you want validated.","Convert to a schema'd PCollection so the conversion path isn't needed."],"exampleFix":"// before\n- type: LogData  # emits plain strings\n- type: MyTransform\n  config:\n    output_schema: {schema: 'id: INTEGER'}\n// after\n- type: MapToFields\n  input: logged\n  config:\n    id: json_parse(element).id\n    output_schema: {schema: 'id: INTEGER'}","handlingStrategy":"type-guard","validationCode":"def row_convertible(el) -> bool:\n    return isinstance(el, dict) or hasattr(el, '_asdict')","typeGuard":"def is_row_like(el) -> bool:\n    return isinstance(el, dict) or hasattr(el, '_asdict')","tryCatchPattern":"try:\n    expand_output_schema_transform(spec, outputs, eh)\nexcept (TypeError, ValueError) as e:\n    if 'Cannot convert element' in str(e):\n        print('Emit dicts or beam.Row from the upstream transform')\n    else:\n        raise","preventionTips":["Make transforms emit dicts or beam.Row, not raw strings/objects.","Insert MapToFields to reshape untyped data before output_schema validation.","Add with_output_types/row typing to custom transforms."],"tags":["python","apache-beam","yaml","schema"],"backgroundTag":"type-mismatch","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"}