microsoft/aspire · error · InvalidOperationException
The connection string
Error message
The connection string '{ConnectionName}' contains both '{DeploymentKey}' and '{ModelKey}' keys. Either of these may be specified, but not both. What it means
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.
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
Example fix
// before "ConnectionStrings:openai": "Endpoint=https://api.openai.com;Key=sk-...;Deployment=gpt-4o;Model=gpt-4o" // after "ConnectionStrings:openai": "Endpoint=https://api.openai.com;Key=sk-...;Model=gpt-4o"
Defensive patterns
Strategy: validation
Validate before calling
var parts = new System.Data.Common.DbConnectionStringBuilder { ConnectionString = cs };
bool hasDeployment = parts.ContainsKey("Deployment");
bool hasModel = parts.ContainsKey("Model");
if (hasDeployment && hasModel)
throw new InvalidOperationException("Connection string may contain Deployment or Model, not both."); Try / catch
try
{
builder.AddOpenAIClient("openai");
}
catch (InvalidOperationException ex) when (ex.Message.Contains("both 'Deployment' and 'Model'"))
{
logger.LogError(ex, "OpenAI connection string has conflicting Deployment/Model keys.");
throw;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- An OpenAIClient could not be configured. Ensure valid…
- An OpenAIClient could not be configured. Ensure valid…
- A BlobServiceClient could not be configured. Ensure valid…
- A BlobServiceClient could not be configured. Ensure valid…
- A ChatCompletionsClient could not be configured. Ensure…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/cd213ec83816573a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Components/Aspire.OpenAI/AspireOpenAIClientBuilder.cs:72
/// </summary>
public virtual string ConfigurationSectionName => ServiceKey is null ?
AspireOpenAIExtensions.DefaultConfigSectionName :
$"{AspireOpenAIExtensions.DefaultConfigSectionName}:{ServiceKey}";
internal string GetRequiredDeploymentName()
{
string? deploymentName = null;
var configuration = HostBuilder.Configuration;
if (configuration.GetConnectionString(ConnectionName) is string connectionString)
{
// Some hosting solutions require named deployments, while others identify the target directly by model name.
var connectionBuilder = new DbConnectionStringBuilder { ConnectionString = connectionString };
var deploymentValue = ConnectionStringValue(connectionBuilder, DeploymentKey);
var modelValue = ConnectionStringValue(connectionBuilder, ModelKey);
if (deploymentValue is not null && modelValue is not null)
{
throw new InvalidOperationException(
$"The connection string '{ConnectionName}' contains both '{DeploymentKey}' and '{ModelKey}' keys. Either of these may be specified, but not both.");
}
deploymentName = deploymentValue ?? modelValue;
}
if (string.IsNullOrEmpty(deploymentName))
{
var configSection = configuration.GetSection(ConfigurationSectionName);
deploymentName = configSection[DeploymentKey];
}
if (string.IsNullOrEmpty(deploymentName))
{
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.");
}
return deploymentName;View on GitHub (pinned to 25830f84bd)