{"record":{"id":"d04f65e388afb17f","repo":"crewAIInc/crewAI","slug":"failed-to-generate-embedding-e","errorCode":null,"errorMessage":"Failed to generate embedding: {e}","messagePattern":"Failed to generate embedding: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/rag/embedding_service.py","lineNumber":270,"sourceCode":"\n        Returns:\n            List of floats representing the embedding\n\n        Raises:\n            RuntimeError: If embedding generation fails\n        \"\"\"\n        if not text or not text.strip():\n            logger.warning(\"Empty text provided for embedding\")\n            return []\n\n        try:\n            # Use ChromaDB's embedding function interface\n            embeddings = self._embedding_function([text])  # type: ignore\n            return list(embeddings[0]) if embeddings else []\n\n        except Exception as e:\n            logger.error(f\"Error generating embedding for text: {e}\")\n            raise RuntimeError(f\"Failed to generate embedding: {e}\") from e\n\n    def embed_batch(self, texts: list[str]) -> list[list[float]]:\n        \"\"\"\n        Generate embeddings for multiple texts.\n\n        Args:\n            texts: List of texts to embed\n\n        Returns:\n            List of embedding vectors\n\n        Raises:\n            RuntimeError: If embedding generation fails\n        \"\"\"\n        if not texts:\n            return []\n\n        valid_texts = [text for text in texts if text and text.strip()]","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/rag/embedding_service.py#L252-L288","documentation":"EmbeddingService.embed_text catches every exception from calling the underlying embedding function on a single text and re-raises as RuntimeError('Failed to generate embedding: ...'). Common roots: provider API auth errors, rate limits, network failures, or a malformed text payload. Empty/whitespace-only text is short-circuited to [] before this path and only logs a warning.","triggerScenarios":"Expired/invalid API key on the provider call; rate limiting (429) from OpenAI/other providers; transient network outage; text containing content the provider rejects; batch interface returning an unexpected shape.","commonSituations":"Long-running RAG ingestion exhausting quotas; rotated API keys not updated in env; flaky connectivity in containers; embedding at document count spikes.","solutions":["Inspect e.__cause__: 401/403 -> fix API key; 429 -> back off and retry; timeout -> check network/proxy","Add retry with exponential backoff for 429/5xx provider errors","Ensure the provider key env var is set and current before ingestion runs","Batch via embed_batch instead of per-text calls to reduce request count and rate-limit pressure"],"exampleFix":"# before\nvec = service.embed_text(text)\n# after\nfor attempt in range(5):\n    try:\n        vec = service.embed_text(text)\n        break\n    except RuntimeError as e:\n        if '429' in str(e.__cause__ or '') and attempt < 4:\n            time.sleep(2 ** attempt); continue\n        raise","handlingStrategy":"retry","validationCode":"if not text or not text.strip():\n    return []  # skip empty, mirroring the service's own guard","typeGuard":"def is_embeddable_text(t: str) -> bool:\n    return bool(t and t.strip())","tryCatchPattern":"for attempt in range(4):\n    try:\n        vec = service.embed_text(text); break\n    except RuntimeError as e:\n        msg = str(e.__cause__ or '')\n        if ('429' in msg or 'timeout' in msg.lower()) and attempt < 3:\n            time.sleep(2 ** attempt); continue\n        raise","preventionTips":["Retry only transient causes (429, timeouts) with exponential backoff","Check API keys and quotas before long ingestion runs","Prefer embed_batch over per-text calls to cut request volume"],"tags":["rag","embeddings","api","retryable"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}