microsoft/semantic-kernel · error · ServiceInvalidResponseError

Expected an AsyncGenerator response.

Error message

Expected an AsyncGenerator response.

What it means

In the streaming path, _inner_get_streaming_chat_message_contents calls _send_chat_stream_request and asserts the result is an AsyncGenerator before iterating. If it is not, ServiceInvalidResponseError is raised. _send_chat_stream_request is itself an async generator (it uses `yield`), so in normal operation this invariant always holds; the error indicates a broken override or an abnormal return.

Source

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

    @trace_streaming_chat_completion(MODEL_PROVIDER_NAME)
    async def _inner_get_streaming_chat_message_contents(
        self,
        chat_history: "ChatHistory",
        settings: "PromptExecutionSettings",
        function_invoke_attempt: int = 0,
    ) -> AsyncGenerator[list["StreamingChatMessageContent"], Any]:
        if not isinstance(settings, AnthropicChatPromptExecutionSettings):
            settings = self.get_prompt_execution_settings_from_settings(settings)
        assert isinstance(settings, AnthropicChatPromptExecutionSettings)  # nosec

        settings.messages, parsed_system_message = self._prepare_chat_history_for_request(chat_history, stream=True)
        settings.ai_model_id = settings.ai_model_id or self.ai_model_id
        if settings.system is None and parsed_system_message is not None:
            settings.system = parsed_system_message

        response = self._send_chat_stream_request(settings, function_invoke_attempt)
        if not isinstance(response, AsyncGenerator):
            raise ServiceInvalidResponseError("Expected an AsyncGenerator response.")

        async for message in response:
            yield message

    @override
    def _prepare_chat_history_for_request(
        self,
        chat_history: "ChatHistory",
        role_key: str = "role",
        content_key: str = "content",
        stream: bool = False,
    ) -> tuple[list[dict[str, Any]], str | None]:
        """Prepare the chat history for an Anthropic request.

        Allowing customization of the key names for role/author, and optionally overriding the role.

        Args:
            chat_history: The chat history to prepare.

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. If subclassing, keep _send_chat_stream_request as an async generator (it must use `yield`).
  2. Pin/upgrade the anthropic SDK to a version compatible with this Semantic Kernel release.
  3. Do not monkeypatch or replace the streaming method; if you must, preserve the AsyncGenerator return type.
  4. Use the standard streaming API (get_streaming_chat_message_content) rather than calling internals.

Example fix

// before (broken subclass override)
async def _send_chat_stream_request(self, settings, attempt=0):
    return await self.async_client.messages.stream(**settings.prepare_settings_dict())  # not a generator

// after
async def _send_chat_stream_request(self, settings, attempt=0):
    async with self.async_client.messages.stream(**settings.prepare_settings_dict()) as stream:
        async for event in stream:
            yield [self._create_streaming_chat_message_content(event, {}, attempt)]
Defensive patterns

Strategy: type-guard

Validate before calling

from collections.abc import AsyncGenerator
import inspect
fn = service._send_chat_stream_request
assert inspect.isasyncgenfunction(fn), '_send_chat_stream_request must be an async generator'

Type guard

def streaming_method_is_async_generator(service) -> bool:
    return inspect.isasyncgenfunction(service._send_chat_stream_request)

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInvalidResponseError
try:
    async for chunk in service._inner_get_streaming_chat_message_contents(history, settings):
        ...
except ServiceInvalidResponseError:
    # fall back to non-streaming completion
    result = await service._inner_get_chat_message_contents(history, settings)

Prevention

When it happens

Trigger: A subclass overrides _send_chat_stream_request to return something that is not an async generator (e.g. a plain coroutine or list); an early code path returns a non-generator; an anthropic SDK/version mismatch alters the streaming contract.

Common situations: Custom subclass of AnthropicChatCompletion that breaks the streaming method's generator contract; using an incompatible anthropic SDK version; monkeypatching that replaces the method.

Related errors


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