{"record":{"id":"0a87ed7f88ccdd38","repo":"microsoft/semantic-kernel","slug":"invalid-response-type-from-ollama-chat-completion-0a87ed","errorCode":null,"errorMessage":"Invalid response type from Ollama chat completion. Expected AsyncIterator but got {type(response_object)}.","messagePattern":"Invalid response type from Ollama chat completion\\. Expected AsyncIterator but got (.+?)\\.","errorType":"exception","errorClass":"ServiceInvalidResponseError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py","lineNumber":148,"sourceCode":"    @trace_streaming_text_completion(OllamaBase.MODEL_PROVIDER_NAME)\n    async def _inner_get_streaming_text_contents(\n        self,\n        prompt: str,\n        settings: \"PromptExecutionSettings\",\n    ) -> AsyncGenerator[list[StreamingTextContent], Any]:\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=True,\n            **settings.prepare_settings_dict(),\n        )\n\n        if not isinstance(response_object, AsyncIterator):\n            raise ServiceInvalidResponseError(\n                \"Invalid response type from Ollama chat completion. \"\n                f\"Expected AsyncIterator but got {type(response_object)}.\"\n            )\n\n        async for part in response_object:\n            yield [\n                StreamingTextContent(\n                    choice_index=0,\n                    inner_content=part,\n                    ai_model_id=self.ai_model_id,\n                    text=part.response if isinstance(part, GenerateResponse) else part.get(\"response\"),\n                )\n            ]\n\n    # endregion\n","sourceCodeStart":130,"sourceCodeEnd":164,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py#L130-L164","documentation":"Raised inside _inner_get_streaming_text_contents after a streaming call to self.client.generate(stream=True). When stream=True the ollama client should return an AsyncIterator of generate chunks; if it returns a single object instead, the connector cannot iterate and raises ServiceInvalidResponseError. Like error 1121, the message says 'chat completion' but this is the streaming text-completion path.","triggerScenarios":"Calling get_streaming_text_contents when the ollama client ignores stream=True and returns a single GenerateResponse — happens with certain ollama client versions, with mock/stub clients in tests, or when a custom client passed to the constructor does not support async iteration.","commonSituations":"Testing with a fake AsyncClient that returns a dict instead of an async generator; ollama library version mismatch where streaming semantics changed; a custom client object that does not implement __aiter__.","solutions":["Ensure you are using a compatible ollama Python package version that returns an AsyncIterator for stream=True","If passing a custom client, make sure it returns an async generator from generate(stream=True)","In tests, mock self.client.generate to return an async iterator, not a single object","Fall back to non-streaming get_text_contents if streaming is unreliable with your setup"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"from collections.abc import AsyncIterator\n\ndef is_streaming_response(obj) -> bool:\n    return isinstance(obj, AsyncIterator)","tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInvalidResponseError\n\ntry:\n    async for chunk in ollama.get_streaming_text_contents(prompt='Hello', settings=settings):\n        process(chunk)\nexcept ServiceInvalidResponseError as e:\n    logger.error('Ollama streaming returned unexpected type: %s', e)\n    # fall back to non-streaming\n    result = await ollama.get_text_contents(prompt='Hello', settings=settings)","preventionTips":["Ensure your ollama client version supports async streaming generators","When mocking the client in tests, return an async generator from generate(stream=True)","Provide a non-streaming fallback path for environments where streaming is unreliable"],"tags":["ollama","streaming","response-validation","runtime"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}