{"record":{"id":"d2b7123a8057221c","repo":"apache/beam","slug":"input-should-be-a-module-object-got-str-module-instead","errorCode":null,"errorMessage":"Input should be a module object, got {str(module)} instead","messagePattern":"Input should be a module object, got (.+?) instead","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py","lineNumber":235,"sourceCode":"\n    By default, functions and classes that are attributes of an importable\n    module are to be pickled by reference, that is relying on re-importing\n    the attribute from the module at load time.\n\n    If `register_pickle_by_value(module)` is called, all its functions and\n    classes are subsequently to be pickled by value, meaning that they can\n    be loaded in Python processes where the module is not importable.\n\n    This is especially useful when developing a module in a distributed\n    execution environment: restarting the client Python process with the new\n    source code is enough: there is no need to re-install the new version\n    of the module on all the worker nodes nor to restart the workers.\n\n    Note: this feature is considered experimental. See the cloudpickle\n    README.md file for more details and limitations.\n    \"\"\"\n  if not isinstance(module, types.ModuleType):\n    raise ValueError(\n        f\"Input should be a module object, got {str(module)} instead\")\n  # In the future, cloudpickle may need a way to access any module registered\n  # for pickling by value in order to introspect relative imports inside\n  # functions pickled by value. (see\n  # https://github.com/cloudpipe/cloudpickle/pull/417#issuecomment-873684633).\n  # This access can be ensured by checking that module is present in\n  # 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","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py#L217-L253","documentation":"register_pickle_by_value() in Beam's vendored cloudpickle records a module so its code is serialized by value rather than by reference. The library raises ValueError immediately if the argument is not a types.ModuleType instance, because only real module objects carry the __name__/module semantics it needs to register.","triggerScenarios":"Calling apache_beam.internal.cloudpickle.cloudpickle.register_pickle_by_value() with a string module name (e.g. 'my_module'), a class, function, package path, or any non-module object.","commonSituations":"Passing a module name as a string instead of the module object; using __main__-defined helpers accidentally; calling with a dynamically imported object that is actually a class or function, not the module.","solutions":["Pass the module object itself, e.g. register_pickle_by_value(my_module), not a string.","If you only have the name string, do importlib.import_module(name) first.","Verify with isinstance(x, types.ModuleType) before calling.","Register the module right after a normal `import`, not via from-import of individual symbols."],"exampleFix":"// before\nregister_pickle_by_value('mymodule')\n// after\nimport mymodule\nregister_pickle_by_value(mymodule)","handlingStrategy":"type-guard","validationCode":"import types\nif not isinstance(mod, types.ModuleType):\n    raise TypeError('register_pickle_by_value requires a module object')","typeGuard":"def is_module(x) -> bool:\n    import types\n    return isinstance(x, types.ModuleType)","tryCatchPattern":"try:\n    register_pickle_by_value(mod)\nexcept ValueError as e:\n    if 'Input should be a module object' in str(e):\n        register_pickle_by_value(importlib.import_module(str(mod)))\n    else:\n        raise","preventionTips":["Always pass the module object, never its name string.","Register immediately after a top-level import.","Add isinstance(x, types.ModuleType) asserts in helper wrappers."],"tags":["python","apache-beam","cloudpickle","type-error"],"backgroundTag":"invalid-argument-value","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"}