{"record":{"id":"b7f02ca4bb42a024","repo":"apache/beam","slug":"module-is-not-registered-for-pickle-by-value","errorCode":null,"errorMessage":"{module} is not registered for pickle by value","messagePattern":"(.+?) is not registered for pickle by value","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py","lineNumber":260,"sourceCode":"  # sys.modules at registering time and assuming that it will still be in\n  # there when accessed during pickling. Another alternative would be to\n  # store a weakref to the module. Even though cloudpickle does not implement\n  # this introspection yet, in order to avoid a possible breaking change\n  # later, we still enforce the presence of module inside sys.modules.\n  if module.__name__ not in sys.modules:\n    raise ValueError(\n        f\"{module} was not imported correctly, have you used an \"\n        \"`import` statement to access it?\")\n  _PICKLE_BY_VALUE_MODULES.add(module.__name__)\n\n\ndef unregister_pickle_by_value(module):\n  \"\"\"Unregister that the input module should be pickled by value.\"\"\"\n  if not isinstance(module, types.ModuleType):\n    raise ValueError(\n        f\"Input should be a module object, got {str(module)} instead\")\n  if module.__name__ not in _PICKLE_BY_VALUE_MODULES:\n    raise ValueError(f\"{module} is not registered for pickle by value\")\n  else:\n    _PICKLE_BY_VALUE_MODULES.remove(module.__name__)\n\n\ndef list_registry_pickle_by_value():\n  return _PICKLE_BY_VALUE_MODULES.copy()\n\n\ndef _is_registered_pickle_by_value(module):\n  module_name = module.__name__\n  if module_name in _PICKLE_BY_VALUE_MODULES:\n    return True\n  while True:\n    parent_name = module_name.rsplit(\".\", 1)[0]\n    if parent_name == module_name:\n      break\n    if parent_name in _PICKLE_BY_VALUE_MODULES:\n      return True","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py#L242-L278","documentation":"unregister_pickle_by_value() raises ValueError when the module's __name__ is not in the _PICKLE_BY_VALUE_MODULES set, i.e. the module was never registered (or was already unregistered) for pickle-by-value.","triggerScenarios":"Calling unregister_pickle_by_value(mymodule) without a prior register_pickle_by_value(mymodule); calling it twice; registering under a different module (e.g. package submodule vs re-exported alias).","commonSituations":"Cleanup/teardown in tests that runs even when registration never happened; double teardown; module renamed between register and unregister.","solutions":["Only call unregister after a successful register_pickle_by_value on the same module.","Check membership first: if mymodule.__name__ in list_registry_pickle_by_value(): unregister...","Make teardown idempotent by catching ValueError around unregister.","Register and unregister in paired try/finally blocks."],"exampleFix":"// before\nunregister_pickle_by_value(mymodule)  # may raise if never registered\n// after\nif mymodule.__name__ in list_registry_pickle_by_value():\n    unregister_pickle_by_value(mymodule)","handlingStrategy":"validation","validationCode":"from apache_beam.internal.cloudpickle.cloudpickle import list_registry_pickle_by_value\nif mod.__name__ not in list_registry_pickle_by_value():\n    return  # nothing to unregister","typeGuard":"def is_registered(mod) -> bool:\n    from apache_beam.internal.cloudpickle.cloudpickle import list_registry_pickle_by_value\n    return mod.__name__ in list_registry_pickle_by_value()","tryCatchPattern":"try:\n    unregister_pickle_by_value(mod)\nexcept ValueError as e:\n    if 'not registered for pickle by value' in str(e):\n        pass  # already unregistered; ignore in teardown\n    else:\n        raise","preventionTips":["Make teardown idempotent.","Pair register/unregister with try/finally.","Check list_registry_pickle_by_value() before unregistering."],"tags":["python","apache-beam","cloudpickle","state"],"backgroundTag":"entity-not-found","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}