microsoft/semantic-kernel · error · ServiceInitializationError

Ollama text model ID is required.

Error message

Ollama text model ID is required.

What it means

Thrown by the OllamaTextCompletion constructor when the resolved text_model_id is empty or None after merging the ai_model_id argument with the OLLAMA_TEXT_MODEL_ID environment variable via OllamaSettings. The service cannot call the Ollama /api/generate endpoint without a model identifier, so it aborts initialization. It is raised as a ServiceInitializationError before any network call is made.

Source

Thrown at python/semantic_kernel/connectors/ai/ollama/services/ollama_text_completion.py:73

            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 AIServiceClientBase
    @override
    def service_url(self) -> str | None:
        if hasattr(self.client, "_client") and isinstance(self.client._client, httpx.AsyncClient):

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pass the model name directly: OllamaTextCompletion(ai_model_id='llama3')
  2. Set the env var: export OLLAMA_TEXT_MODEL_ID=llama3
  3. Add OLLAMA_TEXT_MODEL_ID=llama3 to your .env and pass env_file_path='.env' to the constructor

Example fix

// before
ollama = OllamaTextCompletion()
// after
ollama = OllamaTextCompletion(ai_model_id='llama3')
Defensive patterns

Strategy: validation

Validate before calling

import os

model_id = 'llama3'  # your intended model
if not model_id and not os.environ.get('OLLAMA_TEXT_MODEL_ID'):
    raise RuntimeError('OLLAMA_TEXT_MODEL_ID is not set and no ai_model_id provided')

from semantic_kernel.connectors.ai.ollama import OllamaTextCompletion
ollama = OllamaTextCompletion(ai_model_id=model_id)

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError

try:
    ollama = OllamaTextCompletion()
except ServiceInitializationError as e:
    if 'text model ID is required' in str(e):
        ollama = OllamaTextCompletion(ai_model_id=os.environ['FALLBACK_MODEL'])
    else:
        raise

Prevention

When it happens

Trigger: Constructing OllamaTextCompletion() with ai_model_id omitted (defaults to None) while OLLAMA_TEXT_MODEL_ID is not set in the environment or .env file. Also triggered by passing ai_model_id=None explicitly, or by pointing env_file_path at a file that lacks the key.

Common situations: New project with no .env configured; CI/deploy environment where OLLAMA_* variables are not propagated; env var name typo (e.g. OLLAMA_TEXT_MODEL instead of OLLAMA_TEXT_MODEL_ID); switching from OllamaChatCompletion (which uses OLLAMA_CHAT_MODEL_ID) and forgetting to set the text variant.

Related errors


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