microsoft/semantic-kernel · critical · Exception

OpenAI/Azure OpenAI configuration was not found.

Error message

OpenAI/Azure OpenAI configuration was not found.

What it means

Thrown by GetRealtimeConversationClient when neither the OpenAI configuration (ApiKey + optional endpoint) nor the Azure OpenAI configuration (Endpoint + ApiKey, flagged IsValid) is present and valid. It is a base System.Exception (not a more specific type) signaling a startup-blocking configuration failure.

Source

Thrown at dotnet/samples/Demos/OpenAIRealtime/Program.cs:424

        var openAIOptions = config.GetSection(OpenAIOptions.SectionName).Get<OpenAIOptions>()!;
        var azureOpenAIOptions = config.GetSection(AzureOpenAIOptions.SectionName).Get<AzureOpenAIOptions>()!;

        if (openAIOptions is not null && openAIOptions.IsValid)
        {
            return new RealtimeClient(new ApiKeyCredential(openAIOptions.ApiKey));
        }
        else if (azureOpenAIOptions is not null && azureOpenAIOptions.IsValid)
        {
            var client = new AzureOpenAIClient(
                endpoint: new Uri(azureOpenAIOptions.Endpoint),
                credential: new ApiKeyCredential(azureOpenAIOptions.ApiKey));

            return client.GetRealtimeClient();
        }
        else
        {
            throw new Exception("OpenAI/Azure OpenAI configuration was not found.");
        }
    }

    #endregion
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set OpenAI:ApiKey (and optionally OpenAI:Endpoint) via 'dotnet user-secrets set "OpenAI:ApiKey" "<key>"' or as environment variables.
  2. Alternatively, set AzureOpenAI:Endpoint and AzureOpenAI:ApiKey via user secrets or environment variables so IsValid returns true.
  3. Verify your user secrets project ID matches (check the <UserSecretsId> in the .csproj) and that secrets.json actually contains the key.
  4. Use a more specific exception type (e.g., ConfigurationNotFoundException or InvalidOperationException) for clearer error handling downstream.

Example fix

// before
else
{
    throw new Exception("OpenAI/Azure OpenAI configuration was not found.");
}

// after — explicit, actionable message naming the missing keys
else
{
    throw new InvalidOperationException(
        "No OpenAI or Azure OpenAI configuration found. Set either 'OpenAI:ApiKey' or 'AzureOpenAI:Endpoint' + 'AzureOpenAI:ApiKey' in user secrets or environment variables.");
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate config presence before building the client
var hasOpenAI = !string.IsNullOrEmpty(config["OpenAI:ApiKey"]);
var hasAzure = !string.IsNullOrEmpty(config["AzureOpenAI:Endpoint"]) && !string.IsNullOrEmpty(config["AzureOpenAI:ApiKey"]);
if (!hasOpenAI && !hasAzure)
    throw new InvalidOperationException("Set OpenAI:ApiKey or AzureOpenAI:Endpoint+ApiKey in user secrets or env vars.");

Type guard

bool HasValidOpenAIConfig(IConfiguration c) => !string.IsNullOrWhiteSpace(c["OpenAI:ApiKey"]);

Prevention

When it happens

Trigger: The method loads configuration from user secrets and environment variables; if neither the OpenAI section nor the AzureOpenAI section resolves to a valid set of credentials, execution falls through to the else branch and throws.

Common situations: User secrets not initialized (secrets.json missing or empty); environment variables not set in a container/CI environment; the AzureOpenAIConfig.IsValid check fails because Endpoint or ApiKey is null; typo in the configuration section key name; running on a machine that never had 'dotnet user-secrets set' run for this project.

Related errors


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