apache/beam · error · AttributeError

timestamp not accessible in this context

Error message

timestamp not accessible in this context

What it means

Same family as the element error: DoFnContext.timestamp raises AttributeError('timestamp not accessible in this context') when windowed_value is None, i.e. the current context has no element and hence no per-element timestamp.

Source

Thrown at sdks/python/apache_beam/runners/common.py:1946

    self.state = state
    if element is not None:
      self.set_element(element)

  def set_element(self, windowed_value):
    # type: (Optional[WindowedValue]) -> None
    self.windowed_value = windowed_value

  @property
  def element(self):
    if self.windowed_value is None:
      raise AttributeError('element not accessible in this context')
    else:
      return self.windowed_value.value

  @property
  def timestamp(self):
    if self.windowed_value is None:
      raise AttributeError('timestamp not accessible in this context')
    else:
      return self.windowed_value.timestamp

  @property
  def windows(self):
    if self.windowed_value is None:
      raise AttributeError('windows not accessible in this context')
    else:
      return self.windowed_value.windows

View on GitHub (pinned to 12126d8942)

Solutions

  1. Read timestamps inside process() only, e.g. via `timestamp=beam.DoFn.TimestampParam` or ctx.timestamp there.
  2. Capture the timestamp in process() and store it on the DoFn instance if finish_bundle needs it.
  3. In tests, construct the context with WindowedValue(value, timestamp, windows).

Example fix

// before
def finish_bundle(self):
    ts = self.timestamp  # AttributeError
// after
def process(self, element, ts=beam.DoFn.TimestampParam):
    self.last_ts = ts

def finish_bundle(self):
    ts = self.last_ts
Defensive patterns

Strategy: try-catch

Validate before calling

if getattr(ctx, 'windowed_value', None) is None:
    ts = None
else:
    ts = ctx.timestamp

Type guard

def has_timestamp(ctx):
    return getattr(ctx, 'windowed_value', None) is not None

Try / catch

try:
    ts = ctx.timestamp
except AttributeError as e:
    if 'timestamp not accessible' in str(e):
        ts = MIN_TIMESTAMP
    else:
        raise

Prevention

When it happens

Trigger: Accessing ctx.timestamp (or self.timestamp via context) in start_bundle/finish_bundle or any context constructed without a WindowedValue.

Common situations: Reading the current element timestamp during bundle lifecycle methods; relying on stale context state across process calls; hand-rolled DoFn invocation in tests without a windowed value.

Related errors


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