microsoft/semantic-kernel · error · ServiceInitializationError
Failed to create OpenAI settings.
Error message
Failed to create OpenAI settings.
What it means
Raised in OpenAITextToAudio.__init__ when the OpenAISettings pydantic model fails validation during construction. The original pydantic ValidationError is attached as __cause__ and contains the field-level details. Because every OpenAISettings field is Optional, this fires only on a genuine validation failure, not on a missing model id.
Source
Thrown at python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_to_audio.py:58
org_id: The optional org ID to use. If provided will override,
the env vars or .env file value.
default_headers: The default headers mapping of string keys to
string values for HTTP requests. (Optional)
async_client: An existing client to use. (Optional)
env_file_path: Use the environment settings file as
a fallback to environment variables. (Optional)
env_file_encoding: The encoding of the environment settings file. (Optional)
"""
try:
openai_settings = OpenAISettings(
api_key=api_key,
org_id=org_id,
text_to_audio_model_id=ai_model_id,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
except ValidationError as ex:
raise ServiceInitializationError("Failed to create OpenAI settings.", ex) from ex
if not openai_settings.text_to_audio_model_id:
raise ServiceInitializationError("The OpenAI text to audio model ID is required.")
super().__init__(
ai_model_id=openai_settings.text_to_audio_model_id,
api_key=openai_settings.api_key.get_secret_value() if openai_settings.api_key else None,
ai_model_type=OpenAIModelTypes.TEXT_TO_AUDIO,
org_id=openai_settings.org_id,
service_id=service_id,
default_headers=default_headers,
client=async_client,
)
@classmethod
def from_dict(cls: type[T_], settings: dict[str, Any]) -> T_:
"""Initialize an Open AI service from a dictionary of settings.
Args:
settings: A dictionary of settings for the service.View on GitHub (pinned to c028a0c7dc)
Solutions
- Read the chained cause: except ServiceInitializationError as e: print(e.__cause__.errors())
- Check the .env file for OPENAI_* entries with broken quoting or whitespace
- Pass api_key as str and verify env_file_encoding matches the file
- Reproduce by instantiating OpenAISettings(api_key=..., text_to_audio_model_id=...) in isolation
Example fix
# before service = OpenAITextToAudio(env_file_path="broken.env") # raises ServiceInitializationError: Failed to create OpenAI settings. # after service = OpenAITextToAudio(ai_model_id="tts-1", api_key="sk-...")
Defensive patterns
Strategy: try-catch
Validate before calling
from semantic_kernel.connectors.ai.open_ai.settings.open_ai_settings import OpenAISettings
try:
OpenAISettings(api_key=api_key, text_to_audio_model_id=ai_model_id)
except Exception as e:
raise ValueError(f"OpenAISettings invalid: {e}") from e Type guard
from pydantic import ValidationError
from semantic_kernel.connectors.ai.open_ai.settings.open_ai_settings import OpenAISettings
def settings_are_valid(**kwargs) -> bool:
try:
OpenAISettings(**kwargs)
return True
except ValidationError:
return False Try / catch
from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError
try:
service = OpenAITextToAudio(api_key=api_key)
except ServiceInitializationError as e:
fields = e.__cause__.errors() if e.__cause__ is not None else []
raise RuntimeError(f"OpenAI settings invalid: {fields}") from e Prevention
- Inspect e.__cause__.errors() to find the offending field
- Validate the .env file syntax and encoding
- Construct OpenAISettings alone first to surface the raw error
When it happens
Trigger: Constructing OpenAITextToAudio with an argument pydantic rejects (bad type for api_key/org_id), or an env_file_path pointing to a .env whose values fail validation, or env var binding that KernelBaseSettings cannot coerce.
Common situations: Malformed .env file; secret injected in a non-string type; env_file_encoding mismatch; pydantic v2 stricter coercion rejecting a value that previously auto-coerced.
Related errors
- Failed to create OpenAI settings.
- Failed to create OpenAI settings.
- Failed to create Copilot Studio Agent settings: {exc}
- Failed to create Azure OpenAI settings: {exc}
- Failed to validate Google AI settings: {e}
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/f16057764c6fd739.
Report an issue: GitHub.