microsoft/semantic-kernel · error · ServiceInitializationError

Failed to create Ollama settings.

Error message

Failed to create Ollama settings.

What it means

Raised by the OllamaTextEmbedding constructor when OllamaSettings(...) raises a Pydantic ValidationError during settings construction. This is a wrapper that preserves the original validation error via 'from ex'. It indicates a structural problem with the settings (bad host URL format, wrong types) rather than a missing value.

Source

Thrown at python/semantic_kernel/connectors/ai/ollama/services/ollama_text_embedding.py:66

        Args:
            service_id (Optional[str]): Service ID tied to the execution settings. (Optional)
            ai_model_id (Optional[str]): The model name. (Optional)
            host (Optional[str]): URL of the Ollama server, defaults to None and
                will use the default Ollama service address: http://127.0.0.1:11434. (Optional)
            client (Optional[AsyncClient]): A custom Ollama client to use for the service. (Optional)
            env_file_path (str | None): Use the environment settings file as a fallback to using env vars.
            env_file_encoding (str | None): The encoding of the environment settings file, defaults to 'utf-8'.
        """
        try:
            ollama_settings = OllamaSettings(
                embedding_model_id=ai_model_id,
                host=host,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as ex:
            raise ServiceInitializationError("Failed to create Ollama settings.", ex) from ex

        if not ollama_settings.embedding_model_id:
            raise ServiceInitializationError("Ollama embedding model ID is not set.")

        super().__init__(
            service_id=service_id or ollama_settings.embedding_model_id,
            ai_model_id=ollama_settings.embedding_model_id,
            client=client or AsyncClient(host=ollama_settings.host),
        )

    @override
    async def generate_embeddings(
        self,
        texts: list[str],
        settings: "PromptExecutionSettings | None" = None,
        **kwargs: Any,
    ) -> ndarray:
        raw_embeddings = await self.generate_raw_embeddings(texts, settings, **kwargs)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained ValidationError in the exception traceback for the specific field that failed
  2. Fix the malformed setting — typically provide a full URL for host (http://127.0.0.1:11434)
  3. Verify env_file_path exists and is readable with the correct encoding

Example fix

// before
ollama = OllamaTextEmbedding(host='localhost:11434')
// after
ollama = OllamaTextEmbedding(host='http://127.0.0.1:11434')
Defensive patterns

Strategy: validation

Validate before calling

from pydantic import TypeAdapter
from semantic_kernel.connectors.ai.ollama.ollama_settings import OllamaSettings

# Pre-validate host is a proper URL before constructing
host = 'http://127.0.0.1:11434'
assert host.startswith('http'), 'Ollama host must include scheme (http:// or https://)'

ollama = OllamaTextEmbedding(host=host, ai_model_id='nomic-embed-text')

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError

try:
    ollama = OllamaTextEmbedding(host=host)
except ServiceInitializationError as e:
    logger.error('Settings validation failed: %s (cause: %s)', e, e.__cause__)
    raise

Prevention

When it happens

Trigger: Constructing OllamaTextEmbedding with a malformed host (e.g. host='not-a-url'), or an env_file_path pointing at a corrupt/unreadable .env file, or an OLLAMA_HOST value that fails HttpsUrl validation.

Common situations: Setting OLLAMA_HOST to a bare hostname without scheme (e.g. 'localhost:11434' instead of 'http://localhost:11434'); pointing env_file_path to a non-existent file; encoding mismatch in the .env file.

Related errors


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