apache/beam · error · TypeError

Unable to deterministically order element of set for

Error message

Unable to deterministically order element of set for '%s'

What it means

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.

Solutions

  1. Normalize set elements to one comparable type (e.g. all strings) before encoding.
  2. Map the set to sorted(tuple(...)) of a uniform type, or to a frozenset of canonical strings.
  3. Filter out None/incompatible elements upstream of the coder.
  4. Use the step label in the message to find and fix the transform emitting the mixed-type set.

Example fix

// before
out = set(tags)  # tags: ['a', 1, None]
// after
out = {str(t) for t in tags if t is not None}  # uniform, sortable elements
Defensive patterns

Strategy: type-guard

Validate before calling

def set_sortable(s):
    try:
        sorted(s); return True
    except TypeError:
        return False

Type guard

def has_uniform_elements(s):
    return bool(s) and len({type(e) for e in s}) == 1 and None not in s

Try / catch

try:
    result = p.run()
except TypeError as e:
    if 'Unable to deterministically order element of set' in str(e):
        normalize_set_elements_upstream()  # map to str, drop None

Prevention

When it happens

Trigger: Encoding a PCollection element containing a set with mixed or unorderable elements (e.g. {1, 'a', None}) under a deterministic-coding requirement.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/d049dd15f1d4c836. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/coders/coder_impl.py:473

          raise TypeError(
              "Unable to deterministically order keys of dict for '%s'" %
              self.requires_deterministic_step_label) from exn
        for k, v in ordered_kvs:
          self.encode_to_stream(k, stream, True)
          self.encode_to_stream(v, stream, True)
      else:
        # Loop over dict.items() is optimized by Cython.
        for k, v in dict_value.items():
          self.encode_to_stream(k, stream, True)
          self.encode_to_stream(v, stream, True)
    elif t is set:
      stream.write_byte(SET_TYPE)
      stream.write_var_int64(len(value))
      if self.requires_deterministic_step_label is not None:
        try:
          value = sorted(value)
        except Exception as exn:
          raise TypeError(
              "Unable to deterministically order element of set for '%s'" %
              self.requires_deterministic_step_label) from exn
      for e in value:
        self.encode_to_stream(e, stream, True)
    # All possibly deterministic encodings should be above this clause,
    # all non-deterministic ones below.
    elif self.requires_deterministic_step_label is not None:
      self.encode_special_deterministic(value, stream)
    else:
      stream.write_byte(UNKNOWN_TYPE)
      self.fallback_coder_impl.encode_to_stream(value, stream, nested)

  def encode_special_deterministic(self, value, stream):
    if self.warn_deterministic_fallback:
      _LOGGER.warning(
          "Using fallback deterministic coder for type '%s' in '%s'. ",
          type(value),
          self.requires_deterministic_step_label)

View on GitHub (pinned to 12126d8942)