microsoft/semantic-kernel · error · ServiceResponseException

{type(self)} service failed to generate embeddings

Error message

{type(self)} service failed to generate embeddings

What it means

Raised as ServiceResponseException in _send_embedding_request when any exception occurs during client.embeddings.create. The handler catches all exceptions and wraps them, meaning the original cause (network error, invalid input, rate limit, unsupported model) is available in ex.__cause__.

Source

Thrown at python/semantic_kernel/connectors/ai/open_ai/services/open_ai_handler.py:118

            raise ServiceResponseException(
                f"{type(self)} service failed to complete the prompt",
                ex,
            ) from ex
        except Exception as ex:
            raise ServiceResponseException(
                f"{type(self)} service failed to complete the prompt",
                ex,
            ) from ex

    async def _send_embedding_request(self, settings: OpenAIEmbeddingPromptExecutionSettings) -> list[Any]:
        """Send a request to the OpenAI embeddings endpoint."""
        try:
            response = await self.client.embeddings.create(**settings.prepare_settings_dict())

            self.store_usage(response)
            return [x.embedding for x in response.data]
        except Exception as ex:
            raise ServiceResponseException(
                f"{type(self)} service failed to generate embeddings",
                ex,
            ) from ex

    async def _send_text_to_image_request(self, settings: OpenAITextToImageExecutionSettings) -> ImagesResponse:
        """Send a request to the OpenAI text to image endpoint."""
        try:
            response: ImagesResponse = await self.client.images.generate(
                **settings.prepare_settings_dict(),
            )
            self.store_usage(response)
            return response
        except Exception as ex:
            raise ServiceResponseException(f"Failed to generate image: {ex}") from ex

    async def _send_image_edit_request(
        self,
        image: list[FileTypes],

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Check ex.__cause__ for the specific OpenAI SDK exception to diagnose root cause
  2. Ensure the input texts list is non-empty and each text is within the model's token limit
  3. Verify the embedding model id is current and supported (e.g., text-embedding-3-small)
  4. For rate limits, batch fewer inputs per request or implement backoff

Example fix

# before
embeddings = await service.generate_embeddings([])
# after — validate input first
texts = [t for t in texts if t.strip()]
if not texts:
    raise ValueError('No non-empty texts to embed')
embeddings = await service.generate_embeddings(texts)
Defensive patterns

Strategy: retry

Validate before calling

texts = [t for t in texts if t and t.strip()]
if not texts:
    raise ValueError('Cannot generate embeddings for an empty input list')
MAX_TOKENS = 8191  # for text-embedding-3-small
for t in texts:
    if len(t) > 20000:  # rough heuristic guard
        logger.warning('Text may exceed model token limit')

Try / catch

from semantic_kernel.exceptions import ServiceResponseException
from openai import RateLimitError

try:
    embeddings = await service.generate_embeddings(texts)
except ServiceResponseException as e:
    if isinstance(e.__cause__, RateLimitError):
        await asyncio.sleep(backoff)
        embeddings = await service.generate_embeddings(texts)
    raise

Prevention

When it happens

Trigger: Calling the embedding generation path with invalid inputs: empty text list, text exceeding the model's token limit, an unsupported embedding model id, a network failure, or a rate-limit error during client.embeddings.create.

Common situations: Passing an empty list of texts to embed; using a deprecated embedding model name; batch input that exceeds per-request token limits; network or quota issues during high-volume embedding generation.

Related errors


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