apache/beam · error · UnsupportedOperationException

Cannot access state in non-window observing context.

Error message

Cannot access state in non-window observing context.

What it means

NonWindowObservingProcessBundleContextBase.state(String, boolean) throws UnsupportedOperationException because state access (StateCell/ValueState etc.) is keyed by window, and this context serves a method that is not window-observing. Without a bound window the runner cannot address the state cell.

Source

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

      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(
          "Cannot access state in non-window observing context.");
    }

    @Override
    public org.apache.beam.sdk.state.Timer timer(String timerId) {
      throw new UnsupportedOperationException(
          "Cannot access timer in non-window observing context.");
    }

    @Override
    public TimerMap timerFamily(String timerFamilyId) {
      throw new UnsupportedOperationException(
          "Cannot access timerFamily in non-window observing context.");
    }
  }

  /** Base implementation that does not override methods which need to be window aware. */
  private abstract class ProcessBundleContextBase extends DoFn<InputT, OutputT>.ProcessContext

View on GitHub (pinned to 12126d8942)

Solutions

  1. Add a BoundedWindow parameter to the @ProcessElement method so state access is permitted via the window-observing context.
  2. Only call state() from @ProcessElement on a keyed PCollection (KV input) — move any bundle-level logic to ordinary local fields.
  3. Verify the state id is declared via @StateId on the DoFn; undeclared ids will also fail at validation time.

Example fix

// before
@ProcessElement
public void processElement(ProcessContext c) {
  ValueState<Integer> s = c.state(seenId, ...); // throws
}

// after
@ProcessElement
public void processElement(ProcessContext c, BoundedWindow window) {
  ValueState<Integer> s = c.state(seenId);
}
Defensive patterns

Strategy: validation

Validate before calling

// State access requires a window-observing, keyed @ProcessElement:
boolean stateReady = Arrays.stream(dofn.getClass().getDeclaredMethods())
    .filter(m -> m.isAnnotationPresent(ProcessElement.class))
    .allMatch(m -> Arrays.asList(m.getParameterTypes()).contains(BoundedWindow.class));
// plus: input must be PCollection<KV<K, V>> (keyed).

Type guard

boolean canUseState(DoFn<?> doFn) {
  return Arrays.stream(doFn.getClass().getDeclaredMethods())
      .filter(m -> m.isAnnotationPresent(ProcessElement.class))
      .anyMatch(m -> Arrays.asList(m.getParameterTypes()).contains(BoundedWindow.class));
}

Try / catch

try {
  ValueState<Integer> s = c.state(seenId);
} catch (UnsupportedOperationException e) {
  throw new IllegalStateException("state() needs a window-observing @ProcessElement on a keyed PCollection", e);
}

Prevention

When it happens

Trigger: Calling c.state(stateId, ...) from a @ProcessElement method without a BoundedWindow parameter on a keyed DoFn, or from @StartBundle/@FinishBundle.

Common situations: Adding stateful logic (dedup caches, counters) to an existing DoFn without updating the method signature; forgetting that state APIs also require @ProcessElement context, not bundle lifecycle context; running a stateful DoFn where the runner picked the non-window-observing invoker.

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