{"record":{"id":"cd213ec83816573a","repo":"microsoft/aspire","slug":"the-connection-string-connectionname-contains-both","errorCode":null,"errorMessage":"The connection string '{ConnectionName}' contains both '{DeploymentKey}' and '{ModelKey}' keys. Either of these may be specified, but not both.","messagePattern":"The connection string '(.+?)' contains both '(.+?)' and '(.+?)' keys\\. Either of these may be specified, but not both\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.OpenAI/AspireOpenAIClientBuilder.cs","lineNumber":72,"sourceCode":"    /// </summary>\n    public virtual string ConfigurationSectionName => ServiceKey is null ?\n        AspireOpenAIExtensions.DefaultConfigSectionName :\n        $\"{AspireOpenAIExtensions.DefaultConfigSectionName}:{ServiceKey}\";\n\n    internal string GetRequiredDeploymentName()\n    {\n        string? deploymentName = null;\n\n        var configuration = HostBuilder.Configuration;\n        if (configuration.GetConnectionString(ConnectionName) is string connectionString)\n        {\n            // Some hosting solutions require named deployments, while others identify the target directly by model name.\n            var connectionBuilder = new DbConnectionStringBuilder { ConnectionString = connectionString };\n            var deploymentValue = ConnectionStringValue(connectionBuilder, DeploymentKey);\n            var modelValue = ConnectionStringValue(connectionBuilder, ModelKey);\n            if (deploymentValue is not null && modelValue is not null)\n            {\n                throw new InvalidOperationException(\n                    $\"The connection string '{ConnectionName}' contains both '{DeploymentKey}' and '{ModelKey}' keys. Either of these may be specified, but not both.\");\n            }\n\n            deploymentName = deploymentValue ?? modelValue;\n        }\n\n        if (string.IsNullOrEmpty(deploymentName))\n        {\n            var configSection = configuration.GetSection(ConfigurationSectionName);\n            deploymentName = configSection[DeploymentKey];\n        }\n\n        if (string.IsNullOrEmpty(deploymentName))\n        {\n            throw new InvalidOperationException($\"The deployment could not be determined. Ensure a '{DeploymentKey}' or '{ModelKey}' value is provided in 'ConnectionStrings:{ConnectionName}', or specify a '{DeploymentKey}' in the '{ConfigurationSectionName}' configuration section, or specify a '{nameof(deploymentName)}' in the call.\");\n        }\n\n        return deploymentName;","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.OpenAI/AspireOpenAIClientBuilder.cs#L54-L90","documentation":"GetRequiredDeploymentName throws when the resolved connection string contains both a Deployment key and a Model key. The library cannot decide which identifier to target, so the connection string must contain exactly one of the two.","triggerScenarios":"Providing a connection string like \"Endpoint=https://x;Key=k;Deployment=gpt4o;Model=gpt4o\" to AddOpenAIClient and resolving a deployment name from it — both keys present simultaneously.","commonSituations":"Copy-pasting Azure OpenAI style strings (Deployment=) and OpenAI style strings (Model=) into one connection string; merging environment-specific connection strings; template concatenation accidentally including both keys.","solutions":["Remove either the Deployment= or the Model= key from the connection string so only one remains","Standardize on Model= for OpenAI endpoints and Deployment= for Azure OpenAI endpoints","Keep the value in the config section instead if both notions are needed"],"exampleFix":"// before\n\"ConnectionStrings:openai\": \"Endpoint=https://api.openai.com;Key=sk-...;Deployment=gpt-4o;Model=gpt-4o\"\n// after\n\"ConnectionStrings:openai\": \"Endpoint=https://api.openai.com;Key=sk-...;Model=gpt-4o\"","handlingStrategy":"validation","validationCode":"var parts = new System.Data.Common.DbConnectionStringBuilder { ConnectionString = cs };\nbool hasDeployment = parts.ContainsKey(\"Deployment\");\nbool hasModel = parts.ContainsKey(\"Model\");\nif (hasDeployment && hasModel)\n    throw new InvalidOperationException(\"Connection string may contain Deployment or Model, not both.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    builder.AddOpenAIClient(\"openai\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"both 'Deployment' and 'Model'\"))\n{\n    logger.LogError(ex, \"OpenAI connection string has conflicting Deployment/Model keys.\");\n    throw;\n}","preventionTips":["Standardize on Model= for OpenAI and Deployment= for Azure OpenAI","Validate connection strings with a config linter before deployment","Avoid merging connection-string fragments from multiple environments"],"tags":["openai","connection-string","conflicting-keys","configuration"],"backgroundTag":"mutually-exclusive-options","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"}