microsoft/semantic-kernel · error · ServiceResponseException

{type(self)} service failed to complete the request

Error message

{type(self)} service failed to complete the request

What it means

Wraps any exception thrown by the Anthropic SDK's async_client.messages.create(...) during a non-streaming chat request. ServiceResponseException is a generic transport/API-level failure carrier; the original exception is chained via 'from ex' and passed as a second argument so callers can inspect the underlying AnthropicError (auth, rate limit, overload, invalid_request, etc.).

Source

Thrown at python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py:334

                metadata = metadata | {"usage": metadata.get("usage", {}) | {"output_tokens": output_tokens}}

        return StreamingChatMessageContent(
            choice_index=0,
            inner_content=stream_event,
            ai_model_id=self.ai_model_id,
            metadata=metadata,
            role=AuthorRole.ASSISTANT,
            finish_reason=finish_reason,
            items=items,
            function_invoke_attempt=function_invoke_attempt,
        )

    async def _send_chat_request(self, settings: AnthropicChatPromptExecutionSettings) -> list["ChatMessageContent"]:
        """Send the chat request."""
        try:
            response = await self.async_client.messages.create(**settings.prepare_settings_dict())
        except Exception as ex:
            raise ServiceResponseException(
                f"{type(self)} service failed to complete the request",
                ex,
            ) from ex

        response_metadata: dict[str, Any] = {"id": response.id}
        if hasattr(response, "usage") and response.usage is not None:
            response_metadata["usage"] = response.usage

        return [self._create_chat_message_content(response, response_metadata)]

    async def _send_chat_stream_request(
        self,
        settings: AnthropicChatPromptExecutionSettings,
        function_invoke_attempt: int = 0,
    ) -> AsyncGenerator[list["StreamingChatMessageContent"], None]:
        """Send the chat stream request.

        The stream yields a sequence of stream events, which are used to create streaming chat message content:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained exception (ex / __cause__) to identify the Anthropic error type and act on it (re-auth, back off, fix payload).
  2. For 429/529: implement exponential backoff with jitter and retry; consider reducing request frequency or upgrading rate limits.
  3. Verify ANTHROPIC_API_KEY is set and valid, the model_id is correct and accessible, and settings.prepare_settings_dict() produces a valid payload.
  4. For network timeouts, configure HTTP retries/timeouts on the async client.

Example fix

from semantic_kernel.exceptions import ServiceResponseException

try:
    result = await service.get_chat_message_contents(history=history, settings=settings)
except ServiceResponseException as e:
    cause = e.__cause__
    print(type(cause), cause)
Defensive patterns

Strategy: retry

Validate before calling

from semantic_kernel.exceptions import ServiceResponseException

# Pre-flight: basic payload sanity (cannot fully prevent transient/network errors)
assert settings.model_id, "model_id must be set"
assert history and len(history) > 0, "history must not be empty"

Try / catch

from semantic_kernel.exceptions import ServiceResponseException
import asyncio

async def call_with_retry(service, history, settings, attempts=4):
    for attempt in range(attempts):
        try:
            return await service.get_chat_message_contents(history=history, settings=settings)
        except ServiceResponseException as e:
            cause = e.__cause__
            name = type(cause).__name__ if cause else ""
            if name in {"RateLimitError", "APIStatusError", "OverloadedError"} and attempt < attempts - 1:
                await asyncio.sleep(2 ** attempt)
                continue
            raise

Prevention

When it happens

Trigger: Any failure in the HTTP call to the Anthropic Messages endpoint: expired/invalid API key (AuthenticationError), 429 rate limiting, 529 overloaded, malformed request_body from settings.prepare_settings_dict(), network timeout, or SDK internal errors.

Common situations: Wrong/expired ANTHROPIC_API_KEY or Anthropic settings; hitting token-per-minute or request-per-minute quotas; sending more max_tokens than the model allows; model_id typo or access to a model not enabled for the key; transient network issues or Anthropic-side outages (529).

Related errors


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