{"record":{"id":"75ca5686bb74fbd8","repo":"apache/beam","slug":"min-batch-size-must-be-1-got-min-batch-size","errorCode":null,"errorMessage":"min_batch_size must be >= 1, got {min_batch_size}","messagePattern":"min_batch_size must be >= 1, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/util.py","lineNumber":1386,"sourceCode":"      # Elements are sorted by length and batched optimally\n\n      # Batch with custom size function\n      data = [{'text': 'short'}, {'text': 'medium text'},\n              {'text': 'long text here'}]\n      batched = data | SortAndBatchElements(\n          min_batch_size=1,\n          max_batch_size=10,\n          max_batch_weight=100,\n          element_size_fn=lambda x: len(x['text']))\n  \"\"\"\n  def __init__(\n      self,\n      min_batch_size: int,\n      max_batch_size: int,\n      max_batch_weight: int,\n      element_size_fn: Optional[Callable[[Any], int]] = None):\n    if min_batch_size < 1:\n      raise ValueError(f'min_batch_size must be >= 1, got {min_batch_size}')\n    if max_batch_size < min_batch_size:\n      raise ValueError(\n          f'max_batch_size ({max_batch_size}) must be >= '\n          f'min_batch_size ({min_batch_size})')\n    if max_batch_weight < 1:\n      raise ValueError(f'max_batch_weight must be >= 1, got {max_batch_weight}')\n    if element_size_fn is not None and not callable(element_size_fn):\n      raise TypeError('element_size_fn must be callable')\n\n    self._min_batch_size = min_batch_size\n    self._max_batch_size = max_batch_size\n    self._max_batch_weight = max_batch_weight\n\n    # None means the DoFn will use its own _default_element_size method,\n    # which tries len() and warns once on TypeError before falling back to 1.\n    self._element_size_fn = element_size_fn\n\n  def expand(self, pcoll):","sourceCodeStart":1368,"sourceCodeEnd":1404,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/util.py#L1368-L1404","documentation":"GroupIntoBatches' parameters object validates in __init__ that min_batch_size is at least 1; a batch of size 0 or negative is meaningless so it raises ValueError with the offending value interpolated. This is an eager constructor-time guard, so the failure occurs before any pipeline executes.","triggerScenarios":"Calling util.GroupIntoBatches(...) with min_batch_size=0 or a negative integer (common when the value comes from a computed/defaulted variable or an unparsed CLI flag).","commonSituations":"Config values defaulting to 0 via `or` on falsy input; parsing '--min-batch-size' flags without validation; deriving batch size from a division that floors to 0.","solutions":["Pass an explicit min_batch_size >= 1 to GroupIntoBatches","If the value comes from config/CLI, coerce and validate: max(1, int(value))","Check upstream defaults so an empty/unset value doesn't fall through as 0"],"exampleFix":"// before\nutil.GroupIntoBatches(min_batch_size=0, max_batch_size=100, max_batch_weight=1000)\n// after\nutil.GroupIntoBatches(min_batch_size=max(1, requested_min), max_batch_size=100, max_batch_weight=1000)","handlingStrategy":"validation","validationCode":"if min_batch_size < 1:\n    raise ValueError('min_batch_size must be >= 1')","typeGuard":"def valid_min_batch(v): return isinstance(v, int) and v >= 1","tryCatchPattern":null,"preventionTips":["Validate config/CLI-derived batch sizes before constructing transforms","Avoid relying on falsy defaults producing 0","Add unit tests for parameter edge values"],"tags":["python","apache-beam","validation","constructor","batching"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}