microsoft/semantic-kernel · error · InvalidOperationException

Invalid OpenAI client type '{connection.Type}' was specified

Error message

Invalid OpenAI client type '{connection.Type}' was specified.

What it means

Thrown by AgentDefinitionExtensions.GetOpenAIClient when the agent definition carries a ModelConnection whose Type is non-null but matches neither the recognized 'openai' nor 'azure_openai' (AzureOpenAI) constants. Only those two connection kinds can be turned into an OpenAIClient, so anything else is rejected.

Source

Thrown at dotnet/src/Agents/OpenAI/Extensions/AgentDefinitionExtensions.cs:158

            if (connection.Type.Equals(OpenAI, StringComparison.OrdinalIgnoreCase))
            {
                return OpenAIAssistantAgent.CreateOpenAIClient(connection.GetApiKeyCredential(), connection.TryGetEndpoint(), httpClient);
            }
            else if (connection.Type.Equals(AzureOpenAI, StringComparison.OrdinalIgnoreCase))
            {
                var endpoint = connection.TryGetEndpoint();
                Verify.NotNull(endpoint, "Endpoint must be specified when using Azure OpenAI.");

                if (connection.ExtensionData.TryGetValue(ApiKey, out var apiKey) && apiKey is not null)
                {
                    return OpenAIAssistantAgent.CreateAzureOpenAIClient(connection.GetApiKeyCredential(), endpoint, httpClient);
                }

                var tokenCredential = kernel.Services.GetRequiredService<TokenCredential>();
                return OpenAIAssistantAgent.CreateAzureOpenAIClient(tokenCredential, endpoint, httpClient);
            }

            throw new InvalidOperationException($"Invalid OpenAI client type '{connection.Type}' was specified.");
        }

        // Use the client registered on the kernel
        var client = kernel.GetAllServices<OpenAIClient>().FirstOrDefault();
        return (OpenAIClient?)client ?? throw new InvalidOperationException("OpenAI client not found.");
    }

    #region private
    private const string Temperature = "temperature";
    private const string TopP = "top_p";

    private static float? GetTemperature(this AgentDefinition agentDefinition)
    {
        Verify.NotNull(agentDefinition);

        if (agentDefinition?.Model?.Options?.TryGetValue(Temperature, out var temperature) ?? false)
        {
            return (float?)temperature;

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set Model.Connection.Type to exactly 'azure_openai' for Azure or 'openai' for the public service (comparison is case-insensitive).
  2. If you do not need a per-agent connection, remove the Model.Connection block so the kernel-resolved OpenAIClient is used instead.
  3. Validate the agent-definition file against the documented schema before passing it to the agent factory.
  4. Log connection.Type at startup to catch typos early in configuration loading.

Example fix

// before
model:
  connection:
    type: azure
// after
model:
  connection:
    type: azure_openai
Defensive patterns

Strategy: validation

Validate before calling

var type = agentDefinition?.Model?.Connection?.Type;
var valid = new[] { "openai", "azure_openai" };
if (type is not null && !valid.Contains(type, StringComparer.OrdinalIgnoreCase))
    throw new ArgumentException($"Unsupported connection type '{type}'. Valid: {string.Join(", ", valid)}");

Type guard

static bool IsValidConnectionType(string? type) =>
    type is null || type.Equals("openai", StringComparison.OrdinalIgnoreCase)
                 || type.Equals("azure_openai", StringComparison.OrdinalIgnoreCase);

Try / catch

try { var client = agentDefinition.GetOpenAIClient(kernel); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Invalid OpenAI client type")) {
    // surface configuration error to the user with valid types
}

Prevention

When it happens

Trigger: Calling GetOpenAIClient (directly or via agent invocation from a YAML/JSON AgentDefinition) where Model.Connection.Type is a typo like 'azure', 'open_ai', 'ollama', 'anthropic', or any provider the converter does not construct.

Common situations: Misspelled connection type in the agent-definition file; switching providers without updating the type field; leftover type value after renaming; using a self-hosted/local endpoint while the type still names a cloud provider.

Related errors


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