microsoft/semantic-kernel · critical · Exception

OpenAI/Azure OpenAI configuration was not found.

Error message

OpenAI/Azure OpenAI configuration was not found.

What it means

The Function Invocation Approval sample tries OpenAI options first, then Azure OpenAI options, and throws a plain Exception if neither set is valid. This is the 'no chat-completion backend configured at all' terminal branch — neither OpenAIOptions nor AzureOpenAIOptions.IsValid passed.

Source

Thrown at dotnet/samples/Demos/FunctionInvocationApproval/Program.cs:192

            .Build();

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

        if (openAIOptions is not null && openAIOptions.IsValid)
        {
            builder.AddOpenAIChatCompletion(openAIOptions.ChatModelId, openAIOptions.ApiKey);
        }
        else if (azureOpenAIOptions is not null && azureOpenAIOptions.IsValid)
        {
            builder.AddAzureOpenAIChatCompletion(
                azureOpenAIOptions.ChatDeploymentName,
                azureOpenAIOptions.Endpoint,
                azureOpenAIOptions.ApiKey);
        }
        else
        {
            throw new Exception("OpenAI/Azure OpenAI configuration was not found.");
        }
    }

    #endregion
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Configure at least one of OpenAI or AzureOpenAI options fully so its IsValid is true.
  2. Check the IsValid implementation to see which fields each option requires and supply them.
  3. Set the relevant environment variables if not using appsettings.
  4. Pick exactly one provider to avoid ambiguous precedence.

Example fix

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

// after (actionable message + typed exception)
else
{
    throw new InvalidOperationException(
        "No chat completion backend configured. Set either OpenAI (ApiKey+ChatModelId) " +
        "or AzureOpenAI (ApiKey+Endpoint+ChatDeploymentName). " +
        $"openAIValid={openAIOptions?.IsValid ?? false}, azureValid={azureOpenAIOptions?.IsValid ?? false}.");
}
Defensive patterns

Strategy: validation

Validate before calling

bool openAiValid = openAIOptions is not null && openAIOptions.IsValid;
bool azureValid = azureOpenAIOptions is not null && azureOpenAIOptions.IsValid;
if (!openAiValid && !azureValid)
    throw new InvalidOperationException(
        $"No chat backend configured. openAIValid={openAiValid}, azureValid={azureValid}.");

Type guard

static bool HasAnyBackend(OpenAiOptions? o, AzureOpenAiOptions? a) =>
    (o?.IsValid ?? false) || (a?.IsValid ?? false);

Prevention

When it happens

Trigger: openAIOptions is null/invalid AND azureOpenAIOptions is null or azureOpenAIOptions.IsValid is false.

Common situations: Neither OpenAI nor AzureOpenAI section configured, IsValid returns false because required sub-fields are missing, or the env is missing both sets of variables.

Related errors


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