{"record":{"id":"594b98ef34e412bf","repo":"apache/beam","slug":"ptransform-getoutputcoder-called","errorCode":null,"errorMessage":"PTransform.getOutputCoder called.","messagePattern":"PTransform\\.getOutputCoder called\\.","errorType":"exception","errorClass":"CannotProvideCoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java","lineNumber":331,"sourceCode":"  private void readObject(ObjectInputStream oos) {\n    // We don't really want to be serializing this object, but we\n    // often have serializable anonymous DoFns nested within a\n    // PTransform.\n  }\n\n  /**\n   * Returns the default {@code Coder} to use for the output of this single-output {@code\n   * PTransform}.\n   *\n   * <p>By default, always throws\n   *\n   * @throws CannotProvideCoderException if no coder can be inferred\n   * @deprecated Instead, the PTransform should explicitly call {@link PCollection#setCoder} on the\n   *     returned PCollection.\n   */\n  @Deprecated\n  protected Coder<?> getDefaultOutputCoder() throws CannotProvideCoderException {\n    throw new CannotProvideCoderException(\"PTransform.getOutputCoder called.\");\n  }\n\n  /**\n   * Returns the default {@code Coder} to use for the output of this single-output {@code\n   * PTransform} when applied to the given input.\n   *\n   * <p>By default, always throws.\n   *\n   * @throws CannotProvideCoderException if none can be inferred.\n   * @deprecated Instead, the PTransform should explicitly call {@link PCollection#setCoder} on the\n   *     returned PCollection.\n   */\n  @Deprecated\n  protected Coder<?> getDefaultOutputCoder(@SuppressWarnings(\"unused\") InputT input)\n      throws CannotProvideCoderException {\n    return getDefaultOutputCoder();\n  }\n","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java#L313-L349","documentation":"PTransform.getDefaultOutputCoder() is a deprecated hook that was meant to be overridden to supply an output coder. The base implementation throws CannotProvideCoderException because the generic PTransform cannot infer a coder. If your transform neither overrides this nor sets a coder on its output PCollection, pipeline construction fails.","triggerScenarios":"Applying a custom PTransform that returns a PCollection without ever calling output.setCoder(...) and without overriding getDefaultOutputCoder, so Beam falls back to the deprecated base method.","commonSituations":"Custom transforms producing PCollection<Row>/POJOs where type erasure prevents inference; older tutorials still using the deprecated override; transforms whose output type is a raw or generic type.","solutions":["In expand(), explicitly set a coder: PCollection<O> out = input.apply(...); out.setCoder(MyCoder.of()); return out;","Override getDefaultOutputCoder(input) (the modern one-argument variant) to return a Coder for your output","Ensure the output type is a concrete, coder-inferrable class (avoid raw/generic types)"],"exampleFix":"// before\nreturn input.apply(\"t\", new MyTransform());\n// after\nPCollection<O> out = input.apply(\"t\", new MyTransform());\nout.setCoder(MyCoder.of());\nreturn out;","handlingStrategy":"validation","validationCode":"// Verify a coder is available before expanding\nCoder<?> c = null;\ntry {\n  java.lang.reflect.Method m = PTransform.class.getDeclaredMethod(\"getDefaultOutputCoder\", PCollection.class);\n  m.setAccessible(true);\n  c = (Coder<?>) m.invoke(transform, input);\n} catch (CannotProvideCoderException | ReflectiveOperationException ignored) { }\nif (c == null) { /* set coder explicitly on output */ }","typeGuard":null,"tryCatchPattern":"try {\n  PCollection<O> out = input.apply(transform);\n} catch (CannotProvideCoderException | IllegalStateException e) {\n  PCollection<O> out = input.apply(transform).setCoder(MyCoder.of());\n}","preventionTips":["Always call setCoder() on output PCollections in custom transforms","Avoid raw/generic output types that defeat coder inference","Migrate away from the deprecated getDefaultOutputCoder() override"],"tags":["java","apache-beam","coder"],"backgroundTag":"schema-validation-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"}