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 text completion service raises a `ValidationError`. Config/structure failure (no network yet); the original validation error is chained. This is the text-completion analogue of 1111 (chat).
Source
Thrown at python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py:70
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(
text_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.text_model_id:
raise ServiceInitializationError("Ollama text model ID is required.")
super().__init__(
service_id=service_id or ollama_settings.text_model_id,
ai_model_id=ollama_settings.text_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"]:
return OllamaTextPromptExecutionSettings
# Override from AIServiceClientBaseView on GitHub (pinned to c028a0c7dc)
Solutions
- Inspect the chained `ex` ValidationError for the failing field.
- Set OLLAMA_HOST to a full URL (http://host:port) or pass host= explicitly.
- Verify env_file_path exists and env_file_encoding is valid (default utf-8).
- Pass ai_model_id (text model) directly to bypass env resolution.
Example fix
# before
svc = OllamaTextCompletion() # -> ServiceInitializationError
# after
svc = OllamaTextCompletion(
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'),
text_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_text_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 = OllamaTextCompletion()
except ServiceInitializationError as e:
raise SystemExit(f'Ollama text completion misconfigured: {e.__cause__ or e}') from e Prevention
- Set OLLAMA_HOST as a full http:// URL (with port).
- Validate OllamaSettings with pydantic before constructing the service.
- Ensure env_file_path exists and env_file_encoding is a valid codec.
When it happens
Trigger: Constructing `OllamaTextCompletion(...)` where merged config (text_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 codec, or non-string text_model_id.
Common situations: OLLAMA_HOST missing scheme, env_file_path wrong, env_file_encoding set to an unsupported codec, typo'd OLLAMA_TEXT_MODEL_ID.
Related errors
- Failed to create Ollama settings.
- Failed to validate Google AI settings: {e}
- Failed to validate Mistral AI settings: {e}
- Failed to create NVIDIA settings.
- Failed to create NVIDIA settings.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/165d62c62131942c.
Report an issue: GitHub.