microsoft/autogen · error · InvalidOperationException

Parameter name cannot be null

Error message

Parameter name cannot be null

What it means

FunctionContractExtension.ToOpenAIFunctionDefinition throws InvalidOperationException while converting a FunctionContract to an OpenAI FunctionDefinition when a parameter has a null Name. JSON-schema object properties are keyed by parameter name, so a nameless parameter cannot be represented.

Source

Thrown at dotnet/src/AutoGen.OpenAI.V1/Extension/FunctionContractExtension.cs:33

    /// Convert a <see cref="FunctionContract"/> to a <see cref="FunctionDefinition"/> that can be used in gpt funciton call.
    /// </summary>
    /// <param name="functionContract">function contract</param>
    /// <returns><see cref="FunctionDefinition"/></returns>
    public static FunctionDefinition ToOpenAIFunctionDefinition(this FunctionContract functionContract)
    {
        var functionDefinition = new FunctionDefinition
        {
            Name = functionContract.Name,
            Description = functionContract.Description,
        };
        var requiredParameterNames = new List<string>();
        var propertiesSchemas = new Dictionary<string, JsonSchema>();
        var propertySchemaBuilder = new JsonSchemaBuilder().Type(SchemaValueType.Object);
        foreach (var param in functionContract.Parameters ?? [])
        {
            if (param.Name is null)
            {
                throw new InvalidOperationException("Parameter name cannot be null");
            }

            var schemaBuilder = new JsonSchemaBuilder().FromType(param.ParameterType ?? throw new ArgumentException("param.ParameterType cannot be null"));
            if (param.Description != null)
            {
                schemaBuilder = schemaBuilder.Description(param.Description);
            }

            if (param.IsRequired)
            {
                requiredParameterNames.Add(param.Name);
            }

            var schema = schemaBuilder.Build();
            propertiesSchemas[param.Name] = schema;

        }
        propertySchemaBuilder = propertySchemaBuilder.Properties(propertiesSchemas);

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Give every parameter in functionContract.Parameters a non-null, unique Name
  2. Prefer building contracts from real .NET methods (the built-in reflection-based contract provider) so names are always populated
  3. Add a unit test asserting all parameter names are non-empty when contracts are created dynamically

Example fix

// before
var contract = new FunctionContract
{
    Name = "get_weather",
    Parameters = new[] { new FunctionParameter { ParameterType = typeof(string) } } // Name missing
};

// after
var contract = new FunctionContract
{
    Name = "get_weather",
    Parameters = new[] { new FunctionParameter { Name = "city", ParameterType = typeof(string), IsRequired = true } }
};
Defensive patterns

Strategy: validation

Validate before calling

bool ContractIsValid(FunctionContract c) =>
    (c.Parameters ?? []).All(p => !string.IsNullOrEmpty(p.Name));

Type guard

static bool HasNamedParameters(FunctionContract c) =>
    c.Parameters?.All(p => p.Name is not null) ?? true;

Try / catch

catch (InvalidOperationException ex) when (ex.Message == "Parameter name cannot be null")
{
    throw new ArgumentException($"Function {fn.Name} has a parameter without a name", ex);
}

Prevention

When it happens

Trigger: Registering a function/TypeTool whose contract was built with a parameter entry where Name was never assigned (e.g. hand-constructed FunctionContract.Parameters, or a custom contract builder bug).

Common situations: Manually authoring FunctionContract instances instead of deriving them from CLR methods; deserializing contracts from JSON where the name field casing didn't bind; partially-implemented IFunctionContract providers.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/e6e921b9c2b04e15. Report an issue: GitHub.