{"record":{"id":"da59431fe59a15a5","repo":"BerriAI/litellm","slug":"failed-to-generate-embedding-e","errorCode":null,"errorMessage":"Failed to generate embedding: {e}","messagePattern":"Failed to generate embedding: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/caching/redis_semantic_cache.py","lineNumber":510,"sourceCode":"        router: Final = resolve_embedding_router(self.embedding_model, llm_router, llm_model_list)\n        try:\n            if router is not None:\n                embedding_response = await router.aembedding(\n                    model=self.embedding_model,\n                    input=prompt,\n                    cache={\"no-store\": True, \"no-cache\": True},\n                    metadata=build_router_embedding_metadata(metadata),\n                )\n            else:\n                embedding_response = await litellm.aembedding(\n                    model=self.embedding_model,\n                    input=prompt,\n                    cache={\"no-store\": True, \"no-cache\": True},\n                )\n            return embedding_response[\"data\"][0][\"embedding\"]\n        except Exception as e:\n            print_verbose(f\"Error generating async embedding: {e}\")\n            raise ValueError(f\"Failed to generate embedding: {e}\") from e\n\n    async def async_set_cache(self, key: str, value: object, **kwargs) -> None:\n        \"\"\"\n        Asynchronously store a value in the semantic cache.\n\n        Args:\n            key: The cache key used to isolate semantic cache entries\n            value: The response value to cache\n            **kwargs: Additional arguments including 'messages' for the prompt\n                and optional 'ttl' for time-to-live\n        \"\"\"\n        print_verbose(f\"Async Redis semantic-cache set_cache, kwargs: {kwargs}\")\n\n        try:\n            prompt: Final = self._get_prompt_from_kwargs(**kwargs)\n            if prompt is None:\n                print_verbose(\"No prompt provided for semantic caching\")\n                return","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/caching/redis_semantic_cache.py#L492-L528","documentation":"Before a semantic-cache lookup or store, the cache embeds the prompt by calling litellm.aembedding with the configured embedding_model (default text-embedding-ada-002). Any failure in that embedding call — auth error, unknown model, provider outage, malformed response — is caught, logged via print_verbose, and re-raised as ValueError('Failed to generate embedding: ...') with the original cause chained. The original exception text is embedded in the message and is the key to diagnosis.","triggerScenarios":"Semantic caching enabled without a valid OPENAI_API_KEY (for the default ada-002 model); embedding_model set to a model the configured providers don't serve; embedding provider rate-limited or down; router missing a deployment for the embedding model when aembedding is dispatched through it.","commonSituations":"Users enable redis-semantic caching for chat completions but never configure embedding credentials; switching embedding models without updating vector_size/index dims; OpenAI key expired or scoped without embedding access.","solutions":["Read the chained cause in the message — it names the real failure (401, model not found, timeout)","Set a valid key for the embedding model, e.g. os.environ['OPENAI_API_KEY'] = 'sk-...' when using the default ada-002","Point embedding_model at a model you actually serve, e.g. embedding_model='bedrock/...'|'azure/...'","Verify litellm.aembedding(model=..., input=['ping']) works standalone before enabling semantic caching"],"exampleFix":"# before\ncache = RedisSemanticCache(redis_url=url, similarity_threshold=0.8)  # no OPENAI_API_KEY\n\n# after\nos.environ['OPENAI_API_KEY'] = 'sk-...'\ncache = RedisSemanticCache(redis_url=url, similarity_threshold=0.8,\n                           embedding_model='text-embedding-ada-002')","handlingStrategy":"try-catch","validationCode":"import litellm\n\nasync def embedding_ok(model: str) -> bool:\n    try:\n        r = await litellm.aembedding(model=model, input=['ping'], cache={'no-store': True})\n        return bool(r['data'][0]['embedding'])\n    except Exception:\n        return False\n\n# gate semantic caching on this check at startup","typeGuard":null,"tryCatchPattern":"try:\n    resp = await litellm.acompletion(...)\nexcept ValueError as e:\n    if 'Failed to generate embedding' in str(e):\n        logger.warning('Semantic cache embedding failed; retrying with cache disabled')\n        kwargs['cache']['no-cache'] = True\n        resp = await litellm.acompletion(...)\n    else:\n        raise","preventionTips":["Verify embedding credentials at startup with a one-token ping","Scope semantic caching behind a feature flag so embedding outages degrade to normal caching","Log the chained cause — it distinguishes auth vs model-not-found vs provider outage"],"tags":["redis","semantic-cache","embedding","authentication","openai"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}