{"record":{"id":"0797d2d81cc36846","repo":"microsoft/semantic-kernel","slug":"invalid-response-type-from-ollama-chat-completion-0797d2","errorCode":null,"errorMessage":"Invalid response type from Ollama chat completion. Expected Mapping or GenerateResponse but got {type(response_object)}.","messagePattern":"Invalid response type from Ollama chat completion\\. Expected Mapping or GenerateResponse but got (.+?)\\.","errorType":"exception","errorClass":"ServiceInvalidResponseError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py","lineNumber":115,"sourceCode":"    @trace_text_completion(OllamaBase.MODEL_PROVIDER_NAME)\n    async def _inner_get_text_contents(\n        self,\n        prompt: str,\n        settings: \"PromptExecutionSettings\",\n    ) -> list[TextContent]:\n        if not isinstance(settings, OllamaTextPromptExecutionSettings):\n            settings = self.get_prompt_execution_settings_from_settings(settings)\n        assert isinstance(settings, OllamaTextPromptExecutionSettings)  # nosec\n\n        response_object = await self.client.generate(\n            model=self.ai_model_id,\n            prompt=prompt,\n            stream=False,\n            **settings.prepare_settings_dict(),\n        )\n\n        if not isinstance(response_object, (Mapping, GenerateResponse)):\n            raise ServiceInvalidResponseError(\n                \"Invalid response type from Ollama chat completion. \"\n                f\"Expected Mapping or GenerateResponse but got {type(response_object)}.\"\n            )\n        return [\n            TextContent(\n                inner_content=response_object,\n                ai_model_id=self.ai_model_id,\n                text=response_object.response\n                if isinstance(response_object, GenerateResponse)\n                else response_object[\"response\"],\n            )\n        ]\n\n    @override\n    @trace_streaming_text_completion(OllamaBase.MODEL_PROVIDER_NAME)\n    async def _inner_get_streaming_text_contents(\n        self,\n        prompt: str,","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py#L97-L133","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the ollama Python package version is compatible: pip show ollama and check against the connector's requirements","Ensure the host URL points at a real Ollama server (default http://127.0.0.1:11434), not a proxy returning a different schema","Check the Ollama server is running and the model is pulled: ollama list","Update semantic_kernel to a version matching your ollama package"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"from collections.abc import Mapping\nfrom ollama._types import GenerateResponse\n\ndef is_valid_ollama_response(obj) -> bool:\n    return isinstance(obj, (Mapping, GenerateResponse))","tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInvalidResponseError\n\ntry:\n    result = await ollama.get_text_contents(prompt='Hello', settings=settings)\nexcept ServiceInvalidResponseError as e:\n    logger.error('Ollama returned unexpected type: %s', e)\n    raise","preventionTips":["Pin the ollama Python package version to match the connector's tested version","Verify the Ollama server endpoint returns standard generate responses before integrating","Run a smoke-test request after constructing the service to catch version mismatches early"],"tags":["ollama","response-validation","version-mismatch","runtime"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}