microsoft/semantic-kernel · error · ServiceInitializationError

Error creating OnnxGenAISettings: {e}

Error message

Error creating OnnxGenAISettings: {e}

What it means

Raised by the OnnxGenAIChatCompletion constructor when OnnxGenAISettings(...) raises a Pydantic ValidationError during settings construction. The original error is included via an f-string in the message. This indicates a structural settings problem (wrong type for env values, etc.) rather than a missing value.

Source

Thrown at python/semantic_kernel/connectors/ai/onnx/services/onnx_gen_ai_chat_completion.py:71

        """Initializes a new instance of the OnnxGenAITextCompletion class.

        Args:
            template : The chat template configuration.
            ai_model_path : Local path to the ONNX model Folder.
            ai_model_id : The ID of the AI model. Defaults to None.
            env_file_path : Use the environment settings file as a fallback
                to environment variables.
            env_file_encoding : The encoding of the environment settings file.
            kwargs : Additional arguments.
        """
        try:
            settings = OnnxGenAISettings(
                chat_model_folder=ai_model_path,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as e:
            raise ServiceInitializationError(f"Error creating OnnxGenAISettings: {e}") from e

        if settings.chat_model_folder is None:
            raise ServiceInitializationError(
                "AI model path is not provided. Please provide the 'ai_model_path' parameter in the constructor. "
                "OR set the 'ONNX_GEN_AI_CHAT_MODEL_FOLDER' environment variable."
            )

        if ai_model_id is None:
            ai_model_id = settings.chat_model_folder

        super().__init__(ai_model_id=ai_model_id, ai_model_path=settings.chat_model_folder, template=template, **kwargs)

        if self.enable_multi_modality and template is None:
            raise ServiceInitializationError(
                "When using a multi-modal model, a template must be specified."
                " Please provide a ONNXTemplate in the constructor."
            )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Read the full error message which embeds the Pydantic ValidationError details
  2. Fix the offending environment variable or .env entry
  3. Verify env_file_path is valid and the encoding matches env_file_encoding
Defensive patterns

Strategy: validation

Validate before calling

from pydantic import ValidationError
from semantic_kernel.connectors.ai.onnx.onnx_gen_ai_settings import OnnxGenAISettings

try:
    test_settings = OnnxGenAISettings(chat_model_folder='/path/to/model')
except ValidationError as e:
    print(f'Configuration is invalid: {e}')
    raise

chat = OnnxGenAIChatCompletion(ai_model_path='/path/to/model')

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError

try:
    chat = OnnxGenAIChatCompletion()
except ServiceInitializationError as e:
    logger.error('OnnxGenAI chat settings error: %s', e)
    raise

Prevention

When it happens

Trigger: Constructing OnnxGenAIChatCompletion with settings that fail Pydantic validation — e.g. ONNX_GEN_AI_CHAT_MODEL_FOLDER set to a non-string type in the environment, or a corrupt .env file pointed to by env_file_path.

Common situations: Environment variable with unexpected encoding; env_file_path pointing at an unreadable file; .env file with malformed key=value pairs that Pydantic rejects.

Related errors


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