microsoft/semantic-kernel · error · ServiceInitializationError

Failed to validate Azure AI Inference settings: {e}

Error message

Failed to validate Azure AI Inference settings: {e}

What it means

Raised during Azure AI Inference service construction when AzureAIInferenceSettings (api_key, endpoint, api_version from args/env) fails Pydantic validation. This is a ServiceInitializationError surfaced before any client is built; the original ValidationError is chained so the exact missing/invalid field is visible.

Source

Thrown at python/semantic_kernel/connectors/ai/azure_ai_inference/services/azure_ai_inference_base.py:92

            instruction_role (str | None): The role to use for 'instruction' messages. (Optional)
            credential: The credential to use for authentication. (Optional)
            **kwargs: Additional keyword arguments.

        Raises:
            ServiceInitializationError: If an error occurs during initialization.
        """
        managed_client = client is None
        if not client:
            try:
                azure_ai_inference_settings = AzureAIInferenceSettings(
                    api_key=api_key,
                    endpoint=endpoint,
                    api_version=api_version,
                    env_file_path=env_file_path,
                    env_file_encoding=env_file_encoding,
                )
            except ValidationError as e:
                raise ServiceInitializationError(f"Failed to validate Azure AI Inference settings: {e}") from e

            endpoint = str(azure_ai_inference_settings.endpoint)
            if azure_ai_inference_settings.api_key is not None:
                client = AzureAIInferenceClientType.get_client_class(client_type)(
                    endpoint=endpoint,
                    credential=AzureKeyCredential(azure_ai_inference_settings.api_key.get_secret_value()),
                    user_agent=SEMANTIC_KERNEL_USER_AGENT,
                    api_version=azure_ai_inference_settings.api_version,
                )
            else:
                if credential is None:
                    raise ServiceInitializationError("The 'credential' parameter is required for authentication.")

                client = AzureAIInferenceClientType.get_client_class(client_type)(
                    endpoint=endpoint,
                    credential=credential,
                    user_agent=SEMANTIC_KERNEL_USER_AGENT,
                    api_version=azure_ai_inference_settings.api_version,

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained ValidationError (e.__cause__) to see which field failed.
  2. Set AZURE_AI_INFERENCE_ENDPOINT to a valid https URL (and AZURE_AI_INFERENCE_API_KEY / API_VERSION as needed), or pass endpoint= explicitly.
  3. Verify env_file_path points to an existing .env with the required keys.

Example fix

# before
service = AzureAIInferenceChatCompletion(model_id="gpt-4o")  # no endpoint env → error

# after
export AZURE_AI_INFERENCE_ENDPOINT=https://my-endpoint.services.ai.azure.com/models
export AZURE_AI_INFERENCE_API_KEY=...
service = AzureAIInferenceChatCompletion(model_id="gpt-4o")
Defensive patterns

Strategy: validation

Validate before calling

import os

endpoint = os.getenv("AZURE_AI_INFERENCE_ENDPOINT")
assert endpoint and endpoint.startswith("https://"), \
    "Set AZURE_AI_INFERENCE_ENDPOINT to a valid https URL before constructing the service."

Type guard

def has_valid_endpoint(endpoint: str | None) -> bool:
    return bool(endpoint) and str(endpoint).startswith("https://")

Try / catch

from semantic_kernel.exceptions import ServiceInitializationError
try:
    service = AzureAIInferenceChatCompletion(model_id=model_id)
except ServiceInitializationError as e:
    print("Settings error:", e.__cause__)

Prevention

When it happens

Trigger: No endpoint configured (AZURE_AI_INFERENCE_ENDPOINT missing and no endpoint arg), malformed endpoint URL, invalid api_version format, or env file not found when relying on env_file_path. The settings model requires a valid endpoint.

Common situations: Missing or misnamed environment variables (AZURE_AI_INFERENCE_ENDPOINT / AZURE_AI_INFERENCE_API_KEY / AZURE_AI_INFERENCE_API_VERSION); wrong .env path; endpoint set to a non-URL string; deploying without seeding env vars in CI.

Related errors


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