Skip to content

bug(core): _batch and _abatch lack batch_size validation causing infinite loop #36647

@chang923

Description

@chang923

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelated to a bug, vulnerability, unexpected error with an existing featurecore`langchain-core` package issues & PRsexternal

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions