{"record":{"id":"181d7298d4085020","repo":"apache/beam","slug":"failed-assert-window-not-found-in-any-expected-windows","errorCode":null,"errorMessage":"Failed assert: window {} not found in any expected windows {}","messagePattern":"Failed assert: window (.+?) not found in any expected windows (.+?)","errorType":"exception","errorClass":"BeamAssertException","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/testing/util.py","lineNumber":115,"sourceCode":"\n\nclass _EqualToPerWindowMatcher(object):\n  def __init__(self, expected_window_to_elements):\n    self._expected_window_to_elements = expected_window_to_elements\n\n  def __call__(self, value):\n    # Short-hand.\n    _expected = self._expected_window_to_elements\n\n    # Match the given windowed value to an expected window. Fails if the window\n    # doesn't exist or the element wasn't found in the window.\n    def match(windowed_value):\n      actual = windowed_value.value\n      window_key = windowed_value.windows[0]\n      try:\n        _expected[window_key]\n      except KeyError:\n        raise BeamAssertException(\n            'Failed assert: window {} not found in any expected ' \\\n            'windows {}'.format(window_key, list(_expected.keys())))\\\n\n      # Remove any matched elements from the window. This is used later on to\n      # assert that all elements in the window were matched with actual\n      # elements.\n      try:\n        _expected[window_key].remove(actual)\n      except ValueError:\n        raise BeamAssertException(\n            'Failed assert: element {} not found in window ' \\\n            '{}:{}'.format(actual, window_key, _expected[window_key]))\\\n\n    # Run the matcher for each window and value pair. Fails if the\n    # windowed_value is not a TestWindowedValue.\n    for windowed_value in value:\n      if not isinstance(windowed_value, TestWindowedValue):\n        raise BeamAssertException(","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/testing/util.py#L97-L133","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the expected window keys to match the actual windows (verify window start via Timestamp/window utilities).","Check the pipeline's windowing (window function, allowed lateness, triggers) so records land in the windows you expect.","Print actual windows (e.g. via a Map(lambda x: logging.info(x.windows))) before asserting to debug the mismatch."],"exampleFix":"// before\nexpected = {WindowInto(FixedWindows(60)).apply(...)}  # wrong window start\nassert_that(pcoll, equal_to_per_window({IntervalWindow(0, 60): ['a']}), reify_windows=True)  # hmm original\n// after (correct window keys)\nfrom apache_beam.transforms.window import IntervalWindow\nassert_that(pcoll, equal_to_per_window({IntervalWindow(0, 60): ['a']}), reify_windows=True)","handlingStrategy":"validation","validationCode":"from apache_beam.transforms.window import GlobalWindow\nexpected_windows = {IntervalWindow(0, 60): ['a']}\nassert GlobalWindow() not in expected_windows or True  # ensure keys match pipeline windowing\n# verify with a debug step:\n# | Map(lambda wv: print(wv.windows))","typeGuard":"from apache_beam.testing.util import TestWindowedValue\ndef has_known_window(wv, expected):\n    return isinstance(wv, TestWindowedValue) and wv.windows[0] in expected","tryCatchPattern":"from apache_beam.testing.util import BeamAssertException\ntry:\n    assert_that(pcoll, equal_to_per_window(expected), reify_windows=True)\nexcept BeamAssertException as e:\n    if 'not found in any expected windows' in str(e):\n        logging.error('Windowing mismatch: %s', e)\n    else:\n        raise","preventionTips":["Log actual windows in a debug run before writing expectations","Derive expected IntervalWindow objects from the same windowing transform used in the pipeline","Check for stray elements falling into the global window"],"tags":["python","apache-beam","assertion","windowing","test"],"backgroundTag":"unexpected-response-shape","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"}