fix(core): validate batch_size in _batch and _abatch to prevent infinite loop (#36663)

This commit is contained in:
Sharvil Saxena
2026-04-26 15:13:20 -04:00
committed by GitHub
parent 4613a4d951
commit 78546e9242
2 changed files with 27 additions and 0 deletions

View File

@@ -90,6 +90,9 @@ def _hash_nested_dict(
def _batch(size: int, iterable: Iterable[T]) -> Iterator[list[T]]:
"""Utility batching function."""
if size <= 0:
msg = f"Batch size must be a positive integer, got {size}."
raise ValueError(msg)
it = iter(iterable)
while True:
chunk = list(islice(it, size))
@@ -100,6 +103,9 @@ def _batch(size: int, iterable: Iterable[T]) -> Iterator[list[T]]:
async def _abatch(size: int, iterable: AsyncIterable[T]) -> AsyncIterator[list[T]]:
"""Utility batching function."""
if size <= 0:
msg = f"Batch size must be a positive integer, got {size}."
raise ValueError(msg)
batch: list[T] = []
async for element in iterable:
if len(batch) < size: