{"record":{"id":"1f8047c4a366a0bb","repo":"microsoft/aspire","slug":"a-chatcompletionsclient-could-not-be-configured-ensure-valid","errorCode":null,"errorMessage":"A ChatCompletionsClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or specify a '{nameof(ChatCompletionsClientSettings.Endpoint)}' and optionally a '{nameof(ChatCompletionsClientSettings.Key)}' in the '{configurationSectionName}' configuration section.","messagePattern":"A ChatCompletionsClient could not be configured\\. Ensure valid connection information was provided in 'ConnectionStrings:(.+?)' or specify a '(.+?)' and optionally a '(.+?)' in the '(.+?)' configuration section\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs","lineNumber":149,"sourceCode":"    private sealed class ChatCompletionsClientServiceComponent : AzureComponent<ChatCompletionsClientSettings, ChatCompletionsClient, AzureAIInferenceClientOptions>\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<ChatCompletionsClient, AzureAIInferenceClientOptions> AddClient(\n            AzureClientFactoryBuilder azureFactoryBuilder,\n            ChatCompletionsClientSettings settings,\n            string connectionName, string\n            configurationSectionName)\n        {\n            return azureFactoryBuilder.AddClient<ChatCompletionsClient, AzureAIInferenceClientOptions>((options, _, _) =>\n            {\n                if (settings.Endpoint is null)\n                {\n                    throw new InvalidOperationException($\"A ChatCompletionsClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or specify a '{nameof(ChatCompletionsClientSettings.Endpoint)}' and optionally a '{nameof(ChatCompletionsClientSettings.Key)}' in the '{configurationSectionName}' configuration section.\");\n                }\n                else\n                {\n                    var endpoint = settings.Endpoint;\n\n                    // Connect to Azure AI Foundry using key auth\n                    if (!string.IsNullOrEmpty(settings.Key))\n                    {\n                        var credential = new AzureKeyCredential(settings.Key);\n                        return new ChatCompletionsClient(endpoint, credential, options);\n                    }\n                    else\n                    {\n                        var credential = settings.TokenCredential ?? AzureCredentialHelper.CreateDefaultAzureCredential();\n\n                        // Defines the scopes used for authorization when connecting to Azure AI Inference services.\n                        // Use the default one (ml.azure.com) and add the public one required for Azure Foundry AI.\n                        // If users want to use a different scope they can configure the option using the client builder.","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L131-L167","documentation":"AspireAzureAIInferenceExtensions.AddClient configures a ChatCompletionsClient lazily and throws InvalidOperationException at client creation time when settings.Endpoint is null — meaning no usable connection string or configured endpoint was found.","triggerScenarios":"Calling AddChatCompletionsClient/AddAzureAIInferenceClient with a connection name whose 'ConnectionStrings:{name}' entry is missing or unparseable, and no Endpoint in the '{configurationSectionName}' config section.","commonSituations":"Connection string not registered in appsettings or environment (common in Aspire when the resource isn't referenced); malformed connection string the parser couldn't map to an endpoint; running the app outside the AppHost without copying connection info.","solutions":["Ensure 'ConnectionStrings:{connectionName}' exists and contains a valid endpoint (e.g. Endpoint=https://<resource>.services.ai.azure.com/models)","Or set Endpoint (and optionally Key) under the configuration section passed to the extension","If using Aspire hosting, add a WithReference(chatResource) so the connection string is injected"],"exampleFix":"// before\nbuilder.AddChatCompletionsClient(\"chat\"); // no such connection string\n// after\nbuilder.AddChatCompletionsClient(\"chat\");\n// with appsettings: \"ConnectionStrings\": { \"chat\": \"Endpoint=https://myai.services.ai.azure.com/models;Key=...\" }","handlingStrategy":"validation","validationCode":"var cs = builder.Configuration.GetConnectionString(\"chat\");\nif (string.IsNullOrEmpty(cs) && builder.Configuration.GetSection(\"Aspire:Azure:AI:Inference\").GetSection(\"ChatCompletionsClient\")[\"Endpoint\"] is null)\n    throw new InvalidOperationException(\"No ChatCompletions endpoint configured.\");","typeGuard":null,"tryCatchPattern":"try { var client = serviceProvider.GetRequiredService<ChatCompletionsClient>(); }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"ChatCompletionsClient not configured\"); }","preventionTips":["Use WithReference in the AppHost so connection strings are injected","Verify ConnectionStrings entries exist in appsettings/environment before deployment","Keep endpoint connection string format matching Azure AI Inference expectations"],"tags":["azure","configuration","connection-string"],"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"}