{"record":{"id":"bbc2f692bcbdd582","repo":"apache/beam","slug":"cannot-encode-given-object-of-type-value-getclass","errorCode":null,"errorMessage":"Cannot encode given object of type [<value.getClass()>].","messagePattern":"Cannot encode given object of type \\[<value\\.getClass\\(\\)>\\]\\.","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/kryo/src/main/java/org/apache/beam/sdk/extensions/kryo/KryoCoder.java","lineNumber":209,"sourceCode":"\n  @Override\n  public void encode(T value, OutputStream outStream) throws IOException {\n    final KryoState kryoState = KryoState.get(this);\n    if (value == null) {\n      throw new CoderException(\"Cannot encode a null value.\");\n    }\n    final OutputChunked outputChunked = kryoState.getOutputChunked();\n    outputChunked.setOutputStream(outStream);\n    try {\n      kryoState.getKryo().writeClassAndObject(outputChunked, value);\n      outputChunked.endChunk();\n      outputChunked.flush();\n    } catch (KryoException e) {\n      outputChunked.reset();\n      if (e.getCause() instanceof EOFException) {\n        throw (EOFException) e.getCause();\n      }\n      throw new CoderException(\"Cannot encode given object of type [\" + value.getClass() + \"].\", e);\n    } catch (IllegalArgumentException e) {\n      String message = e.getMessage();\n      if (message != null) {\n        if (message.startsWith(\"Class is not registered\")) {\n          throw new CoderException(message);\n        }\n      }\n      throw e;\n    }\n  }\n\n  @Override\n  public T decode(InputStream inStream) throws IOException {\n    final KryoState kryoState = KryoState.get(this);\n    final InputChunked inputChunked = kryoState.getInputChunked();\n    inputChunked.setInputStream(inStream);\n    try {\n      @SuppressWarnings(\"unchecked\")","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/kryo/src/main/java/org/apache/beam/sdk/extensions/kryo/KryoCoder.java#L191-L227","documentation":"When Kryo throws KryoException during writeClassAndObject, KryoCoder.encode() resets the output and rethrows a CoderException naming the Java class that failed to serialize, chaining the KryoException. EOFException causes are rethrown as-is since they indicate stream truncation, not encoding failure.","triggerScenarios":"Serializing an object graph containing classes Kryo can't handle — non-serializable fields (streams, connections), classes with broken no-arg constructors, or deeply circular references exceeding Kryo limits.","commonSituations":"DoFn output types holding an InputStream/Socket/Logger; classes without no-arg constructors (Kryo's default instantiation); objects from third-party libs never designed for serialization; JDK version changes altering internal classes.","solutions":["Read the chained KryoException cause to find the offending field/class","Register the class with a KryoRegistrar or make it Kryo-serializable (no-arg constructor, no transient-unfriendly fields)","Mark unserializable fields transient or exclude them via a custom Kryo FieldSerializer","Replace unserializable field types with plain data (e.g. byte[] instead of InputStream)"],"exampleFix":"// before\npublic class Event { private InputStream data; }\n// after\npublic class Event { private transient InputStream data; private byte[] dataBytes; }","handlingStrategy":"try-catch","validationCode":"java\n// smoke-test serialization of representative objects before launching\ntry (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {\n  coder.encode(sampleValue, bos);\n} catch (Exception e) { throw new IllegalStateException(\"Unencodable sample: \" + sampleValue.getClass(), e); }","typeGuard":null,"tryCatchPattern":"java\ntry {\n  coder.encode(value, out);\n} catch (CoderException e) {\n  if (e.getMessage().startsWith(\"Cannot encode given object of type\")) {\n    log.severe(\"Kryo cannot serialize \" + e.getCause());\n  }\n}","preventionTips":["Keep DoFn output types POJO-clean: no streams/sockets/loggers","Mark non-serializable fields transient","Register types with a KryoRegistrar","Add a unit test encoding representative instances of every pipeline type"],"tags":["java","beam","kryo","serialization"],"backgroundTag":"json-serialization-failed","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"}