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 GenerateResponse but got {type(response_object)}.

What it means

Raised inside _inner_get_text_contents after a non-streaming call to self.client.generate(stream=False). The ollama Python client is expected to return either a dict-like Mapping or a typed GenerateResponse object; if it returns anything else, the connector cannot extract the 'response' field and raises ServiceInvalidResponseError. Note the message text says 'chat completion' but this is the text-completion path.

Source

Thrown at python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py:115

    @trace_text_completion(OllamaBase.MODEL_PROVIDER_NAME)
    async def _inner_get_text_contents(
        self,
        prompt: str,
        settings: "PromptExecutionSettings",
    ) -> list[TextContent]:
        if not isinstance(settings, OllamaTextPromptExecutionSettings):
            settings = self.get_prompt_execution_settings_from_settings(settings)
        assert isinstance(settings, OllamaTextPromptExecutionSettings)  # nosec

        response_object = await self.client.generate(
            model=self.ai_model_id,
            prompt=prompt,
            stream=False,
            **settings.prepare_settings_dict(),
        )

        if not isinstance(response_object, (Mapping, GenerateResponse)):
            raise ServiceInvalidResponseError(
                "Invalid response type from Ollama chat completion. "
                f"Expected Mapping or GenerateResponse but got {type(response_object)}."
            )
        return [
            TextContent(
                inner_content=response_object,
                ai_model_id=self.ai_model_id,
                text=response_object.response
                if isinstance(response_object, GenerateResponse)
                else response_object["response"],
            )
        ]

    @override
    @trace_streaming_text_completion(OllamaBase.MODEL_PROVIDER_NAME)
    async def _inner_get_streaming_text_contents(
        self,
        prompt: str,

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Verify the ollama Python package version is compatible: pip show ollama and check against the connector's requirements
  2. Ensure the host URL points at a real Ollama server (default http://127.0.0.1:11434), not a proxy returning a different schema
  3. Check the Ollama server is running and the model is pulled: ollama list
  4. Update semantic_kernel to a version matching your ollama package
Defensive patterns

Strategy: try-catch

Type guard

from collections.abc import Mapping
from ollama._types import GenerateResponse

def is_valid_ollama_response(obj) -> bool:
    return isinstance(obj, (Mapping, GenerateResponse))

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInvalidResponseError

try:
    result = await ollama.get_text_contents(prompt='Hello', settings=settings)
except ServiceInvalidResponseError as e:
    logger.error('Ollama returned unexpected type: %s', e)
    raise

Prevention

When it happens

Trigger: Calling get_text_contents or the kernel text-completion pipeline when the Ollama client returns an unexpected type — e.g. an ollama library version change that alters the return shape, a proxy that returns a non-JSON or error object instead of a generate response, or the client returning None on a connection failure.

Common situations: Upgrading or downgrading the ollama Python package to a version with a different GenerateResponse type hierarchy; pointing at a non-Ollama endpoint (reverse proxy, LiteLLM) that returns a different JSON structure; intermittent network errors where the client swallows the HTTP error and returns an error dict that is a Mapping but not recognized.

Related errors


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