Skip to content

Raise ValueError for non-positive batch_size in iter_batch - #721

Merged
joein merged 2 commits into
qdrant:mainfrom
adityaanikam:fix-iter-batch-non-positive-size
Sep 26, 2026
Merged

joein merged 2 commits into
qdrant:mainfrom
adityaanikam:fix-iter-batch-non-positive-size

Conversation

@adityaanikam

Copy link
Copy Markdown
Contributor

Fixes #719

iter_batch (fastembed/common/utils.py) passed batch_size straight into islice with no lower bound check. islice(iterable, 0) returns nothing on every call, so batch_size=0 made embed and rerank silently return an empty result, with no error, across every modality that shares this helper (dense text, sparse text, image, late-interaction, and cross-encoder rerank). A negative batch_size hit islice's own argument validation and raised a cryptic ValueError instead of a clear one.

This adds a single validation check at the top of iter_batch, so it applies once for every caller instead of needing to be duplicated at each public entrypoint.

Testing: added test_iter_batch_rejects_non_positive_size and test_iter_batch_accepts_positive_size to tests/test_common.py. The rejection test fails on unmodified main (batch_size=0 returns without raising) and passes with the fix.

iter_batch passed batch_size straight into islice with no lower-bound
check. A batch_size of 0 made every islice call return an empty list
immediately, so embed and rerank returned an empty result with no error
across every modality that shares this helper. A negative batch_size hit
islice's own argument validation and raised a cryptic ValueError instead
of a clear one.

Validate batch_size >= 1 once in iter_batch itself, since every embed
and rerank entrypoint across dense text, sparse text, image,
late-interaction and cross-encoder rerank funnels through it.

Added test_iter_batch_rejects_non_positive_size and
test_iter_batch_accepts_positive_size to tests/test_common.py. The new
rejection test fails on unmodified code (batch_size=0 returns silently
instead of raising) and passes with the fix.

Fixes qdrant#719
Copilot AI lite review requested due to automatic review settings September 22, 2026 21:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Add assertions for the promised batch-size-specific error message.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds centralized validation to reject non-positive batch_size values in iter_batch.

Changes:

  • Raises a clear ValueError for invalid batch sizes.
  • Adds tests for positive and non-positive sizes.
  • Tests should also assert the expected error message for both invalid values.
File Summary
tests/​test_common.py Adds batch-size validation tests; message assertions are still needed.
fastembed/​common/​utils.py Validates batch size before batching.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/test_common.py Outdated
Comment on lines +70 to +74
with pytest.raises(ValueError):
list(iter_batch([1, 2, 3], 0))

with pytest.raises(ValueError):
list(iter_batch([1, 2, 3], -1))
@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 64ce68dd-696c-475c-8d02-fc61a064c975

📥 Commits

Reviewing files that changed from the base of the PR and between caa96de and c76e65d.

📒 Files selected for processing (2)
  • fastembed/common/utils.py
  • tests/test_common.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

iter_batch now raises ValueError when size is less than 1, when called. For valid sizes, it returns a generator that lazily produces batches. Tests cover positive-size batching and rejection of zero and negative sizes.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: joein

Merge Risk: ⚪ Minimal · up to c76e6

Batch-size errors are clearer, and valid batching remains lazy. No merge-blocking risk is identified.

Security Architecture Review

Security architecture risk: 🔵 Low · up to c76e6

Non-positive batch sizes now raise an error instead of producing an empty result or a less clear error. The change is shared across model APIs, but the reviewed paths show no new access, privilege, or deployment behavior.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The behavior can reach embedding and reranking callers through their shared batching helper. The reviewed paths do not show a corresponding increase in caller authority or access to another asset.

Trust Boundaries and Controls

  • inferred — The new input guard runs in the existing helper before it consumes supplied input. The reviewed facade-to-model paths still lead through that helper rather than around the guard.

Resilience and Maintainability Implications

  • inferred — For the reviewed parallel flow, invalid-size rejection occurs before workers start, avoiding a partially started worker transition for this input error.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: raising ValueError for non-positive batch sizes in iter_batch.
Description check ✅ Passed The description directly explains the bug, the validation fix, affected callers, and added tests.
Linked Issues check ✅ Passed The PR meets issue #719. iter_batch validates size before iteration and raises ValueError when size < 1. Tests cover 0, -1, and a valid positive size. The change prevents empty results for…
Out of Scope Changes check ✅ Passed The changes stay within issue #719. The implementation adds the required shared validation, and the tests verify the required rejection and acceptance behavior. No unrelated changes are identified.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@joein

joein commented Sep 26, 2026

Copy link
Copy Markdown
Member

hey @adityaanikam

thanks for taking care of it!

I've made some fixes so the check would work correctly in the parallel setting

@joein
joein self-requested a review September 26, 2026 17:29
@joein
joein merged commit 9667d11 into qdrant:main Sep 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: batch_size=0 silently returns no embeddings; negative batch_size raises cryptic islice error

3 participants