{"record":{"id":"bd16d39b617b257b","repo":"apache/beam","slug":"cannot-create-union-without-a-sequence-of-types","errorCode":null,"errorMessage":"Cannot create Union without a sequence of types.","messagePattern":"Cannot create Union without a sequence of types\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/typehints.py","lineNumber":582,"sourceCode":"      sub_bindings = [\n          match_type_variables(t, concrete_type) for t in self.union_types\n          if is_consistent_with(concrete_type, t)\n      ]\n      if sub_bindings:\n        return {\n            var: Union[(sub[var] for sub in sub_bindings)]\n            for var in set.intersection(\n                *[set(sub.keys()) for sub in sub_bindings])\n        }\n      else:\n        return {}\n\n    def bind_type_variables(self, bindings):\n      return Union[(bind_type_variables(t, bindings) for t in self.union_types)]\n\n  def __getitem__(self, type_params):\n    if not isinstance(type_params, (abc.Iterable, set)):\n      raise TypeError('Cannot create Union without a sequence of types.')\n\n    # Flatten nested Union's and duplicated repeated type hints.\n    params = set()\n    dict_union = None\n    for t in type_params:\n      validate_composite_type_param(\n          t, error_msg_prefix='All parameters to a Union hint')\n\n      if isinstance(t, self.UnionConstraint):\n        params |= set(t.union_types)\n      elif isinstance(t, DictConstraint):\n        if dict_union is None:\n          dict_union = t\n        else:\n          dict_union.key_type = Union[dict_union.key_type, t.key_type]\n          dict_union.value_type = Union[dict_union.value_type, t.value_type]\n      else:\n        params.add(t)","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/typehints.py#L564-L600","documentation":"UnionHint.__getitem__ raises TypeError('Cannot create Union without a sequence of types.') when the subscription is not an iterable/set — i.e. someone writes Union[int] instead of Union[int, str] or passes a non-sequence. Beam's Union requires a collection of two or more type parameters to combine.","triggerScenarios":"Writing Union[int] with a single bare type; passing a single non-iterable object (Union[SomeClass]) or a generator not recognized as Iterable in older contexts; dynamic code building Union from a single variable.","commonSituations":"Refactors that collapse a union to one member; code generators building hints programmatically that special-case single-type lists incorrectly; confusion between typing.Union (which allows one arg) and Beam's Union.","solutions":["Always pass at least two types: Union[int, str]. If only one type remains, drop the Union and use the type directly.","If building dynamically, wrap single values as a one-element list only if Beam's version accepts it, or special-case: t if single else Union[tuple(ts)].","Prefer typing.Union from the typing module if you need single-argument tolerance, ensuring Beam accepts it (it checks __module__ == 'typing').","Audit hint-building code for cases where a list of types can shrink to length 1."],"exampleFix":"# before\nhint = Union[int]  # single type\n# after\nhint = int  # or Union[int, str] for multiple","handlingStrategy":"validation","validationCode":"def build_union(ts):\n    if not isinstance(ts, (list, tuple, set)) or len(ts) < 2:\n        raise ValueError('Union needs 2+ types')\n    return Union[tuple(ts)]","typeGuard":"def is_nonempty_seq(x):\n    return isinstance(x, (list, tuple, set)) and len(x) >= 2","tryCatchPattern":"try:\n    hint = Union[params]\nexcept TypeError as e:\n    if 'without a sequence of types' in str(e):\n        hint = params[0] if len(params) == 1 else Union[tuple(params)]\n    else:\n        raise","preventionTips":["Handle the single-type case explicitly when building hints dynamically","Remember Beam's Union needs a sequence; prefer 2+ members","Prefer typing.Union when single-argument tolerance is needed"],"tags":["python","apache-beam","typehints","union"],"backgroundTag":"invalid-argument-value","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"}