{"record":{"id":"e731f8eeb20c9dc1","repo":"microsoft/semantic-kernel","slug":"failed-to-initialize-onnxcompletion-service","errorCode":null,"errorMessage":"Failed to initialize OnnxCompletion service","messagePattern":"Failed to initialize OnnxCompletion service","errorType":"exception","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/onnx/services/onnx_gen_ai_completion_base.py","lineNumber":53,"sourceCode":"\n        Raises:\n            ServiceInitializationError: When model cannot be loaded\n        \"\"\"\n        if not ready:\n            raise ImportError(\"onnxruntime-genai is not installed.\")\n        try:\n            json_gen_ai_config = os.path.join(ai_model_path + \"/genai_config.json\")\n            with open(json_gen_ai_config) as file:\n                config: dict = json.load(file)\n                enable_multi_modality = \"vision\" in config.get(\"model\", {})\n                model = OnnxRuntimeGenAi.Model(ai_model_path)\n                if enable_multi_modality:\n                    tokenizer = model.create_multimodal_processor()\n                else:\n                    tokenizer = OnnxRuntimeGenAi.Tokenizer(model)\n                tokenizer_stream = tokenizer.create_stream()\n        except Exception as ex:\n            raise ServiceInitializationError(\"Failed to initialize OnnxCompletion service\", ex) from ex\n\n        super().__init__(\n            model=model,\n            tokenizer=tokenizer,\n            tokenizer_stream=tokenizer_stream,\n            enable_multi_modality=enable_multi_modality,\n            **kwargs,\n        )\n\n    async def _generate_next_token_async(\n        self,\n        prompt: str,\n        settings: OnnxGenAIPromptExecutionSettings,\n        images: list[ImageContent] | None = None,\n        audios: list[AudioContent] | None = None,\n    ) -> AsyncGenerator[list[str], Any]:\n        try:\n            params = OnnxRuntimeGenAi.GeneratorParams(self.model)","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/onnx/services/onnx_gen_ai_completion_base.py#L35-L71","documentation":"Raised by the OnnxGenAICompletionBase constructor when any Exception occurs during model/tokenizer loading — reading genai_config.json, instantiating OnnxRuntimeGenAi.Model, or creating the tokenizer/stream. The broad except clause wraps ALL exceptions as ServiceInitializationError with the original exception chained via 'from ex'. The most common cause is an invalid or missing model path.","triggerScenarios":"Constructing the service with an ai_model_path that does not exist, lacks genai_config.json, has a corrupt config JSON, or where the model files are incompatible with the installed onnxruntime-genai version. Also raised on file permission errors.","commonSituations":"Pointing at a partially downloaded model folder; wrong path (missing trailing directory, relative vs absolute); model config format mismatch with onnxruntime-genai version; file permissions preventing read; genai_config.json missing because the model was downloaded via a different tool.","solutions":["Inspect the chained exception (__cause__) for the specific failure (FileNotFoundError, JSONDecodeError, etc.)","Verify ai_model_path points to a folder containing genai_config.json and model .onnx files","Ensure the onnxruntime-genai version is compatible with the model format","Check file read permissions on the model folder"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import os, json\n\nmodel_path = '/path/to/model_folder'\nconfig_path = os.path.join(model_path, 'genai_config.json')\n\nif not os.path.isdir(model_path):\n    raise RuntimeError(f'Model folder does not exist: {model_path}')\nif not os.path.isfile(config_path):\n    raise RuntimeError(f'genai_config.json not found in: {model_path}')\ntry:\n    with open(config_path) as f:\n        json.load(f)\nexcept json.JSONDecodeError:\n    raise RuntimeError(f'genai_config.json is not valid JSON')","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError\n\ntry:\n    chat = OnnxGenAIChatCompletion(ai_model_path=model_path)\nexcept ServiceInitializationError as e:\n    cause = e.__cause__\n    logger.error('ONNX init failed: %s. Root cause: %s', e, cause)\n    raise","preventionTips":["Verify the model folder exists and contains genai_config.json before constructing","Download models from a trusted source to avoid corrupt or partial folders","Pin a compatible onnxruntime-genai version for your model format","Log e.__cause__ to diagnose the underlying file/parse error"],"tags":["onnx","model-loading","initialization","file-path"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}