Closed
Labels
bugSomething isn't workingSomething isn't workingcode-reviewFound via automated code reviewFound via automated code reviewpriority: highData loss, security, or major correctness bugData loss, security, or major correctness bug
Description
Summary
pipeline.py calls embedder.embed([chunk.text for chunk in chunks]) with all chunks in a single batch and no size limit. A large document (500+ pages) can produce thousands of chunks, exceeding the OpenAI API's per-request token budget or exhausting BgeM3's GPU/CPU memory — causing the entire ingest to fail.
Root Cause
ingestion/pipeline.py around line 473:
vectors = await asyncio.get_event_loop().run_in_executor(
None,
embedder.embed,
[chunk.text for chunk in chunks], # ← no batch size limit
)OpenAI's embeddings API has a per-request token limit. BgeM3 processes all texts simultaneously in a single matrix operation. Neither embedder has an internal chunking loop.
Failure Scenario
- Tenant uploads a 400-page technical manual producing 3,000 chunks.
embedder.embed(all_3000_texts)is called.- OpenAI rejects the request (payload too large) → ingest fails with
APIError. - Or: BgeM3 attempts to allocate a 3,000 × 1024 float32 matrix →
MemoryError. - Document is permanently in
ingest_status='pending'with no sections.
Proposed Fix
EMBED_BATCH_SIZE = 256 # configurable via Settings
async def _embed_in_batches(
embedder: Embedder,
chunks: list[Chunk],
batch_size: int,
) -> list[list[float]]:
vectors = []
for i in range(0, len(chunks), batch_size):
batch = chunks[i : i + batch_size]
batch_vectors = await asyncio.get_event_loop().run_in_executor(
None,
embedder.embed,
[c.text for c in batch],
)
vectors.extend(batch_vectors)
return vectorsReplace the single embedder.embed(...) call with _embed_in_batches(embedder, chunks, EMBED_BATCH_SIZE). Add embed_batch_size: int = 256 to Settings.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingcode-reviewFound via automated code reviewFound via automated code reviewpriority: highData loss, security, or major correctness bugData loss, security, or major correctness bug
Activity
piyushmakhija28 commentedon Jun 21, 2026
Fixed in PR #280 (Sprint R3/R4: fix(sprint-r3): root-cause fixes for all 39 bugs from code review round 3 + Sprint R4 round 4)