{"record":{"id":"b10d837a1bdf3475","repo":"apache/beam","slug":"could-not-determine-schema-for-type-hint-element-type-r-did","errorCode":null,"errorMessage":"Could not determine schema for type hint {element_type!r}. Did you mean to create a schema-aware PCollection? See https://s.apache.org/beam-python-schemas","messagePattern":"Could not determine schema for type hint (.+?)\\. Did you mean to create a schema-aware PCollection\\? See https://s\\.apache\\.org/beam-python-schemas","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/schemas.py","lineNumber":710,"sourceCode":"def schema_from_element_type(element_type: type) -> schema_pb2.Schema:\n  \"\"\"Get a schema for the given PCollection element_type.\n\n  Returns schema as a list of (name, python_type) tuples\"\"\"\n  if isinstance(element_type, row_type.RowTypeConstraint):\n    return named_fields_to_schema(element_type._fields)\n  elif match_is_named_tuple(element_type) or match_dataclass_for_row(\n      element_type):\n    # schema id does not inherit from base classes\n    if row_type._BEAM_SCHEMA_ID in element_type.__dict__:\n      # if the named tuple's schema is in registry, we just use it instead of\n      # regenerating one.\n      schema_id = getattr(element_type, row_type._BEAM_SCHEMA_ID)\n      schema = SCHEMA_REGISTRY.get_schema_by_id(schema_id)\n      if schema is not None:\n        return schema\n    return named_tuple_to_schema(element_type)\n  else:\n    raise TypeError(\n        f\"Could not determine schema for type hint {element_type!r}. Did you \"\n        \"mean to create a schema-aware PCollection? See \"\n        \"https://s.apache.org/beam-python-schemas\")\n\n\ndef named_fields_from_element_type(\n    element_type: type) -> List[Tuple[str, type]]:\n  return named_fields_from_schema(schema_from_element_type(element_type))\n\n\ndef union_schema_type(element_types):\n  \"\"\"Returns a schema whose fields are the union of each corresponding field.\n\n  element_types must be a set of schema-aware types whose fields have the\n  same naming and ordering.\n  \"\"\"\n  named_fields_and_types = []\n  for t in element_types:","sourceCodeStart":692,"sourceCodeEnd":728,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/schemas.py#L692-L728","documentation":"`schema_from_element_type` derives a Schema proto from a Python type hint. It handles NamedTuples, BeamSchema rows, RowTypeConstraints, and simple primitives; a hint that maps to none of these (e.g. plain dict, arbitrary class, dict[str, Any]) cannot be turned into a schema, so it raises this TypeError pointing at schema-aware PCollection docs.","triggerScenarios":"Calling `schema_from_element_type` with a non-schema type hint — e.g. `dict`, a plain class, `Any`, or a Union — via `from_type_hint`, `expand` on a PTransform expecting typed input, or `named_fields_from_element_type`.","commonSituations":"Applying a typed transform (like `MapToFields`, `to_row`, or SQL) to a PCollection of dicts instead of NamedTuples/rows; forgetting to annotate DoFn output types.","solutions":["Emit NamedTuple instances (or BeamSchema rows) from your transform instead of dicts","Apply `Map(lambda d: MyNamedTuple(**d))` to convert dicts to a NamedTuple before the typed transform","Annotate your DoFn's process return type with the schema type so the schema can be inferred"],"exampleFix":"// before\np | beam.Map(lambda x: {'name': x[0], 'age': x[1]}) | SqlTransform(...)\n// after\nclass Row(typing.NamedTuple):\n  name: str\n  age: int\np | beam.Map(lambda x: Row(name=x[0], age=x[1])) | SqlTransform(...)","handlingStrategy":"validation","validationCode":"import typing\nif not (isinstance(element_type, type) and issubclass(element_type, tuple) and hasattr(element_type, '_fields')):\n  raise TypeError('element_type must be a NamedTuple/row type')","typeGuard":"def is_schema_type(t) -> bool:\n  return isinstance(t, type) and issubclass(t, tuple) and hasattr(t, '_fields')","tryCatchPattern":"try:\n  schema = schema_from_element_type(hint)\nexcept TypeError:\n  raise TypeError('convert your elements to a NamedTuple first') from None","preventionTips":["Emit NamedTuples or Beam rows from transforms feeding typed operations","Annotate DoFn/Map output types so schema inference works","Avoid dicts and untyped classes as PCollection element types when schemas are needed"],"tags":["python","schema","typehints","typed-pipeline"],"backgroundTag":"incompatible-source-type","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"}