apache/beam · error · BeamAssertException
Failed assert: Received element
Error message
Failed assert: Received element {} is not of type TestWindowedValue. Did you forget to set reify_windows=True on the assertion? What it means
equal_to_per_window requires the asserted PCollection to contain TestWindowedValue objects (i.e., windows reified into the elements). Its __call__ type-checks each received item; plain values mean windows were not reified, so per-window matching is impossible, and BeamAssertException is raised with a hint to set reify_windows=True.
Solutions
- Pass reify_windows=True to assert_that: assert_that(pcoll, equal_to_per_window(expected), reify_windows=True).
- Ensure nothing between the pipeline and the assertion discards windowing information.
- If per-window assertions aren't needed, use plain equal_to instead.
Example fix
// before
assert_that(pcoll, equal_to_per_window({win: ['a']}))
// after
assert_that(pcoll, equal_to_per_window({win: ['a']}), reify_windows=True) Defensive patterns
Strategy: type-guard
Validate before calling
# ensure the assertion is wired with reified windows assert_that(pcoll, equal_to_per_window(expected), reify_windows=True)
Type guard
from apache_beam.testing.util import TestWindowedValue
def is_test_windowed_value(x) -> bool:
return isinstance(x, TestWindowedValue) 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 of type TestWindowedValue' in str(e):
logging.error('Add reify_windows=True: %s', e)
else:
raise Prevention
- Always pass reify_windows=True with equal_to_per_window
- Never insert a Map that unwraps windowed values before a per-window assertion
- Review assertion helpers' signatures for required flags
When it happens
Trigger: Calling assert_that(pcoll, equal_to_per_window({...})) WITHOUT reify_windows=True; the assertion receives plain elements (or non-TestWindowedValue objects) and fails on the first one.
Common situations: Copy-pasting an ordinary assert_that/pcol equal_to assertion and swapping in equal_to_per_window without adding the reify_windows flag; using a runner/DoFn that strips window information before the assertion.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Failed assert: element
- Failed assert: unmatched elements
- Failed assert: window
- Failed assert: [] == %r
- Failed assert: %r == %r
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/8e47da2fd04eca2a.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/python/apache_beam/testing/util.py:133
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(
'Failed assert: Received element {} is not of type ' \
'TestWindowedValue. Did you forget to set reify_windows=True ' \
'on the assertion?'.format(windowed_value))
match(windowed_value)
# Finally, some elements may not have been matched. Assert that we removed
# all the elements that we received from the expected list. If the list is
# non-empty, then there are unmatched elements.
for win in _expected:
if _expected[win]:
raise BeamAssertException(
'Failed assert: unmatched elements {} in window {}'.format(
_expected[win], win))
def equal_to_per_window(expected_window_to_elements):
"""Matcher used by assert_that to check to assert expected windows.
View on GitHub (pinned to 12126d8942)