microsoft/semantic-kernel · error · ServiceInvalidResponseError

No message content found in response.

Error message

No message content found in response.

What it means

Raised as ServiceInvalidResponseError in `_create_chat_message_content` (the raw Mapping/dict branch of non-streaming chat) when the response dict has no top-level `'message'` key (`response.get('message', None)` is falsy). The connector cannot locate the assistant message envelope, so it refuses to fabricate content.

Source

Thrown at python/semantic_kernel/connectors/ai/ollama/services/ollama_chat_completion.py:279

                TextContent(
                    text=response.message.content,
                    inner_content=response.message,
                )
            )
        self._parse_tool_calls(response.message.tool_calls, items)
        return ChatMessageContent(
            role=AuthorRole.ASSISTANT,
            items=items,
            inner_content=response,
            ai_model_id=self.ai_model_id,
            metadata=self._get_metadata_from_chat_response(response),
        )

    def _create_chat_message_content(self, response: Mapping[str, Any]) -> ChatMessageContent:
        """Create a chat message content from the response."""
        items: list[CMC_ITEM_TYPES] = []
        if not (message := response.get("message", None)):
            raise ServiceInvalidResponseError("No message content found in response.")

        if content := message.get("content", None):
            items.append(
                TextContent(
                    text=content,
                    inner_content=message,
                )
            )
        if tool_calls := message.get("tool_calls", None):
            for tool_call in tool_calls:
                items.append(
                    FunctionCallContent(
                        inner_content=tool_call,
                        ai_model_id=self.ai_model_id,
                        name=tool_call.get("function").get("name"),
                        arguments=tool_call.get("function").get("arguments"),
                    )
                )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Check the Ollama server logs / the raw dict inner_content for an 'error' field explaining the real problem.
  2. Confirm the model id exists (`ollama list`) and the server is healthy (`ollama ps`).
  3. Align the Ollama server version with the schema the SDK/connector expects.
  4. If the dict legitimately has no message (error), surface it to the user rather than retrying blindly.

Example fix

# before - server returns {'error': 'model not found'}
await svc.get_chat_message_contents(chat_history, settings)

# after - detect error envelope, fetch the model first
import subprocess
subprocess.run(['ollama','pull','llama3'], check=True)
await svc.get_chat_message_contents(chat_history, settings)
Defensive patterns

Strategy: validation

Validate before calling

# Before relying on the service, confirm it returns a message envelope
resp = await svc.client.chat(model=svc.ai_model_id, messages=[{'role':'user','content':'hi'}], stream=False)
if isinstance(resp, dict):
    assert resp.get('message'), f'response missing message: {resp}'

Type guard

from collections.abc import Mapping

def response_has_message(resp) -> bool:
    return isinstance(resp, Mapping) and bool(resp.get('message'))

Try / catch

from semantic_kernel.exceptions import ServiceInvalidResponseError
try:
    resp = await svc.get_chat_message_contents(chat_history, settings)
except ServiceInvalidResponseError as e:
    if 'No message content found in response' in str(e):
        # likely an error envelope; fetch raw to inspect
        raw = await svc.client.chat(model=svc.ai_model_id, messages=[{'role':'user','content':'hi'}], stream=False)
        raise RuntimeError(f'Ollama returned unexpected body: {raw}') from e
    raise

Prevention

When it happens

Trigger: The Ollama server returned a dict that lacks the 'message' field - e.g. an error response body, a partial/keep-alive response, or an Ollama API version whose non-streaming schema moved the content under a different key.

Common situations: Ollama server returned an error JSON (e.g. {'error':'model not found'}) that the SDK mapped to a dict without 'message'; an API version mismatch; an unexpected response shape from a load endpoint.

Related errors


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