apache/beam · error · ValueError
Either size or error should be set. Received
Error message
Either size or error should be set. Received {size = %s, error = %s}. What it means
ApproximateUnique.parse_input_params requires exactly one of 'size' (sample size) or 'error' (estimation error) to be given. Passing BOTH is rejected with _MULTI_VALUE_ERR_MSG ('Either size or error should be set. Received {size = %s, error = %s}.'). The two parameters are mutually exclusive constructors of the estimator.
Solutions
- Provide only one parameter: delete either size or error.
- If you know the desired sample size, pass only size (int >= 16); if you know the tolerance, pass only error.
- Centralize construction so config merging cannot fill both fields.
Example fix
// before beam.ApproximateUnique(size=1000, error=0.02) // after beam.ApproximateUnique(error=0.02)
Defensive patterns
Strategy: validation
Validate before calling
assert not (size is not None and error is not None), \
'ApproximateUnique accepts only one of size or error'
params = {k: v for k, v in [('size', size), ('error', error)] if v is not None}
assert len(params) == 1 Type guard
def exactly_one_of(size, error) -> bool:
return (size is None) != (error is None) Try / catch
try:
t = beam.ApproximateUnique(size=size, error=error)
except ValueError as e:
if 'Either size or error' in str(e):
t = beam.ApproximateUnique(error=error if error is not None else 0.02)
else:
raise Prevention
- Never pass both size and error; pick one estimation strategy.
- Wrap ApproximateUnique in a helper that takes exactly one parameter.
- Audit merged configs for both fields being populated.
When it happens
Trigger: beam.ApproximateUnique(size=100, error=0.02) — both supplied; Parse from a transform spec where both keys were set in pipeline options or a YAML payload.
Common situations: Copy-pasted code where a default size stayed while an error value was added; CLI/config merging that set both fields.
Related errors
- ApproximateUnique needs a size >= 16 for an error <= 0.50…
- ApproximateUnique needs an estimation error between 0.01…
- Both a BigQuery table and a query were specified. Please…
- Either size or error should be set. Received
- Timing number 0b" + timingNumber.toString(2) + " has more…
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/ae6d8996e5215b25.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/python/apache_beam/transforms/stats.py:122
@staticmethod
def parse_input_params(size=None, error=None):
"""
Check if input params are valid and return sample size.
:param size: an int not smaller than 16, which we would use to estimate
number of unique values.
:param error: max estimation error, which is a float between 0.01 and 0.50.
If error is given, sample size will be calculated from error with
_get_sample_size_from_est_error function.
:return: sample size
:raises:
ValueError: If both size and error are given, or neither is given, or
values are out of range.
"""
if None not in (size, error):
raise ValueError(ApproximateUnique._MULTI_VALUE_ERR_MSG % (size, error))
elif size is None and error is None:
raise ValueError(ApproximateUnique._NO_VALUE_ERR_MSG)
elif size is not None:
if not isinstance(size, int) or size < 16:
raise ValueError(ApproximateUnique._INPUT_SIZE_ERR_MSG % (size))
else:
return size
else:
if error < 0.01 or error > 0.5:
raise ValueError(ApproximateUnique._INPUT_ERROR_ERR_MSG % (error))
else:
return ApproximateUnique._get_sample_size_from_est_error(error)
@staticmethod
def _get_sample_size_from_est_error(est_err):
"""
:return: sample size
View on GitHub (pinned to 12126d8942)