{"record":{"id":"cb7b5efb4baf8592","repo":"apache/beam","slug":"unable-to-deterministically-encode-s-of-type-s-for-the-input","errorCode":null,"errorMessage":"Unable to deterministically encode '%s' of type '%s', for the input of '%s'. The object defines __getstate__ but not __setstate__.","messagePattern":"Unable to deterministically encode '(.+?)' of type '(.+?)', for the input of '(.+?)'\\. The object defines __getstate__ but not __setstate__\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/coders/coder_impl.py","lineNumber":538,"sourceCode":"      stream.write_byte(NAMED_TUPLE_TYPE)\n      self.encode_type(type(value), stream)\n      try:\n        self.iterable_coder_impl.encode_to_stream(value, stream, True)\n      except Exception as e:\n        raise TypeError(self._deterministic_encoding_error_msg(value)) from e\n    elif isinstance(value, enum.Enum):\n      stream.write_byte(ENUM_TYPE)\n      self.encode_type(type(value), stream)\n      # Enum values can be of any type.\n      try:\n        self.encode_to_stream(value.value, stream, True)\n      except Exception as e:\n        raise TypeError(self._deterministic_encoding_error_msg(value)) from e\n    elif (hasattr(value, \"__getstate__\") and\n          # https://github.com/apache/beam/issues/33020\n          type(value).__reduce__ == object.__reduce__):\n      if not hasattr(value, \"__setstate__\"):\n        raise TypeError(\n            \"Unable to deterministically encode '%s' of type '%s', \"\n            \"for the input of '%s'. The object defines __getstate__ but not \"\n            \"__setstate__.\" %\n            (value, type(value), self.requires_deterministic_step_label))\n      stream.write_byte(NESTED_STATE_TYPE)\n      self.encode_type(type(value), stream)\n      state_value = value.__getstate__()\n      try:\n        self.encode_to_stream(state_value, stream, True)\n      except Exception as e:\n        raise TypeError(self._deterministic_encoding_error_msg(value)) from e\n    else:\n      raise TypeError(self._deterministic_encoding_error_msg(value))\n\n  def _deterministic_encoding_error_msg(self, value):\n    return (\n        \"Unable to deterministically encode '%s' of type '%s', \"\n        \"please provide a type hint for the input of '%s'\" %","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/coders/coder_impl.py#L520-L556","documentation":"Beam can deterministically encode objects that define __getstate__ (with default object.__reduce__) by encoding their state. If the object defines __getstate__ but not __setstate__, it cannot be reconstructed on the other side of the wire, so the coder raises TypeError naming the input step. This guards against silently producing data the pipeline cannot decode.","triggerScenarios":"Encoding a custom class instance that defines __getstate__ (inheriting pickle behavior from an ancestor that does) but not __setstate__, as input to a step requiring deterministic encoding; the hasattr(value, '__setstate__') check fails in encode_special_deterministic.","commonSituations":"Subclassing a class that defines __getstate__ (e.g. some library base classes) and using instances as pipeline data; adding __getstate__ for pickling without __setstate__; Python 3.11+ default object __getstate__ interactions (see Beam issue #33020).","solutions":["Add a matching __setstate__ method that restores state from the __getstate__ payload.","Remove the custom __getstate__ if unnecessary so a deterministic fallback applies.","Convert the object into a NamedTuple or frozen dataclass before sending it through the pipeline.","Give the PTransform a concrete type hint with a deterministically encodable type and map the object to that type."],"exampleFix":"// before\nclass Thing:\n    def __getstate__(self):\n        return self.__dict__\n\n// after\nclass Thing:\n    def __getstate__(self):\n        return self.__dict__\n    def __setstate__(self, state):\n        self.__dict__.update(state)","handlingStrategy":"type-guard","validationCode":"def supports_deterministic_state(obj):\n    return (hasattr(obj, '__getstate__') and hasattr(obj, '__setstate__')\n            and type(obj).__reduce__ == object.__reduce__)","typeGuard":"def is_statefully_encodable(obj) -> bool:\n    t = type(obj)\n    return hasattr(obj, '__getstate__') and hasattr(obj, '__setstate__') and t.__reduce__ == object.__reduce__","tryCatchPattern":"try:\n    coder.encode(obj)\nexcept TypeError:\n    obj = to_namedtuple(obj)\n    coder.encode(obj)","preventionTips":["If you define __getstate__, always define __setstate__","Prefer NamedTuples/frozen dataclasses over custom stateful classes for pipeline data","Watch for inherited __getstate__ from base classes"],"tags":["apache-beam","python","coder","serialization","pickle"],"backgroundTag":"unsupported-operation","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"}