{"record":{"id":"44ab566dd0f9d974","repo":"apache/beam","slug":"constructor-or-constructor-method-can-only-be-specified-once","errorCode":null,"errorMessage":"Constructor or constructor method can only be specified once","messagePattern":"Constructor or constructor method can only be specified once","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/external.py","lineNumber":360,"sourceCode":"      builder_method = external_transforms_pb2.BuilderMethod(\n          name=builder_method_name,\n          schema=builder_method_schema,\n          payload=builder_method_payload)\n      builder_method.name = builder_method_name\n      payload.builder_methods.append(builder_method)\n    return payload\n\n  def with_constructor(self, *args, **kwargs):\n    \"\"\"\n    Specifies the Java constructor to use.\n    Arguments provided using args and kwargs will be applied to the Java\n    transform constructor in the specified order.\n\n    :param args: parameter values of the constructor.\n    :param kwargs: parameter names and values of the constructor.\n    \"\"\"\n    if self._has_constructor():\n      raise ValueError(\n          'Constructor or constructor method can only be specified once')\n\n    self._constructor_param_args = args\n    self._constructor_param_kwargs = kwargs\n\n  def with_constructor_method(self, method_name, *args, **kwargs):\n    \"\"\"\n    Specifies the Java constructor method to use.\n    Arguments provided using args and kwargs will be applied to the Java\n    transform constructor method in the specified order.\n\n    :param method_name: name of the constructor method.\n    :param args: parameter values of the constructor method.\n    :param kwargs: parameter names and values of the constructor method.\n    \"\"\"\n    if self._has_constructor():\n      raise ValueError(\n          'Constructor or constructor method can only be specified once')","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/external.py#L342-L378","documentation":"A JavaClassLookupPayloadBuilder may specify at most one constructor strategy: either with_constructor(...) or with_constructor_method(...). Calling with_constructor after either has already been set raises this ValueError because the object-construction path would be ambiguous.","triggerScenarios":"Chaining builder.with_constructor(...).with_constructor(...) or calling with_constructor after with_constructor_method.","commonSituations":"Conditional builder code where both branches set a constructor; reusing one builder for multiple configurations.","solutions":["Call only one of with_constructor / with_constructor_method per builder.","Create a fresh JavaClassLookupPayloadBuilder when you need a different construction strategy.","Wrap conditional setup with a check like if not builder._has_constructor()."],"exampleFix":"// before\nbuilder.with_constructor_method('create', cfg).with_constructor(cfg)\n// after\nbuilder.with_constructor_method('create', cfg)  # single construction strategy","handlingStrategy":"validation","validationCode":"if builder._has_constructor():\n    raise ValueError('constructor already specified on this builder')\nbuilder.with_constructor(*args)","typeGuard":"def constructor_free(builder) -> bool:\n    return not builder._has_constructor()","tryCatchPattern":"try:\n    builder.with_constructor(*args)\nexcept ValueError as e:\n    if 'can only be specified once' in str(e):\n        builder = JavaClassLookupPayloadBuilder(class_name)\n        builder.with_constructor(*args)\n    else:\n        raise","preventionTips":["Set the construction strategy exactly once, immediately after builder creation.","Don't share builder instances across configuration branches.","Encapsulate builder setup in one helper function."],"tags":["python","apache-beam","cross-language","builder-api"],"backgroundTag":"conflicting-config-options","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"}