microsoft/semantic-kernel · error · AgentInitializationException

Failed to create OpenAI settings.

Error message

Failed to create OpenAI settings.

What it means

Raised by the deprecated setup_resources() static method when constructing OpenAISettings raises a pydantic ValidationError. OpenAISettings reads from constructor arguments and environment variables; a ValidationError means the assembled configuration violates a field constraint (bad type, failed regex, mutually exclusive values, etc.). The original ValidationError is chained as the cause.

Source

Thrown at python/semantic_kernel/agents/open_ai/openai_responses_agent.py:433

            org_id: The organization ID
            env_file_path: The environment file path
            env_file_encoding: The environment file encoding, defaults to utf-8
            default_headers: The default headers to add to the client
            kwargs: Additional keyword arguments

        Returns:
            An OpenAI client instance and the configured Response model name
        """
        try:
            openai_settings = OpenAISettings(
                responses_model_id=ai_model_id,
                api_key=api_key,
                org_id=org_id,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as ex:
            raise AgentInitializationException("Failed to create OpenAI settings.", ex) from ex

        if not openai_settings.api_key:
            raise AgentInitializationException("The OpenAI API key is required.")

        if not openai_settings.responses_model_id:
            raise AgentInitializationException("The OpenAI Responses model ID is required.")

        merged_headers = dict(copy(default_headers)) if default_headers else {}
        if default_headers:
            merged_headers.update(default_headers)
        if APP_INFO:
            merged_headers.update(APP_INFO)
            merged_headers = prepend_semantic_kernel_to_user_agent(merged_headers)

        client = AsyncOpenAI(
            api_key=openai_settings.api_key.get_secret_value() if openai_settings.api_key else None,
            organization=openai_settings.org_id,
            default_headers=merged_headers,

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained ValidationError (the `ex` argument) — it lists the exact field and reason that failed.
  2. Migrate off the deprecated setup_resources() to OpenAIResponsesAgent.create_client(), which has identical settings logic.
  3. Fix the offending environment variable or argument so OpenAISettings constructs cleanly.
  4. Run a standalone OpenAISettings() call in a REPL to surface the validation error in isolation.

Example fix

// before
client, model = OpenAIResponsesAgent.setup_resources(api_key=os.environ['OPENAI_API_KEY'])

// after
client, model = OpenAIResponsesAgent.create_client()  # reads env automatically
Defensive patterns

Strategy: try-catch

Validate before calling

from semantic_kernel.connectors.ai.open_ai.settings.open_ai_settings import OpenAISettings
try:
    OpenAISettings()  # validate env/args in isolation
except Exception as e:
    print('settings invalid:', e)

Try / catch

from semantic_kernel.exceptions.agent_exceptions import AgentInitializationException
try:
    client, model = OpenAIResponsesAgent.setup_resources()
except AgentInitializationException as e:
    print('init failed:', e.__cause__)  # the ValidationError
    raise

Prevention

When it happens

Trigger: Calling OpenAIResponsesAgent.setup_resources(...) with an argument whose value fails pydantic validation (e.g. org_id of the wrong type, an env file that can't be parsed), or with environment variables (OPENAI_API_KEY, etc.) set to values that violate OpenAISettings field validators.

Common situations: A .env file with a malformed value, a typo that turns a string into an unparseable type, a pydantic v2 upgrade that tightened validators, or passing a SecretStr where a plain str is expected. Note this method is deprecated in favor of create_client().

Related errors


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