microsoft/aspire · error · ArgumentException
Partition key paths must be a string or a string collection.
Error message
Partition key paths must be a string or a string collection.
What it means
AddContainerForPolyglot accepts a loosely-typed partitionKeyPaths parameter to support polyglot callers. At runtime it must be either a string (single path) or an IEnumerable<string>; any other type throws ArgumentException. This is a runtime type check because static typing cannot constrain the polyglot input.
Solutions
- Pass a single string path like "/id" or an IEnumerable<string> such as string[] or List<string>.
- Convert generic collections with .Cast<string>().ToArray() before calling.
- Normalize any dynamic input to IEnumerable<string> first and reject other types with your own validation.
Example fix
// before
object partitionKeys = new object[] { "/id", "/tenant" }; // object[] is not IEnumerable<string>
db.AddContainerForPolyglot("orders", partitionKeys, null);
// after
object partitionKeys = new[] { "/id", "/tenant" }; // string[]
db.AddContainerForPolyglot("orders", partitionKeys, null); Defensive patterns
Strategy: type-guard
Validate before calling
if (partitionKeyPaths is not string and not IEnumerable<string>) throw new ArgumentException("Partition key paths must be a string or a string collection."); Type guard
static bool IsValidPartitionKeyArg(object? o) => o is string or IEnumerable<string>;
Try / catch
try { db.AddContainerForPolyglot("orders", rawArg, null); } catch (ArgumentException ex) when (ex.ParamName == "partitionKeyPaths") { /* coerce rawArg to string[] and retry */ } Prevention
- Always pass string or string[]/List<string> from polyglot callers.
- Cast object collections with .Cast<string>().ToArray() first.
- Document the accepted argument shape in caller code.
When it happens
Trigger: Calling AddContainerForPolyglot with partitionKeyPaths that is neither string nor IEnumerable<string>, e.g. an object[], List<object>, char, or a boxed non-string value.
Common situations: Invocation from other-language or generated code that builds the argument dynamically; passing a JSON-derived array typed as object[] instead of string[].
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Could not invoke ' ' because parameter ' ' expects , but…
- Endpoint must be a string, endpoint reference, or reference…
- URL must be a string, Uri, or parameter resource builder.
- A of type cannot be assigned to a BicepValue< >.
- Address prefix must be a string or a parameter resource…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/cac288552276f1bb.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs:422
/// <summary>
/// Adds an Azure Cosmos DB container resource
/// </summary>
[AspireExport("addContainer")]
internal static IResourceBuilder<AzureCosmosDBContainerResource> AddContainerForPolyglot(
this IResourceBuilder<AzureCosmosDBDatabaseResource> builder,
[ResourceName] string name,
[AspireUnion(typeof(string), typeof(IEnumerable<string>))] object partitionKeyPaths,
string? containerName = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);
ArgumentNullException.ThrowIfNull(partitionKeyPaths);
return partitionKeyPaths switch
{
string partitionKeyPath => builder.AddContainer(name, partitionKeyPath, containerName),
IEnumerable<string> partitionKeyPathCollection => builder.AddContainer(name, partitionKeyPathCollection, containerName),
_ => throw new ArgumentException("Partition key paths must be a string or a string collection.", nameof(partitionKeyPaths))
};
}
/// <summary>
/// Adds a container to the associated Cosmos DB database resource with hierarchical partition keys.
/// </summary>
/// <param name="builder">CosmosDBDatabase resource builder.</param>
/// <param name="name">Name of container resource.</param>
/// <param name="partitionKeyPaths">Hierarchical partition key paths for the container.</param>
/// <param name="containerName">The name of the container. If not provided, this defaults to the same value as <paramref name="name"/>.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
[AspireExportIgnore(Reason = "Polyglot AppHosts use the internal addContainer dispatcher export.")]
public static IResourceBuilder<AzureCosmosDBContainerResource> AddContainer(this IResourceBuilder<AzureCosmosDBDatabaseResource> builder, [ResourceName] string name, IEnumerable<string> partitionKeyPaths, string? containerName = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);
ArgumentNullException.ThrowIfNull(partitionKeyPaths);
var partitionKeyPathsArray = partitionKeyPaths.ToArray();View on GitHub (pinned to 25830f84bd)