{"record":{"id":"28ac10a6b1d22303","repo":"apache/beam","slug":"cannot-encode-a-null-predictionresult","errorCode":null,"errorMessage":"cannot encode a null PredictionResult","messagePattern":"cannot encode a null PredictionResult","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/ml/inference/remote/src/main/java/org/apache/beam/sdk/ml/inference/remote/PredictionResultCoder.java","lineNumber":50,"sourceCode":"\n  private final Coder<InputT> inputCoder;\n  private final Coder<OutputT> outputCoder;\n\n  private PredictionResultCoder(Coder<InputT> inputCoder, Coder<OutputT> outputCoder) {\n    this.inputCoder = inputCoder;\n    this.outputCoder = outputCoder;\n  }\n\n  public static <InputT, OutputT> PredictionResultCoder<InputT, OutputT> of(\n      Coder<InputT> inputCoder, Coder<OutputT> outputCoder) {\n    return new PredictionResultCoder<>(inputCoder, outputCoder);\n  }\n\n  @Override\n  public void encode(PredictionResult<InputT, OutputT> value, OutputStream outStream)\n      throws CoderException, IOException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null PredictionResult\");\n    }\n    inputCoder.encode(value.getInput(), outStream);\n    outputCoder.encode(value.getOutput(), outStream);\n  }\n\n  @Override\n  public PredictionResult<InputT, OutputT> decode(InputStream inStream)\n      throws CoderException, IOException {\n    InputT input = inputCoder.decode(inStream);\n    OutputT output = outputCoder.decode(inStream);\n    return PredictionResult.create(input, output);\n  }\n\n  @Override\n  public List<? extends Coder<?>> getCoderArguments() {\n    return Arrays.asList(inputCoder, outputCoder);\n  }\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/ml/inference/remote/src/main/java/org/apache/beam/sdk/ml/inference/remote/PredictionResultCoder.java#L32-L68","documentation":"PredictionResultCoder.encode() requires a non-null PredictionResult because a coder must write a deterministic byte stream; null values are not encodable in Beam coders. Encoding a null raises CoderException with this message.","triggerScenarios":"A PCollection of PredictionResult elements contains a null element (e.g. a DoFn emitted null, or a handler returned null) and the pipeline attempts to serialize it for shuffle, GBK, or sink writing.","commonSituations":"Custom inference DoFns that emit null on failed requests, joins/CoGroup producing null values, and test harnesses calling coder.encode(null, ...) directly.","solutions":["Filter out or replace null PredictionResult elements before they hit a coder boundary (e.g. Filter.notNull() or Map to an error record).","Fix the upstream DoFn/handler so it never emits null PredictionResults.","In unit tests, assert non-null before calling encode."],"exampleFix":"// before\nresults.add(null); // failed request\n// after\nif (result != null) results.add(result);","handlingStrategy":"type-guard","validationCode":"pcollection.apply(Filter.by(Objects::nonNull));","typeGuard":"boolean isEncodable(PredictionResult<?, ?> r) { return r != null; }","tryCatchPattern":"try { coder.encode(result, outStream); } catch (CoderException e) { /* handle null/undecodable element before shuffle */ }","preventionTips":["Never emit null from DoFns producing PredictionResults","Use Filter.notNull() before shuffles/sinks","Map failures to explicit error records instead of null"],"tags":["java","beam","coder","null"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}