microsoft/autogen · error · ArgumentNullException

Value cannot be null. (Parameter 'functionContract.Name')

Error message

Value cannot be null. (Parameter 'functionContract.Name')

What it means

ArgumentNullException ('functionContract.Name') thrown by ChatTool.CreateFunctionTool inside ToChatTool when the FunctionContract has a null Name. The OpenAI tools API requires every function tool to have a name, so a nameless contract cannot be converted.

Source

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

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

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

        }
        propertySchemaBuilder = propertySchemaBuilder.Properties(propertiesSchemas);
        propertySchemaBuilder = propertySchemaBuilder.Required(requiredParameterNames);

        var option = new System.Text.Json.JsonSerializerOptions()
        {
            PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase
        };

        var functionDefinition = ChatTool.CreateFunctionTool(
           functionContract.Name ?? throw new ArgumentNullException(nameof(functionContract.Name)),
           functionContract.Description,
           BinaryData.FromObjectAsJson(propertySchemaBuilder.Build(), option));

        return functionDefinition;
    }

    /// <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>
    [Obsolete("Use ToChatTool instead")]
    public static ChatTool ToOpenAIFunctionDefinition(this FunctionContract functionContract)
    {
        return functionContract.ToChatTool();
    }
}

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set FunctionContract.Name to a valid function name (letters, digits, underscores, <=64 chars)
  2. Create contracts via TypeReflector.GetFunctionContract so Name comes from the delegate/method
  3. Null-check contract.Name before registering tools and throw a clearer error at your boundary

Example fix

// before
var contract = new FunctionContract { Description = "Gets weather" }; // Name missing
contract.ToChatTool(); // ArgumentNullException

// after
var contract = new FunctionContract { Name = "get_weather", Description = "Gets weather", ... };
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(contract.Name)) { /* assign a valid function name before ToChatTool */ }

Type guard

static bool ContractHasName(FunctionContract c) => !string.IsNullOrWhiteSpace(c.Name);

Try / catch

catch (ArgumentNullException ex) when (ex.ParamName == "functionContract.Name") { /* set Name and re-register */ }

Prevention

When it happens

Trigger: Calling ToChatTool() on a FunctionContract constructed without setting Name; contract built from reflection where the method name could not be resolved.

Common situations: Hand-authored contracts with a typo (e.g. setting Description but not Name); dynamic method wrappers that lose the method name; test fixtures constructed incompletely.

Related errors


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