apache/beam · error · UnsupportedOperationException

Cannot access window in non-window observing context.

Error message

Cannot access window in non-window observing context.

What it means

The FnApiDoFnRunner's NonWindowObservingProcessBundleContextBase.window() throws UnsupportedOperationException because the current DoFn method was not declared window-observing (@ProcessElement with a BoundedWindow parameter), so no window is bound at invocation time. Calling c.window() from such a context is a programming error: the window argument simply is not available there.

Source

Thrown at sdks/java/harness/src/main/java/org/apache/beam/fn/harness/FnApiDoFnRunner.java:1966

    }

    @Override
    public CausedByDrain causedByDrain() {
      return currentElement.causedByDrain();
    }

    @Override
    public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
      return currentElement.causedByDrain();
    }
  }

  /** Provides base arguments for a {@link DoFnInvoker} for a non-window observing method. */
  private abstract class NonWindowObservingProcessBundleContextBase
      extends ProcessBundleContextBase {
    @Override
    public BoundedWindow window() {
      throw new UnsupportedOperationException(
          "Cannot access window in non-window observing context.");
    }

    @Override
    public Object sideInput(String tagId) {
      throw new UnsupportedOperationException(
          "Cannot access sideInput in non-window observing context.");
    }

    @Override
    public <T> T sideInput(PCollectionView<T> view) {
      throw new UnsupportedOperationException(
          "Cannot access sideInput in non-window observing context.");
    }

    @Override
    public State state(String stateId, boolean alwaysFetched) {
      throw new UnsupportedOperationException(

View on GitHub (pinned to 12126d8942)

Solutions

  1. Add a BoundedWindow parameter to the @ProcessElement method signature so Beam makes it window-observing, then use that parameter instead of context.window().
  2. If you need the window in bundle lifecycle methods, restructure to capture per-element windows in @ProcessElement and store them in state or an accumulator.
  3. Confirm the DoFn does not run with a context base that intentionally rejects window access (non-window-observing methods).

Example fix

// before
@ProcessElement
public void processElement(ProcessContext c) {
  BoundedWindow w = c.window(); // throws
}

// after
@ProcessElement
public void processElement(ProcessContext c, BoundedWindow window) {
  // use window directly
}
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the @ProcessElement signature includes a BoundedWindow before calling c.window():
// Check via reflection at startup:
boolean windowObserving = Arrays.stream(dofn.getClass().getDeclaredMethods())
    .filter(m -> m.isAnnotationPresent(ProcessElement.class))
    .allMatch(m -> Arrays.stream(m.getParameterTypes()).anyMatch(BoundedWindow.class::isAssignableFrom));

Type guard

interface WindowAwareContext { BoundedWindow window(); }
BoundedWindow safeWindow(DoFn.ProcessContext ctx) {
  return ctx instanceof WindowAwareContext ? ((WindowAwareContext) ctx).window() : null;
}

Try / catch

try {
  BoundedWindow w = c.window();
  useWindow(w);
} catch (UnsupportedOperationException e) {
  // not a window-observing context; add BoundedWindow param to @ProcessElement
  throw new IllegalStateException("Add BoundedWindow parameter to @ProcessElement", e);
}

Prevention

When it happens

Trigger: Calling ProcessContext.window() from a @ProcessElement method whose signature lacks a BoundedWindow parameter, or from a @StartBundle/@FinishBundle method, when the runner supplies the non-window-observing invoker context.

Common situations: Developers upgrading a DoFn and adding window() calls without changing the method signature; copy-pasting windowed logic into a non-windowed DoFn; accessing window in StartBundle/FinishBundle cleanup code.

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/c615ddd0763b89af. Report an issue: GitHub.