{"record":{"id":"ab081dbc6425a4a2","repo":"microsoft/aspire","slug":"an-openaiclient-could-not-be-configured-ensure-valid-ab081d","errorCode":null,"errorMessage":"An OpenAIClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}'.","messagePattern":"An OpenAIClient could not be configured\\. Ensure valid connection information was provided in 'ConnectionStrings:(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.Azure.AI.OpenAI/AspireConfigurableOpenAIExtensions.cs","lineNumber":96,"sourceCode":"\n        var connectionBuilder = new DbConnectionStringBuilder\n        {\n            ConnectionString = connectionString\n        };\n\n        if (connectionBuilder.TryGetValue(ConnectionStringEndpoint, out var endpoint) && endpoint != null && Uri.TryCreate(endpoint.ToString(), UriKind.Absolute, out var endpointUri))\n        {\n            serviceUri = endpointUri;\n        }\n\n        if (connectionBuilder.TryGetValue(ConnectionStringKey, out var key) && key != null)\n        {\n            apiKey = key.ToString()?.Trim();\n        }\n\n        if (serviceUri == null && string.IsNullOrEmpty(apiKey))\n        {\n            throw new InvalidOperationException($\"An OpenAIClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}'.\");\n        }\n\n        if (connectionBuilder.ContainsKey(ConnectionStringIsAzure))\n        {\n            return bool.TryParse(connectionBuilder[ConnectionStringIsAzure].ToString(), out var isAzure) && isAzure;\n        }\n\n        if (serviceUri != null && serviceUri.Host.Contains(\".azure.\", StringComparison.OrdinalIgnoreCase))\n        {\n            return true;\n        }\n\n        return false;\n    }\n}\n","sourceCodeStart":78,"sourceCodeEnd":112,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.Azure.AI.OpenAI/AspireConfigurableOpenAIExtensions.cs#L78-L112","documentation":"IsAzureConnectionString (used by AddOpenAIClientFromConfiguration / AddKeyedOpenAIClientFromConfiguration in the configurable OpenAI extension) throws InvalidOperationException when the connection string provides neither a service URI nor an API key — there is not enough information to construct an OpenAIClient.","triggerScenarios":"Passing a connection string that contains only unrelated keys (or is empty) to AddOpenAIClientFromConfiguration, so neither a Uri-valued entry nor a Key entry is found.","commonSituations":"Empty or placeholder connection string in appsettings; wrong key names (e.g. 'Token' instead of 'Key'); connection string copied from Azure OpenAI docs but used with the non-Azure configurable extension expecting Uri/Key; missing environment variable substituted into the string.","solutions":["Add 'Endpoint=https://api.openai.com/v1' (or a service URI) or 'Key=...' (or both) to 'ConnectionStrings:{connectionName}'","Fix key names in the connection string — the parser looks for a URI and a key entry","Verify the env var/config backing the connection string is actually populated at runtime"],"exampleFix":"// before\n\"ConnectionStrings\": { \"openai\": \"\" }\n// after\n\"ConnectionStrings\": { \"openai\": \"Endpoint=https://api.openai.com/v1;Key=sk-...\" }","handlingStrategy":"validation","validationCode":"var cs = builder.Configuration.GetConnectionString(\"openai\");\nif (string.IsNullOrWhiteSpace(cs) || !(cs.Contains(\"Endpoint=\") || cs.Contains(\"Key=\")))\n    throw new InvalidOperationException(\"Connection string must include Endpoint and/or Key.\");","typeGuard":null,"tryCatchPattern":"try { builder.AddOpenAIClientFromConfiguration(\"openai\"); }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"OpenAI connection string lacks endpoint/key\"); }","preventionTips":["Include at least 'Key=...' or 'Endpoint=...' in every OpenAI connection string","Validate substituted env vars are non-empty before launch","Use the Azure-specific extension when the connection string comes from an Azure OpenAI resource"],"tags":["openai","connection-string","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"}