{"record":{"id":"cfd056331be564aa","repo":"apache/beam","slug":"encode-not-implemented-s","errorCode":null,"errorMessage":"Encode not implemented: %s.","messagePattern":"Encode not implemented: (.+?)\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/coders/coders.py","lineNumber":144,"sourceCode":"      coder.__class__.__name__.encode('utf-8'),\n      pickler.dumps(coder, use_zlib=True))\n\n\ndef deserialize_coder(serialized):\n  from apache_beam.internal import pickler\n  return pickler.loads(serialized.split(b'$', 1)[1], use_zlib=True)\n\n\n# pylint: enable=wrong-import-order, wrong-import-position\n\n\nclass Coder(object):\n  \"\"\"Base class for coders.\"\"\"\n  def encode(self, value):\n    # type: (Any) -> bytes\n\n    \"\"\"Encodes the given object into a byte string.\"\"\"\n    raise NotImplementedError('Encode not implemented: %s.' % self)\n\n  def decode(self, encoded):\n    \"\"\"Decodes the given byte string into the corresponding object.\"\"\"\n    raise NotImplementedError('Decode not implemented: %s.' % self)\n\n  def encode_nested(self, value):\n    \"\"\"Uses the underlying implementation to encode in nested format.\"\"\"\n    return self.get_impl().encode_nested(value)\n\n  def decode_nested(self, encoded):\n    \"\"\"Uses the underlying implementation to decode in nested format.\"\"\"\n    return self.get_impl().decode_nested(encoded)\n\n  def is_deterministic(self):\n    # type: () -> bool\n\n    \"\"\"Whether this coder is guaranteed to encode values deterministically.\n","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/coders/coders.py#L126-L162","documentation":"Coder.encode is abstract in apache_beam; the base class raises NotImplementedError('Encode not implemented: %s.') whenever a subclass fails to override encode. This means an instance of a coder class that never implemented encoding was used where the pipeline needed to serialize an element.","triggerScenarios":"Calling coder.encode(value) on the base Coder class or a custom subclass that only overrides decode/other methods; passing a custom coder object to DoFn/transform APIs that then tries to serialize elements.","commonSituations":"Users writing custom coders forgetting to override encode (often only decode was implemented); using an abstract/base Coder directly in tests or pipelines; refactors that renamed a coder's encode method.","solutions":["Implement encode(self, value) returning bytes in your Coder subclass","Use a built-in coder (coders.BytesCoder, ToBytesCoder, etc.) instead of a bare Coder","Check that you instantiate the concrete coder class, not the abstract base"],"exampleFix":"// before\nclass MyCoder(Coder):\n    def decode(self, encoded):\n        return json.loads(encoded)\n// after\nclass MyCoder(Coder):\n    def encode(self, value):\n        return json.dumps(value).encode('utf-8')\n    def decode(self, encoded):\n        return json.loads(encoded)","handlingStrategy":"type-guard","validationCode":"if type(coder).encode is Coder.encode:\n    raise TypeError('coder does not implement encode')","typeGuard":"def is_encodable(coder):\n    return callable(getattr(coder, 'encode', None)) and type(coder).encode is not Coder.encode","tryCatchPattern":"try:\n    data = coder.encode(value)\nexcept NotImplementedError as e:\n    log.error('coder %s cannot encode: %s', coder, e)\n    data = fallback_coder.encode(value)","preventionTips":["Always implement both encode and decode in custom coders","Round-trip test custom coders in unit tests","Use built-in coders when possible"],"tags":["python","apache-beam","serialization","abstract-method"],"backgroundTag":"method-not-implemented","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"}