microsoft/autogen · error · InvalidOperationException

Parameter name cannot be null

Error message

Parameter name cannot be null

What it means

InvalidOperationException thrown in FunctionContractExtension.ToChatTool when a FunctionContract parameter has a null Name. Each parameter must become a named property in the tool's JSON schema, so a nameless parameter cannot be represented.

Source

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

namespace AutoGen.OpenAI.Extension;

public static class FunctionContractExtension
{
    /// <summary>
    /// Convert a <see cref="FunctionContract"/> to a <see cref="ChatTool"/> that can be used in gpt funciton call.
    /// </summary>
    /// <param name="functionContract">function contract</param>
    /// <returns><see cref="ChatTool"/></returns>
    public static ChatTool ToChatTool(this FunctionContract functionContract)
    {
        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 ArgumentNullException(nameof(param.ParameterType)));
            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. Use TypeReflector.GetFunctionContract(delegate/methodInfo) so parameter names are populated automatically
  2. Ensure every FunctionParameter in a hand-built contract has a non-null Name
  3. Validate the contract before registration (assert param.Name is not null) to fail with better context

Example fix

// before
var contract = new FunctionContract
{
    Name = "get_weather",
    Parameters = [new FunctionParameter { Name = null, ParameterType = typeof(string) }],
};
tool = contract.ToChatTool(); // throws

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

Strategy: validation

Validate before calling

foreach (var p in contract.Parameters ?? [])
{
    if (p.Name is null) throw new ArgumentException($"Parameter '{p}' of function '{contract.Name}' has no name");
}

Type guard

static bool ContractHasNamedParameters(FunctionContract c) => (c.Parameters ?? []).All(p => p.Name is not null);

Try / catch

catch (InvalidOperationException ex) when (ex.Message == "Parameter name cannot be null") { /* fix contract and re-register tool */ }

Prevention

When it happens

Trigger: A FunctionContract whose Parameters collection contains a FunctionParameter with Name == null — usually from hand-built contracts, custom TypeReflector implementations, or plugin loading that failed to resolve parameter names.

Common situations: Manually authoring FunctionContract instead of deriving it from a delegate via TypeReflector; dynamic plugin loaders that lose parameter metadata; obfuscated/reflected methods where parameter names are unavailable.

Related errors


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