{"record":{"id":"f2b34f5083a3560f","repo":"microsoft/aspire","slug":"an-openaiclient-could-not-be-configured-ensure-valid","errorCode":null,"errorMessage":"An OpenAIClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or specify a '{nameof(AzureOpenAISettings.Endpoint)}' or '{nameof(AzureOpenAISettings.Key)}' in the '{configurationSectionName}' configuration section.","messagePattern":"An OpenAIClient could not be configured\\. Ensure valid connection information was provided in 'ConnectionStrings:(.+?)' or specify a '(.+?)' or '(.+?)' in the '(.+?)' configuration section\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.Azure.AI.OpenAI/AspireAzureOpenAIExtensions.cs","lineNumber":101,"sourceCode":"    }\n\n    private sealed class OpenAIComponent : AzureComponent<AzureOpenAISettings, AzureOpenAIClient, AzureOpenAIClientOptions>\n    {\n        // GenAI telemetry isn't stable so MEAI currently has source name of \"Experimental.Microsoft.Extensions.AI\".\n        // Listen to both names to ensure we capture telemetry from both stable and experimental versions.\n        // When MEAI removes experimental from the source name, Aspire will continue to work without changes.\n        protected override string[] ActivitySourceNames => [\"Experimental.Microsoft.Extensions.AI\", \"Microsoft.Extensions.AI\"];\n        protected override string[] MetricSourceNames => [\"Experimental.Microsoft.Extensions.AI\", \"Microsoft.Extensions.AI\"];\n\n        protected override IAzureClientBuilder<AzureOpenAIClient, AzureOpenAIClientOptions> AddClient(\n            AzureClientFactoryBuilder azureFactoryBuilder, AzureOpenAISettings settings, string connectionName,\n            string configurationSectionName)\n        {\n            return azureFactoryBuilder.AddClient<AzureOpenAIClient, AzureOpenAIClientOptions>((options, _, _) =>\n            {\n                if (settings.Endpoint is null)\n                {\n                    throw new InvalidOperationException($\"An OpenAIClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or specify a '{nameof(AzureOpenAISettings.Endpoint)}' or '{nameof(AzureOpenAISettings.Key)}' in the '{configurationSectionName}' configuration section.\");\n                }\n                else\n                {\n                    // Connect to Azure OpenAI\n\n                    if (!string.IsNullOrEmpty(settings.Key))\n                    {\n                        var credential = new ApiKeyCredential(settings.Key);\n                        return new AzureOpenAIClient(settings.Endpoint, credential, options);\n                    }\n                    else\n                    {\n                        return new AzureOpenAIClient(settings.Endpoint, settings.Credential ?? AzureCredentialHelper.CreateDefaultAzureCredential(), options);\n                    }\n                }\n            });\n        }\n","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.Azure.AI.OpenAI/AspireAzureOpenAIExtensions.cs#L83-L119","documentation":"AspireAzureOpenAIExtensions.AddClient throws InvalidOperationException when AzureOpenAISettings.Endpoint is null at client creation, meaning neither the connection string nor the config section yielded an endpoint (or key-only credential info).","triggerScenarios":"AddAzureOpenAIClient called with a connection name that has no 'ConnectionStrings:{name}' entry, or an unparseable entry, and no Endpoint in '{configurationSectionName}'.","commonSituations":"AppHost resource not referenced (no injected connection string); appsettings missing the Azure OpenAI section; running outside Aspire without providing endpoint+key; connection string format the parser doesn't recognize (e.g. bare key without IsAzure flag).","solutions":["Provide 'ConnectionStrings:{connectionName}' with Endpoint=https://<resource>.openai.azure.com/ (and Key if using key auth)","Or set Endpoint and Key under the '{configurationSectionName}' section","Add WithReference(azureOpenAI) in the AppHost so connection info is injected"],"exampleFix":"// before\nbuilder.AddAzureOpenAIClient(\"openai\"); // no connection string\n// after\n// appsettings: \"ConnectionStrings\": { \"openai\": \"Endpoint=https://myres.openai.azure.com/;Key=...\" }\nbuilder.AddAzureOpenAIClient(\"openai\");","handlingStrategy":"validation","validationCode":"var cs = builder.Configuration.GetConnectionString(\"openai\");\nif (string.IsNullOrEmpty(cs) && builder.Configuration[\"Aspire:Azure:AI:OpenAI:Endpoint\"] is null)\n    throw new InvalidOperationException(\"No Azure OpenAI endpoint configured.\");","typeGuard":null,"tryCatchPattern":"try { var client = serviceProvider.GetRequiredService<AzureOpenAIClient>(); }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"AzureOpenAIClient not configured\"); }","preventionTips":["Always pair AddAzureOpenAIClient with WithReference in the AppHost","Keep Endpoint and Key in sync in config sections","Test configuration binding in CI with a minimal host"],"tags":["azure","openai","configuration"],"backgroundTag":"missing-required-config-field","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}