microsoft/semantic-kernel · error · ServiceInvalidResponseError
Invalid response type from Ollama chat completion. Expected
Error message
Invalid response type from Ollama chat completion. Expected AsyncIterator but got {type(response_object)}. What it means
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.
Source
Thrown at python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py:148
@trace_streaming_text_completion(OllamaBase.MODEL_PROVIDER_NAME)
async def _inner_get_streaming_text_contents(
self,
prompt: str,
settings: "PromptExecutionSettings",
) -> AsyncGenerator[list[StreamingTextContent], Any]:
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=True,
**settings.prepare_settings_dict(),
)
if not isinstance(response_object, AsyncIterator):
raise ServiceInvalidResponseError(
"Invalid response type from Ollama chat completion. "
f"Expected AsyncIterator but got {type(response_object)}."
)
async for part in response_object:
yield [
StreamingTextContent(
choice_index=0,
inner_content=part,
ai_model_id=self.ai_model_id,
text=part.response if isinstance(part, GenerateResponse) else part.get("response"),
)
]
# endregion
View on GitHub (pinned to c028a0c7dc)
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
Defensive patterns
Strategy: try-catch
Type guard
from collections.abc import AsyncIterator
def is_streaming_response(obj) -> bool:
return isinstance(obj, AsyncIterator) Try / catch
from semantic_kernel.exceptions.service_exceptions import ServiceInvalidResponseError
try:
async for chunk in ollama.get_streaming_text_contents(prompt='Hello', settings=settings):
process(chunk)
except ServiceInvalidResponseError as e:
logger.error('Ollama streaming returned unexpected type: %s', e)
# fall back to non-streaming
result = await ollama.get_text_contents(prompt='Hello', settings=settings) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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__.
Related errors
- Invalid response type from Ollama chat completion. Expected
- Agent Failure - Run not created for thread: ${threadId}
- Invalid response type from Ollama streaming chat completion.
- Invalid response type from Ollama streaming chat completion.
- No message content found in response part.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/0a87ed7f88ccdd38.
Report an issue: GitHub.