apache/beam · error · ValueError

Constructor or constructor method can only be specified once

Error message

Constructor or constructor method can only be specified once

What it means

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.

Solutions

  1. Call only one of with_constructor / with_constructor_method per builder.
  2. Create a fresh JavaClassLookupPayloadBuilder when you need a different construction strategy.
  3. Wrap conditional setup with a check like if not builder._has_constructor().

Example fix

// before
builder.with_constructor_method('create', cfg).with_constructor(cfg)
// after
builder.with_constructor_method('create', cfg)  # single construction strategy
Defensive patterns

Strategy: validation

Validate before calling

if builder._has_constructor():
    raise ValueError('constructor already specified on this builder')
builder.with_constructor(*args)

Type guard

def constructor_free(builder) -> bool:
    return not builder._has_constructor()

Try / catch

try:
    builder.with_constructor(*args)
except ValueError as e:
    if 'can only be specified once' in str(e):
        builder = JavaClassLookupPayloadBuilder(class_name)
        builder.with_constructor(*args)
    else:
        raise

Prevention

When it happens

Trigger: Chaining builder.with_constructor(...).with_constructor(...) or calling with_constructor after with_constructor_method.

Common situations: Conditional builder code where both branches set a constructor; reusing one builder for multiple configurations.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/44ab566dd0f9d974. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/transforms/external.py:360

      builder_method = external_transforms_pb2.BuilderMethod(
          name=builder_method_name,
          schema=builder_method_schema,
          payload=builder_method_payload)
      builder_method.name = builder_method_name
      payload.builder_methods.append(builder_method)
    return payload

  def with_constructor(self, *args, **kwargs):
    """
    Specifies the Java constructor to use.
    Arguments provided using args and kwargs will be applied to the Java
    transform constructor in the specified order.

    :param args: parameter values of the constructor.
    :param kwargs: parameter names and values of the constructor.
    """
    if self._has_constructor():
      raise ValueError(
          'Constructor or constructor method can only be specified once')

    self._constructor_param_args = args
    self._constructor_param_kwargs = kwargs

  def with_constructor_method(self, method_name, *args, **kwargs):
    """
    Specifies the Java constructor method to use.
    Arguments provided using args and kwargs will be applied to the Java
    transform constructor method in the specified order.

    :param method_name: name of the constructor method.
    :param args: parameter values of the constructor method.
    :param kwargs: parameter names and values of the constructor method.
    """
    if self._has_constructor():
      raise ValueError(
          'Constructor or constructor method can only be specified once')

View on GitHub (pinned to 12126d8942)