{"record":{"id":"c2d55ac965fac50a","repo":"apache/beam","slug":"cannot-serialize-arguments-for-constructor-as-json-exn","errorCode":null,"errorMessage":"Cannot serialize arguments for {constructor} as json: {exn}","messagePattern":"Cannot serialize arguments for (.+?) as json: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"sdks/python/apache_beam/transforms/ptransform.py","lineNumber":1252,"sourceCode":"        f'{constructor.__module__}.{constructor.__qualname__}')\n    try:\n      imported_constructor = (\n          python_callable.PythonCallableWithSource.\n          load_from_fully_qualified_name(fully_qualified_name))\n      if imported_constructor != wrapper:\n        raise ImportError('Different object.')\n    except ImportError:\n      warnings.warn(f'Cannot import {constructor} as {fully_qualified_name}.')\n      return transform\n\n    try:\n      config = json.dumps({\n          'constructor': fully_qualified_name,\n          'args': args,\n          'kwargs': kwargs,\n      })\n    except TypeError as exn:\n      warnings.warn(\n          f'Cannot serialize arguments for {constructor} as json: {exn}')\n      return transform\n\n    original_annotations = transform.annotations\n    transform.annotations = lambda: {\n        **original_annotations(),\n        # These override whatever may have been provided earlier.\n        # The outermost call is expected to be the most specific.\n        'yaml_provider': 'python',\n        'yaml_type': 'PyTransform',\n        'yaml_args': config, }\n    return transform\n\n  return wrapper\n","sourceCodeStart":1234,"sourceCodeEnd":1267,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/ptransform.py#L1234-L1267","documentation":"After resolving the constructor, Beam serializes the transform's args/kwargs to a JSON config for cross-language/spec representation. If json.dumps raises TypeError (non-JSON-serializable argument like a custom object, datetime, or set), it warns 'Cannot serialize arguments for <constructor> as json: <exn>' and returns the transform without the annotation.","triggerScenarios":"Passing arguments that are not JSON-serializable (datetime, Decimal, custom class instances, numpy scalars in some versions) to a ptransform whose configuration is being captured.","commonSituations":"Building pipelines with rich config objects in notebooks or scripts, then trying to export/specify them for remote runners or Beam YAML composition.","solutions":["Convert arguments to JSON-serializable types: str for datetimes, float/int for Decimals and numpy scalars, lists for sets.","Pass primitives (str/int/float/bool/list/dict) instead of custom config objects.","Pre-serialize complex values yourself (e.g. json.dumps to a string) and parse them inside the transform.","Ignore the warning if the annotation is not required for your runner."],"exampleFix":"// before\nMyTransform(config=MyConfig(start=datetime.now()))\n// after\nMyTransform(config={'start': datetime.now().isoformat()})","handlingStrategy":"validation","validationCode":"import json\ndef assert_jsonable(obj):\n    try:\n        json.dumps(obj)\n    except TypeError as e:\n        raise ValueError(f'argument not JSON-serializable: {e}')","typeGuard":null,"tryCatchPattern":"import warnings\nwith warnings.catch_warnings():\n    warnings.simplefilter('error')\n    try:\n        MyTransform(config=cfg)  # raises if config not JSON-serializable\n    except TypeError as e:\n        print(f'convert config to JSON types: {e}')","preventionTips":["Restrict ptransform arguments to JSON-native types","Serialize datetimes/Decimals to strings before passing","Add a unit test that json.dumps all pipeline configs"],"tags":["serialization","json","arguments","ptransform"],"backgroundTag":"json-serialization-failed","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"}