{"record":{"id":"f0fecac8c140063b","repo":"apache/beam","slug":"assign-context-window-should-not-be-none-this-might-be-due","errorCode":null,"errorMessage":"assign_context.window should not be None. This might be due to a DoFn returning a TimestampedValue.","messagePattern":"assign_context\\.window should not be None\\. This might be due to a DoFn returning a TimestampedValue\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/util.py","lineNumber":1442,"sourceCode":"  Will raise an exception when used after DoFns that return TimestampedValue\n  elements.\n  \"\"\"\n  def __init__(self, window_coder):\n    \"\"\"Create a new WindowFn with compatible coder.\n    To be applied to PCollections with windows that are compatible with the\n    given coder.\n\n    Arguments:\n      window_coder: coders.Coder object to be used on windows.\n    \"\"\"\n    super().__init__()\n    if window_coder is None:\n      raise ValueError('window_coder should not be None')\n    self._window_coder = window_coder\n\n  def assign(self, assign_context):\n    if assign_context.window is None:\n      raise ValueError(\n          'assign_context.window should not be None. '\n          'This might be due to a DoFn returning a TimestampedValue.')\n    return [assign_context.window]\n\n  def get_window_coder(self):\n    return self._window_coder\n\n\ndef reify_metadata_default_window(\n    element, timestamp=DoFn.TimestampParam, pane_info=DoFn.PaneInfoParam):\n  key, value = element\n  if timestamp == window.MIN_TIMESTAMP:\n    timestamp = None\n  return key, (value, timestamp, pane_info)\n\n\ndef restore_metadata_default_window(element):\n  key, values = element","sourceCodeStart":1424,"sourceCodeEnd":1460,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/util.py#L1424-L1460","documentation":"The identity window-assigning WindowFn's assign() expects an AssignContext whose window is set; a None window raises ValueError hinting the cause is usually a DoFn returning a TimestampedValue, which strips/re-creates windowing info instead of preserving the window. This surfaces as a runtime failure during window reassignment.","triggerScenarios":"A user DoFn emits beam.TimestampedValue(value, ts) while the pipeline applies window reassignment; the resulting element has no window object, so assign() gets assign_context.window=None and raises.","commonSituations":"Adding timestamps inside a DoFn via TimestampedValue under windowed/reassignment pipelines; mixing unwindowed and windowed PCollections; converting PCollection elements in ways that reset windows.","solutions":["Emit elements with windows instead: return beam.window.TimestampedValue within a window-preserving DoFn, or use beam.WindowInto with an explicit WindowFn","Use beam.pvalue.TaggedOutput with attributes, or attach timestamps via a Map before windowing so windows persist","If using GroupIntoBatches/reassignment, emit plain values and let the WindowInto assign timestamps via context","Restructure the DoFn to return windowed values, e.g. beam.window.WindowedValue(...) rather than TimestampedValue(...)"],"exampleFix":"// before\ndef process(self, element):\n  yield beam.TimestampedValue(element, timestamp)\n// after\ndef process(self, element, window=beam.DoFn.WindowParam, timestamp=beam.DoFn.TimestampParam):\n  yield beam.window.WindowedValue(element, timestamp, [window])","handlingStrategy":"try-catch","validationCode":"if isinstance(out, TimestampedValue):\n    raise TypeError('DoFn must yield windowed values, not TimestampedValue')","typeGuard":"def is_windowed(v): return hasattr(v, 'windows') and v.windows","tryCatchPattern":"try:\n    result = pcoll | util.GroupIntoBatches(...)\nexcept ValueError as e:\n    if 'assign_context.window' in str(e):\n        fix_dofn_to_emit_windowed_values()","preventionTips":["Never yield TimestampedValue from DoFns feeding window-reassignment transforms","Use WindowedValue or window params to preserve windows","Keep timestamp attachment before WindowInto, not inside it"],"tags":["python","apache-beam","windowing","timestamps","dofn"],"backgroundTag":"invalid-state-transition","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}