microsoft/semantic-kernel · error · ServiceInitializationError

Failed to create Ollama settings.

Error message

Failed to create Ollama settings.

What it means

Raised as ServiceInitializationError when constructing the pydantic `OllamaSettings` for the Ollama chat completion service raises a `ValidationError`. Config/structure failure (local Ollama settings, no network yet). The original validation error is chained via `from ex`.

Source

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

        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(
                chat_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.chat_model_id:
            raise ServiceInitializationError("Ollama chat model ID is required.")

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

    # region Overriding base class methods

    # Override from AIServiceClientBase
    @override
    def get_prompt_execution_settings_class(self) -> type["PromptExecutionSettings"]:
        """Get the request settings class."""
        return OllamaChatPromptExecutionSettings

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained `ex` for the offending field.
  2. Set OLLAMA_HOST to a full URL (http://host:port) or pass host= explicitly.
  3. Verify env_file_path exists and env_file_encoding is a valid codec (default utf-8).
  4. Pass chat_model_id directly to bypass env resolution.

Example fix

# before
svc = OllamaChatCompletion()  # -> ServiceInitializationError

# after
svc = OllamaChatCompletion(
    host='http://localhost:11434',
    ai_model_id='llama3',
)
Defensive patterns

Strategy: validation

Validate before calling

from semantic_kernel.connectors.ai.ollama import OllamaSettings
from pydantic import ValidationError
try:
    OllamaSettings(host=os.environ.get('OLLAMA_HOST', 'http://localhost:11434'),
                   chat_model_id='llama3')
except ValidationError as e:
    raise SystemExit(f'Fix Ollama settings first: {e}')

Type guard

from semantic_kernel.exceptions import ServiceInitializationError

def is_ollama_settings_error(e: BaseException) -> bool:
    return isinstance(e, ServiceInitializationError) and 'Failed to create Ollama settings' in str(e)

Try / catch

from semantic_kernel.exceptions import ServiceInitializationError
try:
    svc = OllamaChatCompletion()
except ServiceInitializationError as e:
    raise SystemExit(f'Ollama chat misconfigured: {e.__cause__ or e}') from e

Prevention

When it happens

Trigger: Constructing `OllamaChatCompletion(...)` where merged config (chat_model_id/host/env_file_path/env_file_encoding + OLLAMA_* env) fails pydantic validation: invalid OLLAMA_HOST URL, unreadable env file, bad env_file_encoding value, or non-string chat_model_id.

Common situations: OLLAMA_HOST set without scheme/port (e.g. 'localhost:11434' instead of 'http://localhost:11434'), .env path wrong, env_file_encoding set to an unsupported codec, typo'd OLLAMA_CHAT_MODEL_ID.

Related errors


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