{"record":{"id":"0d5ecf5e59dd65ee","repo":"microsoft/aspire","slug":"model-must-be-a-foundrymodel-or-a-string-model-name","errorCode":null,"errorMessage":"Model must be a FoundryModel or a string model name.","messagePattern":"Model must be a FoundryModel or a string model name\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/FoundryExtensions.cs","lineNumber":108,"sourceCode":"    [AspireExport(\"addDeployment\")]\n    internal static IResourceBuilder<FoundryDeploymentResource> AddDeploymentForPolyglot(\n        this IResourceBuilder<FoundryResource> builder,\n        [ResourceName] string name,\n        [AspireUnion(typeof(FoundryModel), typeof(string))] object model,\n        string? modelVersion = null,\n        string? format = null)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentNullException.ThrowIfNull(model);\n        ArgumentException.ThrowIfNullOrEmpty(name);\n\n        return model switch\n        {\n            FoundryModel foundryModel when modelVersion is null && format is null => builder.AddDeployment(name, foundryModel),\n            FoundryModel => throw new ArgumentException(\"Model version and format must be omitted when using a FoundryModel.\", nameof(modelVersion)),\n            string modelName when modelVersion is not null && format is not null => builder.AddDeployment(name, modelName, modelVersion, format),\n            string => throw new ArgumentException(\"Model version and format are required when the model is provided as a string.\", nameof(modelVersion)),\n            _ => throw new ArgumentException(\"Model must be a FoundryModel or a string model name.\", nameof(model))\n        };\n    }\n\n    /// <summary>\n    /// Adds and returns a Microsoft Foundry Deployment resource to the application model using a <see cref=\"FoundryModel\"/>.\n    /// </summary>\n    /// <param name=\"builder\">The Microsoft Foundry resource builder.</param>\n    /// <param name=\"name\">The name of the Microsoft Foundry Deployment resource.</param>\n    /// <param name=\"model\">The model descriptor, using the <see cref=\"FoundryModel\"/> class like so: <code lang=\"csharp\">aiFoundry.AddDeployment(name: \"chat\", model: FoundryModel.OpenAI.Gpt5Mini)</code></param>\n    /// <returns>A reference to the <see cref=\"IResourceBuilder{T}\"/>.</returns>\n    /// <remarks>\n    /// <example>\n    /// Create a deployment for the OpenAI GTP-5-mini model:\n    /// <code lang=\"csharp\">\n    /// var builder = DistributedApplication.CreateBuilder(args);\n    ///\n    /// var aiFoundry = builder.AddFoundry(\"aiFoundry\");\n    /// var gpt5mini = aiFoundry.AddDeployment(\"chat\", FoundryModel.OpenAI.Gpt5Mini);","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/FoundryExtensions.cs#L90-L126","documentation":"AddDeploymentForPolyglot only accepts two shapes for the model argument: a FoundryModel instance or a string model name (with version and format). Any other type (or null) falls into the switch's discard arm and throws this ArgumentException.","triggerScenarios":"Passing null, or an object of an unexpected type (e.g. a custom model wrapper) as the model parameter of AddDeploymentForPolyglot.","commonSituations":"Passing a variable typed object/dynamic; refactoring changed the model type; passing null after a failed lookup that returned null instead of throwing.","solutions":["Pass a FoundryModel instance or a string model name","If the value can be null, check it before calling and fail fast with your own message"],"exampleFix":"// before\nobject model = GetModel();\nbuilder.AddDeploymentForPolyglot(\"deploy\", model);\n// after\nif (model is not (FoundryModel or string)) throw new InvalidOperationException($\"Unsupported model: {model?.GetType().Name}\");\nbuilder.AddDeploymentForPolyglot(\"deploy\", model);","handlingStrategy":"type-guard","validationCode":"if (model is null) throw new ArgumentNullException(nameof(model));","typeGuard":"bool IsValidModelArg(object? m) => m is FoundryModel or string;","tryCatchPattern":"try { builder.AddDeploymentForPolyglot(name, model); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"FoundryModel or a string\")) { /* normalize model to FoundryModel or string */ }","preventionTips":["Type the model parameter/variable as FoundryModel or string at the call site","Null-check or handle failed lookups before invoking the API","Avoid dynamic/object-typed intermediaries for the model value"],"tags":["azure-foundry","arguments","type-mismatch"],"backgroundTag":"invalid-argument-value","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"}