{"record":{"id":"310cbd02b720dcf0","repo":"apache/beam","slug":"requested-execution-of-a-stateful-dofn-but-no-user-state","errorCode":null,"errorMessage":"Requested execution of a stateful DoFn, but no user state context is available. This likely means that the current runner does not support the execution of stateful DoFns.","messagePattern":"Requested execution of a stateful DoFn, but no user state context is available\\. This likely means that the current runner does not support the execution of stateful DoFns\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"sdks/python/apache_beam/runners/common.py","lineNumber":1494,"sourceCode":"    output_handler = _OutputHandler(\n        windowing.windowfn,\n        main_receivers,\n        tagged_receivers,\n        per_element_output_counter,\n        getattr(fn, 'output_batch_converter', None),\n        getattr(\n            do_fn_signature.process_method.method_value,\n            '_beam_yields_batches',\n            False),\n        getattr(\n            do_fn_signature.process_batch_method.method_value,\n            '_beam_yields_elements',\n            False),\n        check_user_dofn_output=check_user_dofn_output,\n    )\n\n    if do_fn_signature.is_stateful_dofn() and not user_state_context:\n      raise Exception(\n          'Requested execution of a stateful DoFn, but no user state context '\n          'is available. This likely means that the current runner does not '\n          'support the execution of stateful DoFns.')\n\n    self.do_fn_invoker = DoFnInvoker.create_invoker(\n        do_fn_signature,\n        output_handler,\n        self.context,\n        side_inputs,\n        args,\n        kwargs,\n        user_state_context=user_state_context,\n        bundle_finalizer_param=self.bundle_finalizer_param)\n\n  def process(self, windowed_value):\n    # type: (WindowedValue) -> Iterable[SplitResultResidual]\n    try:\n      return self.do_fn_invoker.invoke_process(windowed_value)","sourceCodeStart":1476,"sourceCodeEnd":1512,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/runners/common.py#L1476-L1512","documentation":"Beam raises this when a DoFn is marked stateful (uses StateParam/TimerParam) but DoFnInvoker is created without a user_state_context, which supplies state/timer access backed by the runner. Without it, state and timer parameters cannot be fulfilled, so construction fails early with this exception.","triggerScenarios":"Using DoFnInvoker.create_invoker (or a runner path) for a stateful DoFn without passing a user_state_context (e.g. running on a runner or test harness that lacks state support).","commonSituations":"Running stateful pipelines on runners without stateful DoFn support (some batch/direct/test harnesses or older runner versions); unit-testing stateful DoFns with plain create_invoker and no UserStateContext; missing --streaming or state flags on runners that gate the feature.","solutions":["Run on a runner that supports stateful DoFns (e.g. Dataflow/Flink/Spark with state support, DirectRunner where applicable) and pass a user_state_context to create_invoker","In tests, supply a mock/fake UserStateContext when constructing the invoker","If state is not actually required, refactor the DoFn to be stateless"],"exampleFix":"# before\ninvoker = DoFnInvoker.create_invoker(sig, output_processor=...)  # no context\n# after\ninvoker = DoFnInvoker.create_invoker(\n    sig,\n    output_processor=...,\n    user_state_context=my_user_state_context)","handlingStrategy":"validation","validationCode":"def assert_state_supported(dofn_signature, context):\n    if dofn_signature.is_stateful_dofn() and context is None:\n        raise Exception('Stateful DoFn requires a runner/UserStateContext that supports state')","typeGuard":"def runner_supports_state(runner_name):\n    return runner_name in ('DirectRunner', 'DataflowRunner', 'FlinkRunner', 'SparkRunner')","tryCatchPattern":"try:\n    invoker = DoFnInvoker.create_invoker(sig, output_processor=proc,\n                                         user_state_context=ctx)\nexcept Exception as e:\n    if 'no user state context' in str(e):\n        logging.error('Runner does not support stateful DoFns; switch runner or refactor to stateless')\n    raise","preventionTips":["Verify runner stateful-DoFn support before adding StateParam/TimerParam","Pass a user_state_context (real or fake) whenever invoking stateful DoFns in tests","Gate stateful features with a pipeline-option check at job construction"],"tags":["apache-beam","python","user-state","runner-support"],"backgroundTag":"missing-required-config","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"}