Skip to content

Commit b0b1d2c

Browse files
Guard chunk_and_stitch_embed against a non-positive cap
The shared helper assumed cap was positive; a cap <= 0 (a caller misconfiguration) surfaced as a raw range() error (0) or a misleading empty-stitch validation failure (< 0). Raise ValueError eagerly with a clear message, mirroring the chunk_size guard TeiEmbeddingProvider already applies at construction.
1 parent 19dc109 commit b0b1d2c

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/openarmature/retrieval/_wire.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ async def chunk_and_stitch_embed(
117117
per-mapping wire shaping, POST, and parse live in the closure. Returns the
118118
stitched :class:`EmbeddingResponse`.
119119
"""
120+
# cap is the provider's per-call input limit; a non-positive cap is a caller
121+
# misconfiguration -- fail loudly rather than surfacing a raw range() error
122+
# (cap == 0) or a misleading empty-stitched validation failure (cap < 0).
123+
if cap <= 0:
124+
raise ValueError(f"cap must be positive (got {cap})")
120125
stitched_vectors: list[list[float]] = []
121126
chunk_bodies: list[Any] = []
122127
input_tokens_total: int | None = None

tests/unit/test_retrieval_provider.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,19 @@ def handler(req: httpx.Request) -> httpx.Response:
14921492
assert response.response_id is None
14931493

14941494

1495+
async def test_chunk_and_stitch_embed_rejects_non_positive_cap() -> None:
1496+
# The shared helper guards a caller passing cap <= 0 (a misconfigured per-call
1497+
# limit): fail loudly rather than a raw range() error (0) or an empty stitch.
1498+
from openarmature.retrieval._wire import chunk_and_stitch_embed
1499+
1500+
async def _noop(chunk: list[str]) -> tuple[list[list[float]], None, None, list[Any]]:
1501+
return [], None, None, []
1502+
1503+
for bad_cap in (0, -1):
1504+
with pytest.raises(ValueError, match="cap must be positive"):
1505+
await chunk_and_stitch_embed(["a"], model="m", cap=bad_cap, embed_chunk=_noop)
1506+
1507+
14951508
# --- /rerank wire + chunk-and-stitch ----------------------------------------
14961509

14971510

0 commit comments

Comments
 (0)