{"record":{"id":"5ba680bd261d8cb0","repo":"apache/beam","slug":"failed-assert-element-not-found-in-window","errorCode":null,"errorMessage":"Failed assert: element {} not found in window {}:{}","messagePattern":"Failed assert: element (.+?) not found in window (.+?):(.+?)","errorType":"exception","errorClass":"BeamAssertException","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/testing/util.py","lineNumber":125,"sourceCode":"    # 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(\n            'Failed assert: Received element {} is not of type ' \\\n            'TestWindowedValue. Did you forget to set reify_windows=True ' \\\n            'on the assertion?'.format(windowed_value))\n      match(windowed_value)\n\n    # Finally, some elements may not have been matched. Assert that we removed\n    # all the elements that we received from the expected list. If the list is\n    # non-empty, then there are unmatched elements.\n    for win in _expected:\n      if _expected[win]:","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/testing/util.py#L107-L143","documentation":"In equal_to_per_window's match(), after the window is found, the matcher removes the actual element from the expected list for that window via list.remove(). If the element's value is not present in that window's expected list, remove() raises ValueError, which is converted to BeamAssertException naming the missing element, its window, and the expected contents.","triggerScenarios":"An actual element arrives in a known window but its value differs from every expected value in that window's list (e.g. expected ['a'] but the pipeline produced 'b', or a different timestamped encoding/None vs value).","commonSituations":"Expected/actual value mismatches from transformations in the pipeline under test (e.g. forgetting a map step); float precision differences; comparing encoded vs decoded values; wrong expected payload for one window while others match.","solutions":["Correct the expected element list for that window to match the pipeline's actual output.","Debug why the pipeline emits a different value (inspect the transform producing it).","Compare with exact types (str vs bytes, int vs float) — Python equality is type-sensitive in remove()."],"exampleFix":"// before\nassert_that(pcoll, equal_to_per_window({win: ['a']}), reify_windows=True)  # pipeline emits 'b'\n// after\nassert_that(pcoll, equal_to_per_window({win: ['b']}), reify_windows=True)","handlingStrategy":"validation","validationCode":"# sanity-check expected contents against a dry run of the transform logic\nfor win, elems in expected.items():\n    assert all(isinstance(e, str) for e in elems), 'type mismatch in expected elements'","typeGuard":"def window_has_element(wv, expected) -> bool:\n    return wv.value in expected.get(wv.windows[0], [])","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 window' in str(e):\n        logging.error('Element mismatch: %s', e)\n    else:\n        raise","preventionTips":["Compare exact types (str vs bytes) between expected and produced values","Generate expectations from the same transformation logic used in production code","Add a Map(logging.info) before assert_that when debugging"],"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"}