{"record":{"id":"d79c836cd4b13c50","repo":"microsoft/semantic-kernel","slug":"hugging-face-embeddings-failed","errorCode":null,"errorMessage":"Hugging Face embeddings failed","messagePattern":"Hugging Face embeddings failed","errorType":"exception","errorClass":"ServiceResponseException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/hugging_face/services/hf_text_embedding.py","lineNumber":75,"sourceCode":"            device=resolved_device,\n            generator=SentenceTransformer(  # type: ignore\n                model_name_or_path=ai_model_id,\n                device=resolved_device,\n            ),\n        )\n\n    @override\n    async def generate_embeddings(\n        self,\n        texts: list[str],\n        settings: \"PromptExecutionSettings | None\" = None,\n        **kwargs: Any,\n    ) -> ndarray:\n        try:\n            logger.info(f\"Generating embeddings for {len(texts)} texts.\")\n            return self.generator.encode(sentences=texts, convert_to_numpy=True, **kwargs)\n        except Exception as e:\n            raise ServiceResponseException(\"Hugging Face embeddings failed\", e) from e\n\n    @override\n    async def generate_raw_embeddings(\n        self,\n        texts: list[str],\n        settings: \"PromptExecutionSettings | None\" = None,\n        **kwargs: Any,\n    ) -> \"list[Tensor] | ndarray | Tensor\":\n        try:\n            logger.info(f\"Generating raw embeddings for {len(texts)} texts.\")\n            return self.generator.encode(sentences=texts, **kwargs)\n        except Exception as e:\n            raise ServiceResponseException(\"Hugging Face embeddings failed\", e) from e\n","sourceCodeStart":57,"sourceCodeEnd":89,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/hugging_face/services/hf_text_embedding.py#L57-L89","documentation":"Raised by HuggingFaceTextEmbedding.generate_embeddings when self.generator.encode(...) (SentenceTransformer) throws while producing numpy embeddings. The connector wraps any exception into a ServiceResponseException chained from the cause, so the real failure (model load error, bad input, device error) is in __cause__.","triggerScenarios":"Calling generate_embeddings with inputs that fail SentenceTransformer.encode: empty/None texts, non-string entries, a model that failed to load, CUDA errors, or encode-time exceptions.","commonSituations":"Passing an empty list or list containing None/non-string values. Embedding model not downloaded/cached. GPU OOM on large batches.","solutions":["Inspect the chained __cause__ for the actual SentenceTransformer error.","Sanitize inputs: ensure texts is a non-empty list of strings before calling.","Confirm the embedding model id is valid and fully downloaded.","For GPU OOM, batch the texts into smaller chunks or run on CPU."],"exampleFix":"# before\nemb = await svc.generate_embeddings(texts)  # may contain None\n# after\ntexts = [t for t in texts if isinstance(t, str) and t]\nif texts:\n    emb = await svc.generate_embeddings(texts)","handlingStrategy":"try-catch","validationCode":"assert texts and all(isinstance(t, str) and t for t in texts), 'texts must be non-empty strings'","typeGuard":null,"tryCatchPattern":"try:\n    emb = await svc.generate_embeddings(texts)\nexcept ServiceResponseException as e:\n    logger.error('hf embeddings failed: %r', e.__cause__)\n    raise","preventionTips":["Sanitize inputs to non-empty strings before encoding.","Confirm the embedding model is downloaded.","Batch large inputs to avoid OOM."],"tags":["hugging-face","embeddings","runtime","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}