{"record":{"id":"6137b39977cf5ff9","repo":"apache/beam","slug":"approximateunique-needs-an-estimation-error-between-0-01-and","errorCode":null,"errorMessage":"ApproximateUnique needs an estimation error between 0.01 and 0.50. Received {error = %s}.","messagePattern":"ApproximateUnique needs an estimation error between 0\\.01 and 0\\.50\\. Received (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/stats.py","lineNumber":132,"sourceCode":"      _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\n    Calculate sample size from estimation error\n    \"\"\"\n    return math.ceil(4.0 / math.pow(est_err, 2.0))\n\n  @typehints.with_input_types(T)\n  @typehints.with_output_types(int)\n  class Globally(PTransform):\n    \"\"\" Approximate.Globally approximate number of unique values\"\"\"\n    def __init__(self, size=None, error=None):\n      self._sample_size = ApproximateUnique.parse_input_params(size, error)","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/stats.py#L114-L150","documentation":"When error is given, parse_input_params requires 0.01 <= error <= 0.50; outside that range _INPUT_ERROR_ERR_MSG is raised. This mirrors the size constraint: error is about 2/sqrt(sample_size), so errors below 1% need impractically large samples and errors above 50% are meaningless.","triggerScenarios":"beam.ApproximateUnique(error=0.005) (too precise) or error=0.9 (too loose); error passed as a string like '0.02' that compares incorrectly or is otherwise out of range.","commonSituations":"Users expecting high precision (e.g. 0.1% error); config strings converted to numbers incorrectly; mistaking the value for a percentage (5 instead of 0.05).","solutions":["Choose an error between 0.01 and 0.50, e.g. 0.02 for ~2% error.","If a percentage was intended, divide by 100 (5 -> 0.05).","For tighter precision than 1%, use exact deduplication (beam.Distinct) instead of ApproximateUnique.","Ensure the value is a float, not a string, before passing it."],"exampleFix":"// before\nbeam.ApproximateUnique(error=0.001)\n// after\nbeam.ApproximateUnique(error=0.02)  # within [0.01, 0.50]","handlingStrategy":"validation","validationCode":"if error is not None:\n    error = float(error)\n    assert 0.01 <= error <= 0.50, f'error must be in [0.01, 0.50], got {error}'","typeGuard":"def is_valid_estimation_error(error) -> bool:\n    try:\n        return 0.01 <= float(error) <= 0.50\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    t = beam.ApproximateUnique(error=error)\nexcept ValueError as e:\n    if 'estimation error' in str(e):\n        clamped = min(0.5, max(0.01, float(error)))\n        t = beam.ApproximateUnique(error=clamped)\n    else:\n        raise","preventionTips":["Keep error in [0.01, 0.50]; convert percentages (5 -> 0.05).","Use beam.Distinct for precision better than 1%.","Coerce config values to float before passing."],"tags":["python","apache-beam","approximateunique","value-out-of-range"],"backgroundTag":"argument-out-of-range","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"}