Checked other resources
Package (Required)
Related Issues / PRs
#36648
Reproduction Steps / Example Code (Python)
from langchain_core.indexing.api import _batch, _abatch
# Synchronous _batch with size=0 leads to infinite loop
list(_batch(0, [1, 2, 3]))
# Asynchronous _abatch with size=0 yields empty batches endlessly
import asyncio
from typing import AsyncIterable
async def async_iter(data):
for item in data:
yield item
async def test_abatch():
async for batch in _abatch(0, async_iter([1, 2, 3])):
print(batch) # Prints empty lists indefinitely
asyncio.run(test_abatch())
Error Message and Stack Trace (if applicable)
Description
I am using aindex and index functions from langchain_core.indexing. When the batch_size parameter is mistakenly set to 0 (or any negative value), the internal utility functions _batch and _abatch exhibit harmful behavior:
- Synchronous
_batch: Enters an infinite loop (while True never exits because islice(it, 0) returns empty list indefinitely).
- Asynchronous
_abatch: Yields an empty list for every element in the input iterable, causing silent performance degradation and wasted CPU.
Expected behavior:
The functions should raise a ValueError immediately when size <= 0, failing fast and alerting the user to the invalid configuration.
Actual behavior:
_batch hangs the process; _abatch produces a flood of empty batches without any error.
Proposed fix:
Add a simple validation at the beginning of both functions:
if size <= 0:
raise ValueError("Batch size must be a positive integer.")
### System Info
System Information
------------------
> OS: Linux
> OS Version: #1 SMP Tue Nov 5 00:21:55 UTC 2024
> Python Version: 3.10.20 (main, Mar 11 2026, 17:46:40) [GCC 14.3.0]
Package Information
-------------------
> langchain_core: 1.2.19
> langchain: 1.2.13
> langchain_community: 0.4.1
> langsmith: 0.7.17
> langchain_chroma: 1.1.0
> langchain_classic: 1.0.3
> langchain_deepseek: 1.0.1
> langchain_huggingface: 1.2.1
> langchain_openai: 1.1.11
> langchain_text_splitters: 1.1.1
> langgraph_sdk: 0.3.11
Optional packages not installed
-------------------------------
> deepagents
> deepagents-cli
Other Dependencies
------------------
> aiohttp: 3.13.3
> async-timeout: 4.0.3
> chromadb: 1.5.5
> dataclasses-json: 0.6.7
> httpx: 0.28.1
> httpx-sse: 0.4.3
> huggingface-hub: 1.7.1
> jsonpatch: 1.33
> langgraph: 1.1.2
> numpy: 2.2.6
> openai: 2.28.0
> opentelemetry-api: 1.40.0
> opentelemetry-sdk: 1.40.0
> orjson: 3.11.7
> packaging: 25.0
> pydantic: 2.12.5
> pydantic-settings: 2.13.1
> pytest: 9.0.3
> pyyaml: 6.0.3
> PyYAML: 6.0.3
> requests: 2.32.5
> requests-toolbelt: 1.0.0
> rich: 14.3.3
> sentence-transformers: 5.3.0
> SQLAlchemy: 2.0.48
> sqlalchemy: 2.0.48
> tenacity: 9.1.4
> tiktoken: 0.12.0
> tokenizers: 0.22.2
> transformers: 5.3.0
> typing-extensions: 4.15.0
> uuid-utils: 0.14.1
> websockets: 16.0
> xxhash: 3.6.0
> zstandard: 0.25.0
Checked other resources
Package (Required)
Related Issues / PRs
#36648
Reproduction Steps / Example Code (Python)
Error Message and Stack Trace (if applicable)
Description
I am using
aindexandindexfunctions fromlangchain_core.indexing. When thebatch_sizeparameter is mistakenly set to0(or any negative value), the internal utility functions_batchand_abatchexhibit harmful behavior:_batch: Enters an infinite loop (while Truenever exits becauseislice(it, 0)returns empty list indefinitely)._abatch: Yields an empty list for every element in the input iterable, causing silent performance degradation and wasted CPU.Expected behavior:
The functions should raise a
ValueErrorimmediately whensize <= 0, failing fast and alerting the user to the invalid configuration.Actual behavior:
_batchhangs the process;_abatchproduces a flood of empty batches without any error.Proposed fix:
Add a simple validation at the beginning of both functions: