{"record":{"id":"afd58c432fda90a1","repo":"apache/beam","slug":"cannot-decode-object-from-input-stream","errorCode":null,"errorMessage":"Cannot decode object from input stream.","messagePattern":"Cannot decode object from input stream\\.","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":231,"sourceCode":"        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\")\n      final T instance = (T) kryoState.getKryo().readClassAndObject(inputChunked);\n      return instance;\n    } catch (KryoException e) {\n      throw new CoderException(\"Cannot decode object from input stream.\", e);\n    }\n  }\n\n  @Override\n  public void verifyDeterministic() throws NonDeterministicException {\n    // noop\n  }\n\n  /**\n   * Create a new {@link KryoCoder} instance with the user provided registrar.\n   *\n   * @param registrar registrar to append to list of already registered registrars.\n   * @return new kryo coder\n   */\n  public KryoCoder<T> withRegistrar(KryoRegistrar registrar) {\n    final List<KryoRegistrar> newRegistrars = new ArrayList<>(registrars);\n    registrars.add(registrar);\n    return new KryoCoder<>(options, newRegistrars);","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/kryo/src/main/java/org/apache/beam/sdk/extensions/kryo/KryoCoder.java#L213-L249","documentation":"KryoCoder.decode reads a serialized object from the input stream using Kryo's readClassAndObject. If Kryo encounters malformed, truncated, or incompatible data in the stream it throws a KryoException, which is wrapped in a Beam CoderException with this message. It means the bytes on the wire cannot be decoded into an object by the configured Kryo instance.","triggerScenarios":"Decoding a PCollection element whose bytes were not written by the matching KryoCoder (encode/decode coder mismatch), a truncated or corrupted input stream, or reading data encoded with a different Kryo registration/serialization configuration than the one used at decode time.","commonSituations":"Changing KryoOptions (registrationRequired, custom serializers, registrations) between the job that encoded data and the one decoding it; re-reading old files/checkpoints written by a previous serializer version; incorrect coder inference causing a mismatched coder on a PCollection.","solutions":["Ensure the same KryoCoder/KryoOptions (registrations, serializers) is used on both the encoding and decoding side of the PCollection.","Verify the input stream contains data actually encoded by KryoCoder.encode (readClassAndObject/writeClassAndObject pair).","Check for stream truncation or corruption in the source (e.g. shuffled/serialized bytes in the runner).","Explicitly set the coder with PCollection.setCoder(KryoCoder.of(options)) if type erasure caused coder inference to pick the wrong coder."],"exampleFix":"// before: relying on inferred coder after changing Kryo options\nPCollection<MyType> out = in.apply(...);\n// after: pin the coder so encode/decode use the same Kryo configuration\nPCollection<MyType> out = in.apply(...).setCoder(KryoCoder.of(KryoOptions.withRegistrations(regs)));","handlingStrategy":"try-catch","validationCode":"if (bytes == null || bytes.length == 0) throw new IllegalArgumentException(\"Empty payload for Kryo decode\");","typeGuard":null,"tryCatchPattern":"try { T obj = coder.decode(stream); } catch (CoderException e) { LOG.error(\"Kryo decode failed; check coder/config match\", e); throw new UnrecoverableException(e); }","preventionTips":["Pin the coder explicitly with setCoder(KryoCoder.of(...)) so encode/decode always match","Keep Kryo registrations and serializers identical on producer and consumer","Never change Kryo options mid-stream on persistent data without re-encoding","Test round-trip encode/decode for custom types"],"tags":["java","serialization","kryo","apache-beam"],"backgroundTag":"json-decode-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"}