microsoft/semantic-kernel · error · ServiceInvalidResponseError

Invalid response type from Ollama chat completion. Expected

Error message

Invalid response type from Ollama chat completion. Expected Mapping or ChatResponse but got {type(response_object)}.

What it means

Raised as ServiceInvalidResponseError when the object returned by `self.client.chat(..., stream=False)` is neither a `ChatResponse` (typed ollama SDK) nor a `Mapping` (raw dict, older/compat path). The connector can only build a ChatMessageContent from one of those two shapes, so anything else is treated as a protocol break.

Source

Thrown at python/semantic_kernel/connectors/ai/ollama/services/ollama_chat_completion.py:172

    ) -> list["ChatMessageContent"]:
        if not isinstance(settings, OllamaChatPromptExecutionSettings):
            settings = self.get_prompt_execution_settings_from_settings(settings)
        assert isinstance(settings, OllamaChatPromptExecutionSettings)  # nosec

        prepared_chat_history = self._prepare_chat_history_for_request(chat_history)

        response_object = await self.client.chat(
            model=self.ai_model_id,
            messages=prepared_chat_history,
            stream=False,
            **settings.prepare_settings_dict(),
        )

        if isinstance(response_object, ChatResponse):
            return [self._create_chat_message_content_from_chat_response(response_object)]
        if isinstance(response_object, Mapping):
            return [self._create_chat_message_content(response_object)]
        raise ServiceInvalidResponseError(
            "Invalid response type from Ollama chat completion. "
            f"Expected Mapping or ChatResponse but got {type(response_object)}."
        )

    @override
    @trace_streaming_chat_completion(OllamaBase.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, OllamaChatPromptExecutionSettings):
            settings = self.get_prompt_execution_settings_from_settings(settings)
        assert isinstance(settings, OllamaChatPromptExecutionSettings)  # nosec

        prepared_chat_history = self._prepare_chat_history_for_request(chat_history)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pin the ollama python package to the version semantic_kernel's connector expects (check the connector's requirements).
  2. If passing a custom client, ensure its `.chat(...)` returns either an ollama ChatResponse or a dict/Mapping.
  3. In tests, return ollama.ChatResponse(...) or a dict matching the documented schema.

Example fix

# before (custom test client)
class FakeClient:
    async def chat(self, **kw):
        return MyCustomObject()

# after
from ollama import ChatResponse
class FakeClient:
    async def chat(self, **kw):
        return ChatResponse.model_validate({'message': {'role':'assistant','content':'hi'}})
Defensive patterns

Strategy: type-guard

Validate before calling

# Validate the client returns the expected shape in a smoke test
resp = await svc.client.chat(model=svc.ai_model_id, messages=[{'role':'user','content':'hi'}], stream=False)
from ollama import ChatResponse
from collections.abc import Mapping
assert isinstance(resp, (ChatResponse, Mapping)), f'unexpected type {type(resp)}'

Type guard

from ollama import ChatResponse
from collections.abc import Mapping

def is_valid_ollama_chat_resp(obj) -> bool:
    return isinstance(obj, (ChatResponse, Mapping))

Try / catch

from semantic_kernel.exceptions import ServiceInvalidResponseError
try:
    resp = await svc.get_chat_message_contents(chat_history, settings)
except ServiceInvalidResponseError as e:
    if 'Invalid response type from Ollama chat' in str(e):
        raise RuntimeError('ollama SDK/server version mismatch - pin the expected package version') from e
    raise

Prevention

When it happens

Trigger: The installed `ollama` python package returns an unexpected response type from `client.chat` - e.g. a major SDK version change that returns a different class, a pydantic model that is not a Mapping, or a custom client wrapper returning a foreign object.

Common situations: Upgrading the ollama python SDK to a version whose ChatResponse lives elsewhere / has a different class; using a mocked/fake client in tests that returns a plain object; an ollama server version that returns an unexpected JSON shape that the SDK maps to a new type.

Related errors


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