microsoft/semantic-kernel · error · ServiceResponseException

Hugging Face embeddings failed

Error message

Hugging Face embeddings failed

What it means

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__.

Source

Thrown at python/semantic_kernel/connectors/ai/hugging_face/services/hf_text_embedding.py:75

            device=resolved_device,
            generator=SentenceTransformer(  # type: ignore
                model_name_or_path=ai_model_id,
                device=resolved_device,
            ),
        )

    @override
    async def generate_embeddings(
        self,
        texts: list[str],
        settings: "PromptExecutionSettings | None" = None,
        **kwargs: Any,
    ) -> ndarray:
        try:
            logger.info(f"Generating embeddings for {len(texts)} texts.")
            return self.generator.encode(sentences=texts, convert_to_numpy=True, **kwargs)
        except Exception as e:
            raise ServiceResponseException("Hugging Face embeddings failed", e) from e

    @override
    async def generate_raw_embeddings(
        self,
        texts: list[str],
        settings: "PromptExecutionSettings | None" = None,
        **kwargs: Any,
    ) -> "list[Tensor] | ndarray | Tensor":
        try:
            logger.info(f"Generating raw embeddings for {len(texts)} texts.")
            return self.generator.encode(sentences=texts, **kwargs)
        except Exception as e:
            raise ServiceResponseException("Hugging Face embeddings failed", e) from e

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained __cause__ for the actual SentenceTransformer error.
  2. Sanitize inputs: ensure texts is a non-empty list of strings before calling.
  3. Confirm the embedding model id is valid and fully downloaded.
  4. For GPU OOM, batch the texts into smaller chunks or run on CPU.

Example fix

# before
emb = await svc.generate_embeddings(texts)  # may contain None
# after
texts = [t for t in texts if isinstance(t, str) and t]
if texts:
    emb = await svc.generate_embeddings(texts)
Defensive patterns

Strategy: try-catch

Validate before calling

assert texts and all(isinstance(t, str) and t for t in texts), 'texts must be non-empty strings'

Try / catch

try:
    emb = await svc.generate_embeddings(texts)
except ServiceResponseException as e:
    logger.error('hf embeddings failed: %r', e.__cause__)
    raise

Prevention

When it happens

Trigger: 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.

Common situations: Passing an empty list or list containing None/non-string values. Embedding model not downloaded/cached. GPU OOM on large batches.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/d79c836cd4b13c50. Report an issue: GitHub.