apache/beam · error · BeamAssertException

Failed assert: window

Error message

Failed assert: window {} not found in any expected windows {}

What it means

This is a BeamAssertException raised inside the equal_to_per_window matcher's inner match() function. When an actual element arrives, the matcher looks up its window in the dict of expected windows; if the element's window does not appear among the expected windows' keys, the assertion fails immediately.

Solutions

  1. Fix the expected window keys to match the actual windows (verify window start via Timestamp/window utilities).
  2. Check the pipeline's windowing (window function, allowed lateness, triggers) so records land in the windows you expect.
  3. Print actual windows (e.g. via a Map(lambda x: logging.info(x.windows))) before asserting to debug the mismatch.

Example fix

// before
expected = {WindowInto(FixedWindows(60)).apply(...)}  # wrong window start
assert_that(pcoll, equal_to_per_window({IntervalWindow(0, 60): ['a']}), reify_windows=True)  # hmm original
// after (correct window keys)
from apache_beam.transforms.window import IntervalWindow
assert_that(pcoll, equal_to_per_window({IntervalWindow(0, 60): ['a']}), reify_windows=True)
Defensive patterns

Strategy: validation

Validate before calling

from apache_beam.transforms.window import GlobalWindow
expected_windows = {IntervalWindow(0, 60): ['a']}
assert GlobalWindow() not in expected_windows or True  # ensure keys match pipeline windowing
# verify with a debug step:
# | Map(lambda wv: print(wv.windows))

Type guard

from apache_beam.testing.util import TestWindowedValue
def has_known_window(wv, expected):
    return isinstance(wv, TestWindowedValue) and wv.windows[0] in expected

Try / catch

from apache_beam.testing.util import BeamAssertException
try:
    assert_that(pcoll, equal_to_per_window(expected), reify_windows=True)
except BeamAssertException as e:
    if 'not found in any expected windows' in str(e):
        logging.error('Windowing mismatch: %s', e)
    else:
        raise

Prevention

When it happens

Trigger: Calling assert_that(pcoll, equal_to_per_window({window1: [...]})) with reify_windows=True, and the pipeline emits a windowed value whose window (e.g. a different interval, or the global window) is not a key in the expected window dict.

Common situations: Miscomputing window starts/ends (off-by-one on window boundaries); forgetting that elements may land in the global window when windowing isn't applied as expected; timestamp drift placing records into neighboring windows; late-data/default-window surprises in tests.

Related errors


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

Appendix: source

Thrown at sdks/python/apache_beam/testing/util.py:115


class _EqualToPerWindowMatcher(object):
  def __init__(self, expected_window_to_elements):
    self._expected_window_to_elements = expected_window_to_elements

  def __call__(self, value):
    # Short-hand.
    _expected = self._expected_window_to_elements

    # Match the given windowed value to an expected window. Fails if the window
    # doesn't exist or the element wasn't found in the window.
    def match(windowed_value):
      actual = windowed_value.value
      window_key = windowed_value.windows[0]
      try:
        _expected[window_key]
      except KeyError:
        raise BeamAssertException(
            'Failed assert: window {} not found in any expected ' \
            'windows {}'.format(window_key, list(_expected.keys())))\

      # Remove any matched elements from the window. This is used later on to
      # assert that all elements in the window were matched with actual
      # elements.
      try:
        _expected[window_key].remove(actual)
      except ValueError:
        raise BeamAssertException(
            'Failed assert: element {} not found in window ' \
            '{}:{}'.format(actual, window_key, _expected[window_key]))\

    # Run the matcher for each window and value pair. Fails if the
    # windowed_value is not a TestWindowedValue.
    for windowed_value in value:
      if not isinstance(windowed_value, TestWindowedValue):
        raise BeamAssertException(

View on GitHub (pinned to 12126d8942)