{"record":{"id":"d5e78f529e9fdfdd","repo":"microsoft/aspire","slug":"an-openaiclient-could-not-be-configured-ensure-valid-d5e78f","errorCode":null,"errorMessage":"An OpenAIClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or specify a {nameof(settings.Key)} in the '{configurationSectionName}' configuration section.","messagePattern":"An OpenAIClient could not be configured\\. Ensure valid connection information was provided in 'ConnectionStrings:(.+?)' or specify a (.+?) in the '(.+?)' configuration section\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.OpenAI/AspireOpenAIExtensions.cs","lineNumber":140,"sourceCode":"            builder.Services.AddOpenTelemetry()\n                .WithMetrics(b => b.AddMeter(telemetrySources));\n        }\n\n        return new AspireOpenAIClientBuilder(builder, connectionName, serviceKey, settings.DisableTracing, settings.EnableSensitiveTelemetryData);\n\n        OpenAIClient ConfigureOpenAI(IServiceProvider serviceProvider)\n        {\n            if (settings.Key is not null)\n            {\n                var options = serviceKey is null ?\n                    serviceProvider.GetRequiredService<IOptions<OpenAIClientOptions>>().Value :\n                    serviceProvider.GetRequiredService<IOptionsMonitor<OpenAIClientOptions>>().Get(serviceKey);\n\n                return new OpenAIClient(new ApiKeyCredential(settings.Key), options);\n            }\n            else\n            {\n                throw new InvalidOperationException(\n                        $\"An OpenAIClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or \" +\n                        $\"specify a {nameof(settings.Key)} in the '{configurationSectionName}' configuration section.\");\n            }\n        }\n    }\n}\n","sourceCodeStart":122,"sourceCodeEnd":147,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.OpenAI/AspireOpenAIExtensions.cs#L122-L147","documentation":"The OpenAIClient factory throws when it cannot build a client: neither a connection string nor a settings.Key (via the configuration section) was available. The library fails fast rather than returning an unusable client.","triggerScenarios":"Registering AddOpenAIClient(\"openai\") with no 'ConnectionStrings:openai' entry and no ApiKey under the 'Aspire:OpenAI' configuration section; settings.Key remains null in the factory delegate.","commonSituations":"Missing OPENAI_API_KEY / configuration in a deployed environment; running the client project without an AppHost reference; typo in the connection name or config section; secrets not loaded from user-secrets or Key Vault.","solutions":["Add a connection string with the key, e.g. \"openai\": \"Endpoint=https://api.openai.com;Key=sk-...\" under ConnectionStrings","Set settings.Key via the configuration section, e.g. Aspire:OpenAI:ApiKey configuration or settings callback in AddOpenAIClient","Verify the connection name matches between AddOpenAIClient and configuration","Ensure the AppHost resource reference injects the connection string in the dev/deployed environment"],"exampleFix":"// before\nbuilder.AddOpenAIClient(\"openai\"); // nothing configured\n// after\n// appsettings.json / user-secrets:\n// { \"ConnectionStrings\": { \"openai\": \"Endpoint=https://api.openai.com;Key=sk-...\" } }\nbuilder.AddOpenAIClient(\"openai\");\n// or\nbuilder.AddOpenAIClient(\"openai\", settings => settings.Key = \"sk-...\");","handlingStrategy":"validation","validationCode":"var cs = builder.Configuration.GetConnectionString(\"openai\");\nvar key = builder.Configuration[\"Aspire:OpenAI:ApiKey\"];\nif (string.IsNullOrEmpty(cs) && string.IsNullOrEmpty(key))\n    throw new InvalidOperationException(\"OpenAI requires a connection string or Aspire:OpenAI:ApiKey.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    builder.AddOpenAIClient(\"openai\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"An OpenAIClient could not be configured\"))\n{\n    logger.LogError(ex, \"No OpenAI connection string or API key configured.\");\n    throw;\n}","preventionTips":["Reference the OpenAI hosting resource from the AppHost so the connection string is injected","Store the key in user-secrets/Key Vault with a startup validation check","Verify connection names and config section names match exactly"],"tags":["openai","api-key","configuration","connection-string"],"backgroundTag":"missing-api-key","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"}