{"record":{"id":"b352e8fd8a98072a","repo":"apache/beam","slug":"cannot-call-sideinput-in-a-window-only-context","errorCode":null,"errorMessage":"cannot call sideInput() in a window-only context","messagePattern":"cannot call sideInput\\(\\) in a window-only context","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateContexts.java","lineNumber":71,"sourceCode":"    return new WindowOnlyContext<>(window);\n  }\n\n  private static class WindowOnlyContext<W extends BoundedWindow> implements StateContext<W> {\n    private final W window;\n\n    private WindowOnlyContext(W window) {\n      this.window = window;\n    }\n\n    @Override\n    public PipelineOptions getPipelineOptions() {\n      throw new IllegalArgumentException(\n          \"cannot call getPipelineOptions() in a window-only context\");\n    }\n\n    @Override\n    public <T> T sideInput(PCollectionView<T> view) {\n      throw new IllegalArgumentException(\"cannot call sideInput() in a window-only context\");\n    }\n\n    @Override\n    public W window() {\n      return window;\n    }\n  }\n}\n","sourceCodeStart":53,"sourceCodeEnd":80,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateContexts.java#L53-L80","documentation":"The window-only StateContext returned by StateContexts.windowOnly() carries only a window and no pipeline graph, so sideInput(PCollectionView) cannot be resolved. Beam overrides sideInput() to throw IllegalArgumentException to prevent accessing side inputs where they are unavailable. Side inputs require a full evaluation context that a window-only context does not have.","triggerScenarios":"Calling sideInput(view) on a StateContext that was created as a window-only context, e.g. inside a state or timer callback resolved against StateContexts.windowOnly(window), or any code path where the view's data cannot be looked up.","commonSituations":"Attempting to read a PCollectionView from within an onTimer or state callback in a context that lacks the side-input map; refactored helper methods reused across contexts; tests constructing windowOnly contexts.","solutions":["Only access side inputs via the DoFn's process-context (@AlwaysFetchSideInputs / parameter access), not from state contexts.","Pass the side input value into the state callback explicitly as a parameter captured during processElement.","Guard the call in try-catch for IllegalArgumentException and fall back to a default value."],"exampleFix":"// before\nT value = stateContext.sideInput(view);\n\n// after\nT value = this.capturedSideInput; // read in processElement via side input parameter","handlingStrategy":"validation","validationCode":"// Only access side inputs through ProcessContext parameters in @ProcessElement methods,\n// never via a StateContext obtained from StateContexts.windowOnly(...) or onTimer contexts","typeGuard":"if (stateContext instanceof StateContexts.WindowOnlyStateContext) {\n  throw new IllegalStateException(\"sideInput() unsupported in window-only context\");\n}","tryCatchPattern":"try {\n  value = stateContext.sideInput(view);\n} catch (IllegalArgumentException e) {\n  value = defaultValue;\n}","preventionTips":["Fetch side inputs via DoFn process-element side-input parameters and pass values into state logic explicitly","Never call sideInput() from onTimer or readState callbacks","Keep side-input reads in one layer of the pipeline to avoid leaking them into state contexts"],"tags":["apache-beam","state-api","side-input"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}