apache/beam · error · UnsupportedOperationException

Not expected to access DoFn.FinishBundleContext from @Proces

Error message

Not expected to access DoFn.FinishBundleContext from @ProcessElement

What it means

DoFnTester's ProcessContext cannot supply a FinishBundleContext: FinishBundleContext is only meaningful inside @FinishBundle. If a @ProcessElement method requests it as a parameter, the tester throws UnsupportedOperationException.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/DoFnTester.java:294

              return processContext.pane();
            }

            @Override
            public PipelineOptions pipelineOptions() {
              return getPipelineOptions();
            }

            @Override
            public DoFn<InputT, OutputT>.StartBundleContext startBundleContext(
                DoFn<InputT, OutputT> doFn) {
              throw new UnsupportedOperationException(
                  "Not expected to access DoFn.StartBundleContext from @ProcessElement");
            }

            @Override
            public DoFn<InputT, OutputT>.FinishBundleContext finishBundleContext(
                DoFn<InputT, OutputT> doFn) {
              throw new UnsupportedOperationException(
                  "Not expected to access DoFn.FinishBundleContext from @ProcessElement");
            }

            @Override
            public DoFn<InputT, OutputT>.ProcessContext processContext(DoFn<InputT, OutputT> doFn) {
              return processContext;
            }

            @Override
            public InputT element(DoFn<InputT, OutputT> doFn) {
              return processContext.element();
            }

            @Override
            public Object key() {
              throw new UnsupportedOperationException(
                  "Cannot access key as parameter outside of @OnTimer method.");
            }

View on GitHub (pinned to 12126d8942)

Solutions

  1. Move FinishBundleContext usage into a @FinishBundle method; do not request it in @ProcessElement
  2. Collect per-element outputs in instance state and emit in @FinishBundle instead
  3. Use TestPipeline integration tests where full lifecycle contexts are provided correctly

Example fix

// before
@ProcessElement public void processElement(ProcessContext c, FinishBundleContext fbc) {...}
// after
@ProcessElement public void processElement(ProcessContext c) {...}
@FinishBundle public void finishBundle(FinishBundleContext c) {...}
Defensive patterns

Strategy: type-guard

Type guard

// FinishBundleContext is only valid in @FinishBundle methods
boolean inFinishBundle = method.isAnnotationPresent(FinishBundle.class);

Try / catch

try {
  tester.processElement();
} catch (UnsupportedOperationException e) {
  // FinishBundleContext requested in @ProcessElement
}

Prevention

When it happens

Trigger: A @ProcessElement method with a DoFn.FinishBundleContext parameter executed via DoFnTester.processElement.

Common situations: DoFns that emit per-bundle aggregates from within processElement using finish bundle context; legacy code ported from other frameworks being unit tested with DoFnTester.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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