{"record":{"id":"d049dd15f1d4c836","repo":"apache/beam","slug":"unable-to-deterministically-order-element-of-set-for-s","errorCode":null,"errorMessage":"Unable to deterministically order element of set for '%s'","messagePattern":"Unable to deterministically order element of set for '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/coders/coder_impl.py","lineNumber":473,"sourceCode":"          raise TypeError(\n              \"Unable to deterministically order keys of dict for '%s'\" %\n              self.requires_deterministic_step_label) from exn\n        for k, v in ordered_kvs:\n          self.encode_to_stream(k, stream, True)\n          self.encode_to_stream(v, stream, True)\n      else:\n        # Loop over dict.items() is optimized by Cython.\n        for k, v in dict_value.items():\n          self.encode_to_stream(k, stream, True)\n          self.encode_to_stream(v, stream, True)\n    elif t is set:\n      stream.write_byte(SET_TYPE)\n      stream.write_var_int64(len(value))\n      if self.requires_deterministic_step_label is not None:\n        try:\n          value = sorted(value)\n        except Exception as exn:\n          raise TypeError(\n              \"Unable to deterministically order element of set for '%s'\" %\n              self.requires_deterministic_step_label) from exn\n      for e in value:\n        self.encode_to_stream(e, stream, True)\n    # All possibly deterministic encodings should be above this clause,\n    # all non-deterministic ones below.\n    elif self.requires_deterministic_step_label is not None:\n      self.encode_special_deterministic(value, stream)\n    else:\n      stream.write_byte(UNKNOWN_TYPE)\n      self.fallback_coder_impl.encode_to_stream(value, stream, nested)\n\n  def encode_special_deterministic(self, value, stream):\n    if self.warn_deterministic_fallback:\n      _LOGGER.warning(\n          \"Using fallback deterministic coder for type '%s' in '%s'. \",\n          type(value),\n          self.requires_deterministic_step_label)","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/coders/coder_impl.py#L455-L491","documentation":"Analogous to the dict case: when deterministic coding is required, encode_to_stream sorts set elements before writing them. If the elements are not mutually comparable (mixed types, None), sorting fails and a TypeError naming the step label is raised, chained from the underlying exception.","triggerScenarios":"Encoding a PCollection element containing a set with mixed or unorderable elements (e.g. {1, 'a', None}) under a deterministic-coding requirement.","commonSituations":"Sets assembled from loosely typed upstream data (JSON arrays, unions of branches); frozensets with heterogeneous elements passed as side inputs or keyed values in streaming pipelines.","solutions":["Normalize set elements to one comparable type (e.g. all strings) before encoding.","Map the set to sorted(tuple(...)) of a uniform type, or to a frozenset of canonical strings.","Filter out None/incompatible elements upstream of the coder.","Use the step label in the message to find and fix the transform emitting the mixed-type set."],"exampleFix":"// before\nout = set(tags)  # tags: ['a', 1, None]\n// after\nout = {str(t) for t in tags if t is not None}  # uniform, sortable elements","handlingStrategy":"type-guard","validationCode":"def set_sortable(s):\n    try:\n        sorted(s); return True\n    except TypeError:\n        return False","typeGuard":"def has_uniform_elements(s):\n    return bool(s) and len({type(e) for e in s}) == 1 and None not in s","tryCatchPattern":"try:\n    result = p.run()\nexcept TypeError as e:\n    if 'Unable to deterministically order element of set' in str(e):\n        normalize_set_elements_upstream()  # map to str, drop None","preventionTips":["Convert loosely typed collections to sets of a single canonical type (str) early.","Filter None from sets before they reach coder boundaries.","Test pipeline encodability with deterministic coder requirements enabled."],"tags":["python","coder","determinism","set","typeerror"],"backgroundTag":"type-mismatch","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"}