microsoft/semantic-kernel · error · ServiceInitializationError
Failed to create OpenAI settings.
Error message
Failed to create OpenAI settings.
What it means
Raised by OpenAIAudioToText.__init__ when OpenAISettings construction throws a pydantic ValidationError. The non-Azure OpenAISettings model has fewer fields than its Azure counterpart: api_key (SecretStr|None), org_id (str|None), audio_to_text_model_id (str|None). If a field value fails type coercion (e.g. api_key passed as bytes or org_id as a non-string), pydantic raises ValidationError which is wrapped here.
Source
Thrown at python/semantic_kernel/connectors/ai/open_ai/services/open_ai_audio_to_text.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,
audio_to_text_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.audio_to_text_model_id:
raise ServiceInitializationError("The OpenAI audio to text model ID is required.")
super().__init__(
ai_model_id=openai_settings.audio_to_text_model_id,
api_key=openai_settings.api_key.get_secret_value() if openai_settings.api_key else None,
ai_model_type=OpenAIModelTypes.AUDIO_TO_TEXT,
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
- Inspect the chained ValidationError for the specific field and constraint that failed.
- Pass api_key as a plain string (str), not a SecretStr or bytes.
- Verify your .env file syntax and encoding (default is utf-8).
- Ensure OPENAI_API_KEY is set to a non-empty string in the environment.
Example fix
# before (passing SecretStr instead of str)
from pydantic import SecretStr
service = OpenAIAudioToText(
ai_model_id='whisper-1',
api_key=SecretStr('sk-...'), # wrong type
)
# after (plain string)
service = OpenAIAudioToText(
ai_model_id='whisper-1',
api_key='sk-...',
) Defensive patterns
Strategy: try-catch
Validate before calling
import os
api_key = os.environ.get('OPENAI_API_KEY')
if not api_key or not isinstance(api_key, str):
raise ValueError(
'OPENAI_API_KEY must be set to a non-empty string in the environment or .env file.'
) Try / catch
from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError
try:
service = OpenAIAudioToText(
ai_model_id='whisper-1',
api_key=os.environ.get('OPENAI_API_KEY'),
)
except ServiceInitializationError as e:
cause = e.__cause__
if cause:
print(f'Settings validation failed: {cause}')
raise Prevention
- Pass api_key as a plain string, not a SecretStr or bytes.
- Verify your .env file syntax and encoding (default utf-8).
- Read the chained ValidationError (__cause__) for the specific field that failed.
- Ensure OPENAI_API_KEY is a non-empty string in the environment.
When it happens
Trigger: Constructing OpenAIAudioToText with a malformed .env file, an api_key of the wrong type (e.g. bytes instead of str), or an environment variable with an incompatible value type. Since most fields are str|None, type errors are rare but can occur with corrupt env files.
Common situations: Corrupted .env file with invalid syntax; OPENAI_API_KEY env var set to an empty string in CI; env_file_encoding mismatch causing decode errors; passing api_key as a SecretStr object instead of a plain string.
Related errors
- Failed to create OpenAI settings.
- The input audio content is not readable.
- The audio transcription format '{responseFormat}' is not sup
- The provided reasoning effort '{textEffortLevel}' is not sup
- When used with number_of_responses, best_of controls the num
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/e9e7b0a7d25c60a0.
Report an issue: GitHub.