{"record":{"id":"a8811a0985b08f6c","repo":"microsoft/semantic-kernel","slug":"invalid-response-type-from-ollama-streaming-chat-c-a8811a","errorCode":null,"errorMessage":"Invalid response type from Ollama streaming chat completion. Expected mapping or ChatResponse but got {type(part)}.","messagePattern":"Invalid response type from Ollama streaming chat completion\\. Expected mapping or ChatResponse but got (.+?)\\.","errorType":"exception","errorClass":"ServiceInvalidResponseError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/ollama/services/ollama_chat_completion.py","lineNumber":211,"sourceCode":"            messages=prepared_chat_history,\n            stream=True,\n            **settings.prepare_settings_dict(),\n        )\n\n        if not isinstance(response_object, AsyncIterator):\n            raise ServiceInvalidResponseError(\n                \"Invalid response type from Ollama streaming chat completion. \"\n                f\"Expected AsyncIterator but got {type(response_object)}.\"\n            )\n\n        async for part in response_object:\n            if isinstance(part, ChatResponse):\n                yield [self._create_streaming_chat_message_content_from_chat_response(part, function_invoke_attempt)]\n                continue\n            if isinstance(part, Mapping):\n                yield [self._create_streaming_chat_message_content(part, function_invoke_attempt)]\n                continue\n            raise ServiceInvalidResponseError(\n                \"Invalid response type from Ollama streaming chat completion. \"\n                f\"Expected mapping or ChatResponse but got {type(part)}.\"\n            )\n\n    # endregion\n\n    def _create_streaming_chat_message_content_from_chat_response(\n        self,\n        response: ChatResponse,\n        function_invoke_attempt: int,\n    ) -> StreamingChatMessageContent:\n        \"\"\"Create a chat message content from the response.\"\"\"\n        items: list[STREAMING_ITEM_TYPES] = []\n        if response.message.content:\n            items.append(\n                StreamingTextContent(\n                    choice_index=0,\n                    text=response.message.content,","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/ollama/services/ollama_chat_completion.py#L193-L229","documentation":"Raised as ServiceInvalidResponseError while iterating the streaming AsyncIterator, when an individual yielded `part` is neither a `ChatResponse` nor a `Mapping`. The outer iterator type was fine (passed the 1115 check), but one of its elements has an unrecognized shape, so the connector cannot build a StreamingChatMessageContent from it.","triggerScenarios":"A streaming response from the ollama client that mixes in an unexpected object type among the chunks - e.g. a SDK version that yields a different chunk class, a custom async generator yielding heterogeneous items, or a server that emits an error/control object mid-stream.","commonSituations":"Upgrading the ollama SDK so its stream yields new chunk types; a custom proxy injecting metadata objects into the stream; a test fake generator yielding strings instead of ChatResponse/dict.","solutions":["Ensure every item your client's streaming generator yields is an ollama ChatResponse or a dict/Mapping.","Pin/align the ollama python SDK version with the connector's expected chunk type.","In tests, yield only dicts or ChatResponse instances from the fake async generator."],"exampleFix":"# before\nasync def chat(self, **kw):\n    yield 'partial text'   # not a ChatResponse or Mapping\n\n# after\nasync def chat(self, **kw):\n    yield {'message': {'role':'assistant','content':'partial text'}}","handlingStrategy":"type-guard","validationCode":"# Verify each streamed part type in a smoke test\nfrom ollama import ChatResponse\nfrom collections.abc import Mapping, AsyncIterator\nasync for part in await svc.client.chat(model=svc.ai_model_id, messages=[...], stream=True):\n    assert isinstance(part, (ChatResponse, Mapping)), f'bad part type {type(part)}'","typeGuard":"from ollama import ChatResponse\nfrom collections.abc import Mapping\n\ndef is_valid_ollama_stream_part(part) -> bool:\n    return isinstance(part, (ChatResponse, Mapping))","tryCatchPattern":"from semantic_kernel.exceptions import ServiceInvalidResponseError\ntry:\n    async for chunk in svc._inner_get_streaming_chat_message_contents(chat_history, settings):\n        ...\nexcept ServiceInvalidResponseError as e:\n    if 'Expected mapping or ChatResponse' in str(e):\n        raise RuntimeError('streaming chunk type unsupported by this ollama version') from e\n    raise","preventionTips":["Ensure custom generators yield only dicts or ollama.ChatResponse instances.","Pin the ollama SDK version to match connector expectations.","Filter out control/non-message parts in a wrapper client if your server emits them."],"tags":["ollama","streaming","chat-completion","response-shape","sdk-version","service-invalid-response-error"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}