apache/beam · error · AttributeError

element not accessible in this context

Error message

element not accessible in this context

What it means

DoFnContext.element raises AttributeError('element not accessible in this context') when the context's windowed_value is None. Certain context types (e.g. start_bundle/finish_bundle contexts, or user-side/timing contexts) have no current element, so reading .element is invalid.

Source

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


# TODO(robertwb): Replace core.DoFnContext with this.
class DoFnContext(object):
  """For internal use only; no backwards-compatibility guarantees."""
  def __init__(self, label, element=None, state=None):
    self.label = label
    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. Only access element inside process() where an element is in scope; pass the element as a process parameter instead.
  2. For setup/teardown, use bundle-local state initialized in start_bundle without referencing the element.
  3. In tests, ensure the context is constructed with a WindowedValue before accessing .element.

Example fix

// before
class MyDoFn(beam.DoFn):
    def start_bundle(self):
        self.last = self.element  # AttributeError
// after
class MyDoFn(beam.DoFn):
    def process(self, element):
        self.last = element  # element in scope here
Defensive patterns

Strategy: try-catch

Validate before calling

if getattr(ctx, 'windowed_value', None) is None:
    raise SkipElement('no element in this context')

Type guard

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

Try / catch

try:
    elem = ctx.element
except AttributeError as e:
    if 'element not accessible' in str(e):
        elem = None
    else:
        raise

Prevention

When it happens

Trigger: Accessing `self.element` or ctx.element inside start_bundle/finish_bundle, or in a context created with windowed_value=None (e.g. in tests or setup methods).

Common situations: Trying to read the current element during bundle setup/teardown; caching context accessors outside of process(); custom runner/DoFnInvoker usage without passing a windowed value.

Related errors


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