{"record":{"id":"60ebb77abc9b2d03","repo":"apache/beam","slug":"no-existing-windows-in-this-context","errorCode":null,"errorMessage":"No existing_windows in this context.","messagePattern":"No existing_windows in this context\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/runners/common.py","lineNumber":1905,"sourceCode":"\nclass _NoContext(WindowFn.AssignContext):\n  \"\"\"An uninspectable WindowFn.AssignContext.\"\"\"\n  NO_VALUE = object()\n\n  def __init__(self, value, timestamp=NO_VALUE):\n    self.value = value\n    self._timestamp = timestamp\n\n  @property\n  def timestamp(self):\n    if self._timestamp is self.NO_VALUE:\n      raise ValueError('No timestamp in this context.')\n    else:\n      return self._timestamp\n\n  @property\n  def existing_windows(self):\n    raise ValueError('No existing_windows in this context.')\n\n\nclass DoFnState(object):\n  \"\"\"For internal use only; no backwards-compatibility guarantees.\n\n  Keeps track of state that DoFns want, currently, user counters.\n  \"\"\"\n  def __init__(self, counter_factory):\n    self.step_name = ''\n    self._counter_factory = counter_factory\n\n  def counter_for(self, aggregator):\n    \"\"\"Looks up the counter for this aggregator, creating one if necessary.\"\"\"\n    return self._counter_factory.get_aggregator_counter(\n        self.step_name, aggregator)\n\n\n# TODO(robertwb): Replace core.DoFnContext with this.","sourceCodeStart":1887,"sourceCodeEnd":1923,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/runners/common.py#L1887-L1923","documentation":"The sibling of the timestamp check: ProcessContext.existing_windows unconditionally raises ValueError('No existing_windows in this context.') because this context type never carries window information. Any code path that asks for windows through this context is invalid by construction.","triggerScenarios":"Accessing `ctx.existing_windows` on a ProcessContext (e.g. inside a DoFn's process via the process context or in tests) when the runner-provided context does not expose windows.","commonSituations":"Trying to read input windows in a DoFn without the proper context (e.g. via DoFnTester or a plain Context rather than the windowed receiver); assuming windows are always available in ctx.","solutions":["Access windows through the correct mechanism: declare the DoFn with the windows-related context (e.g. use the `window` DoFn parameter via beam.DoFn.ProcessContext or @beam.DoFn.window_param-style access / process(ctx) with windowed value).","In tests, wrap inputs in WindowedValue so window info is available through the runner path.","Refactor to not need input windows; use beam.WindowInto or window-aware transforms instead."],"exampleFix":"// before\nclass MyDoFn(beam.DoFn):\n    def process(self, ctx):\n        print(ctx.existing_windows)  # always raises\n// after\nclass MyDoFn(beam.DoFn):\n    def process(self, element, window=beam.DoFn.WindowParam):\n        print(window)  # proper window access","handlingStrategy":"type-guard","validationCode":"if not isinstance(ctx, type(None)) and getattr(ctx, 'existing_windows', None) is None:\n    skip_window_logic()","typeGuard":"def has_windows(ctx):\n    try:\n        _ = ctx.existing_windows\n        return True\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    windows = ctx.existing_windows\nexcept ValueError:\n    windows = [GlobalWindow()]  # conservative default\n    log.warning('Window info unavailable; assuming global window')","preventionTips":["Access windows via beam.DoFn.WindowParam or the proper context, not ProcessContext.existing_windows.","Never assume window info is present in test harnesses; wrap inputs in WindowedValue.","Design DoFns that need windows to request them explicitly via parameters."],"tags":["python","apache-beam","process-context","windows","value-error"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}