{"record":{"id":"ad550b851ae75096","repo":"crewAIInc/crewAI","slug":"failed-to-generate-batch-embeddings-e","errorCode":null,"errorMessage":"Failed to generate batch embeddings: {e}","messagePattern":"Failed to generate batch embeddings: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/rag/embedding_service.py","lineNumber":306,"sourceCode":"        valid_texts = [text for text in texts if text and text.strip()]\n        if not valid_texts:\n            logger.warning(\"No valid texts provided for batch embedding\")\n            return []\n\n        try:\n            # Process in batches to avoid API limits\n            all_embeddings: list[list[float]] = []\n\n            for i in range(0, len(valid_texts), self.config.batch_size):\n                batch = valid_texts[i : i + self.config.batch_size]\n                batch_embeddings = self._embedding_function(batch)  # type: ignore\n                all_embeddings.extend(list(e) for e in batch_embeddings)\n\n            return all_embeddings\n\n        except Exception as e:\n            logger.error(f\"Error generating batch embeddings: {e}\")\n            raise RuntimeError(f\"Failed to generate batch embeddings: {e}\") from e\n\n    def get_embedding_dimension(self) -> int | None:\n        \"\"\"\n        Get the dimension of embeddings produced by this service.\n\n        Returns:\n            Embedding dimension or None if unknown\n        \"\"\"\n        # Try to get dimension by generating a test embedding\n        try:\n            test_embedding = self.embed_text(\"test\")\n            return len(test_embedding) if test_embedding else None\n        except Exception:\n            logger.warning(\"Could not determine embedding dimension\")\n            return None\n\n    def validate_connection(self) -> bool:\n        \"\"\"","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/rag/embedding_service.py#L288-L324","documentation":"EmbeddingService.embed_batch validates texts, then slices them into groups of config.batch_size and calls the provider embedding function per batch; any exception (auth, rate limit, network, oversized batch rejected by provider, unexpected return shape) is caught and re-raised as RuntimeError('Failed to generate batch embeddings: ...'). The cause chain preserves the provider error.","triggerScenarios":"batch_size larger than the provider's per-request limit (e.g. >2048 inputs for OpenAI); a 429 triggered by rapid sequential batches; one malformed text in the batch failing the whole call; provider returning fewer embeddings than inputs.","commonSituations":"Bulk document ingestion in RAG pipelines; batch_size copied from docs of a different provider; running ingestion concurrently from multiple workers tripping shared rate limits.","solutions":["Lower config.batch_size to a provider-safe value (e.g. 64-256)","Read e.__cause__ to distinguish auth (fix key) vs 429 (back off) vs validation (inspect texts)","Retry the failing batch alone to identify a poison text; sanitize/remove it","Stagger concurrent ingestion workers or add a global rate limiter"],"exampleFix":"# before\nvecs = service.embed_batch(all_texts)\n# after\nvecs = []\nfor i in range(0, len(all_texts), 64):\n    chunk = all_texts[i:i+64]\n    try:\n        vecs.extend(service.embed_batch(chunk))\n    except RuntimeError:\n        time.sleep(2); vecs.extend(service.embed_batch(chunk))  # naive retry for 429","handlingStrategy":"retry","validationCode":"BATCH_SIZE = 64  # provider-safe value\ntexts = [t for t in texts if t and t.strip()]  # drop empties that can poison batches\nfor i in range(0, len(texts), BATCH_SIZE):\n    ...","typeGuard":"def is_valid_batch(texts: list[str], limit: int) -> bool:\n    return all(t and t.strip() for t in texts) and len(texts) <= limit","tryCatchPattern":"results = []\nfor i in range(0, len(texts), 64):\n    for attempt in range(3):\n        try:\n            results.extend(service.embed_batch(texts[i:i+64])); break\n        except RuntimeError:\n            if attempt == 2: raise\n            time.sleep(2 ** attempt)","preventionTips":["Cap batch_size well below provider limits (64-256)","Filter empty/whitespace texts before batching","Rate-limit concurrent workers sharing one provider key"],"tags":["rag","embeddings","batching","retryable"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}