apache/beam · error · CoderException

cannot encode a null PredictionResult

Error message

cannot encode a null PredictionResult

What it means

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.

Source

Thrown at sdks/java/ml/inference/remote/src/main/java/org/apache/beam/sdk/ml/inference/remote/PredictionResultCoder.java:50

  private final Coder<InputT> inputCoder;
  private final Coder<OutputT> outputCoder;

  private PredictionResultCoder(Coder<InputT> inputCoder, Coder<OutputT> outputCoder) {
    this.inputCoder = inputCoder;
    this.outputCoder = outputCoder;
  }

  public static <InputT, OutputT> PredictionResultCoder<InputT, OutputT> of(
      Coder<InputT> inputCoder, Coder<OutputT> outputCoder) {
    return new PredictionResultCoder<>(inputCoder, outputCoder);
  }

  @Override
  public void encode(PredictionResult<InputT, OutputT> value, OutputStream outStream)
      throws CoderException, IOException {
    if (value == null) {
      throw new CoderException("cannot encode a null PredictionResult");
    }
    inputCoder.encode(value.getInput(), outStream);
    outputCoder.encode(value.getOutput(), outStream);
  }

  @Override
  public PredictionResult<InputT, OutputT> decode(InputStream inStream)
      throws CoderException, IOException {
    InputT input = inputCoder.decode(inStream);
    OutputT output = outputCoder.decode(inStream);
    return PredictionResult.create(input, output);
  }

  @Override
  public List<? extends Coder<?>> getCoderArguments() {
    return Arrays.asList(inputCoder, outputCoder);
  }

View on GitHub (pinned to 12126d8942)

Solutions

  1. Filter out or replace null PredictionResult elements before they hit a coder boundary (e.g. Filter.notNull() or Map to an error record).
  2. Fix the upstream DoFn/handler so it never emits null PredictionResults.
  3. In unit tests, assert non-null before calling encode.

Example fix

// before
results.add(null); // failed request
// after
if (result != null) results.add(result);
Defensive patterns

Strategy: type-guard

Validate before calling

pcollection.apply(Filter.by(Objects::nonNull));

Type guard

boolean isEncodable(PredictionResult<?, ?> r) { return r != null; }

Try / catch

try { coder.encode(result, outStream); } catch (CoderException e) { /* handle null/undecodable element before shuffle */ }

Prevention

When it happens

Trigger: 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.

Common situations: Custom inference DoFns that emit null on failed requests, joins/CoGroup producing null values, and test harnesses calling coder.encode(null, ...) directly.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/28ac10a6b1d22303. Report an issue: GitHub.