{"record":{"id":"5a5718cb82ff9016","repo":"microsoft/semantic-kernel","slug":"failed-to-create-openai-settings-5a5718","errorCode":null,"errorMessage":"Failed to create OpenAI settings.","messagePattern":"Failed to create OpenAI settings\\.","errorType":"exception","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"critical","filePath":"python/semantic_kernel/connectors/ai/open_ai/services/open_ai_realtime.py","lineNumber":98,"sourceCode":"            env_file_encoding (str | None): The encoding of the environment settings file. (Optional)\n            kwargs: Additional arguments.\n                This can include:\n                kernel (Kernel): the kernel to use for function calls\n                plugins (list[object] or dict[str, object]): the plugins to use for function calls\n                settings (OpenAIRealtimeExecutionSettings): the settings to use for the session\n                chat_history (ChatHistory): the chat history to use for the session\n                Otherwise they can also be passed to the context manager.\n        \"\"\"\n        try:\n            openai_settings = OpenAISettings(\n                api_key=api_key,\n                org_id=org_id,\n                realtime_model_id=ai_model_id,\n                env_file_path=env_file_path,\n                env_file_encoding=env_file_encoding,\n            )\n        except ValidationError as ex:\n            raise ServiceInitializationError(\"Failed to create OpenAI settings.\", ex) from ex\n        if not openai_settings.realtime_model_id:\n            raise ServiceInitializationError(\"The OpenAI realtime model ID is required.\")\n        if audio_track:\n            kwargs[\"audio_track\"] = audio_track\n        super().__init__(\n            audio_output_callback=audio_output_callback,\n            ai_model_id=openai_settings.realtime_model_id,\n            service_id=service_id,\n            api_key=openai_settings.api_key.get_secret_value() if openai_settings.api_key else None,\n            org_id=openai_settings.org_id,\n            ai_model_type=OpenAIModelTypes.REALTIME,\n            default_headers=default_headers,\n            client=client,\n            **kwargs,\n        )\n\n\n# region Websocket","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/open_ai/services/open_ai_realtime.py#L80-L116","documentation":"Raised as ServiceInitializationError during OpenAIRealtimeWebRTC construction when the OpenAISettings pydantic model fails validation. The constructor builds settings from api_key, org_id, ai_model_id, env_file_path, and env_file_encoding, then catches pydantic.ValidationError and re-wraps it. The original ValidationError is available in ex.__cause__.","triggerScenarios":"Constructing OpenAIRealtimeWebRTC when the OpenAISettings pydantic validator rejects the input — e.g., api_key fails a field validator, or a combination of fields violates a model-level constraint in OpenAISettings.","commonSituations":"Passing an empty or malformed api_key string that fails pydantic field validation; providing conflicting settings values (e.g., both env_file_path pointing to a missing file with no fallback); a pydantic model_validator on OpenAISettings rejecting the combination of inputs.","solutions":["Inspect ex.__cause__ (the pydantic.ValidationError) to see which field(s) failed validation and why","Fix the specific field validation error reported by pydantic (e.g., provide a non-empty api_key)","Ensure the .env file at env_file_path exists and contains valid entries if relying on it","Construct OpenAISettings directly in a try/except to get clearer error messages before passing to the service"],"exampleFix":"# before\nservice = OpenAIRealtimeWebRTC(audio_track=track)  # may fail if settings invalid\n# after — construct settings explicitly to catch validation early\nfrom semantic_kernel.connectors.ai.open_ai import OpenAISettings\ntry:\n    settings = OpenAISettings(api_key=os.environ['OPENAI_API_KEY'])\nexcept ValidationError as e:\n    print(e.errors())  # inspect specific field errors\n    raise\nservice = OpenAIRealtimeWebRTC(audio_track=track, api_key=settings.api_key.get_secret_value())","handlingStrategy":"validation","validationCode":"from pydantic import ValidationError\nfrom semantic_kernel.connectors.ai.open_ai import OpenAISettings\n\ntry:\n    _ = OpenAISettings(api_key=api_key, org_id=org_id, realtime_model_id=ai_model_id)\nexcept ValidationError as e:\n    for err in e.errors():\n        print(f'Field {err[\"loc\"]}: {err[\"msg\"]}')\n    raise","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError\n\ntry:\n    service = OpenAIRealtimeWebRTC(audio_track=track)\nexcept ServiceInitializationError as e:\n    if 'Failed to create OpenAI settings' in str(e):\n        logger.error('Settings validation failed: %s', e.__cause__)\n        raise","preventionTips":["Construct and validate OpenAISettings separately before passing to the service constructor","Log pydantic ValidationError.errors() to get actionable field-level diagnostics"],"tags":["openai","realtime","webrtc","configuration","pydantic-validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}