{"record":{"id":"ae6d8996e5215b25","repo":"apache/beam","slug":"either-size-or-error-should-be-set-received-size-s-error-s","errorCode":null,"errorMessage":"Either size or error should be set. Received {size = %s, error = %s}.","messagePattern":"Either size or error should be set\\. Received (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/stats.py","lineNumber":122,"sourceCode":"\n  @staticmethod\n  def parse_input_params(size=None, error=None):\n    \"\"\"\n    Check if input params are valid and return sample size.\n\n    :param size: an int not smaller than 16, which we would use to estimate\n      number of unique values.\n    :param error: max estimation error, which is a float between 0.01 and 0.50.\n      If error is given, sample size will be calculated from error with\n      _get_sample_size_from_est_error function.\n    :return: sample size\n    :raises:\n      ValueError: If both size and error are given, or neither is given, or\n      values are out of range.\n    \"\"\"\n\n    if None not in (size, error):\n      raise ValueError(ApproximateUnique._MULTI_VALUE_ERR_MSG % (size, error))\n    elif size is None and error is None:\n      raise ValueError(ApproximateUnique._NO_VALUE_ERR_MSG)\n    elif size is not None:\n      if not isinstance(size, int) or size < 16:\n        raise ValueError(ApproximateUnique._INPUT_SIZE_ERR_MSG % (size))\n      else:\n        return size\n    else:\n      if error < 0.01 or error > 0.5:\n        raise ValueError(ApproximateUnique._INPUT_ERROR_ERR_MSG % (error))\n      else:\n        return ApproximateUnique._get_sample_size_from_est_error(error)\n\n  @staticmethod\n  def _get_sample_size_from_est_error(est_err):\n    \"\"\"\n    :return: sample size\n","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/stats.py#L104-L140","documentation":"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.","triggerScenarios":"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.","commonSituations":"Copy-pasted code where a default size stayed while an error value was added; CLI/config merging that set both fields.","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."],"exampleFix":"// before\nbeam.ApproximateUnique(size=1000, error=0.02)\n// after\nbeam.ApproximateUnique(error=0.02)","handlingStrategy":"validation","validationCode":"assert not (size is not None and error is not None), \\\n    'ApproximateUnique accepts only one of size or error'\nparams = {k: v for k, v in [('size', size), ('error', error)] if v is not None}\nassert len(params) == 1","typeGuard":"def exactly_one_of(size, error) -> bool:\n    return (size is None) != (error is None)","tryCatchPattern":"try:\n    t = beam.ApproximateUnique(size=size, error=error)\nexcept ValueError as e:\n    if 'Either size or error' in str(e):\n        t = beam.ApproximateUnique(error=error if error is not None else 0.02)\n    else:\n        raise","preventionTips":["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."],"tags":["python","apache-beam","approximateunique","mutually-exclusive"],"backgroundTag":"mutually-exclusive-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"}