microsoft/autogen · error · ArgumentException

param.ParameterType cannot be null

Error message

param.ParameterType cannot be null

What it means

ToOpenAIFunctionDefinition throws ArgumentException when a FunctionContract parameter has a non-null Name but a null ParameterType. The schema builder needs a CLR type to derive the JSON schema for each property, and a null type cannot be mapped.

Source

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

    /// <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);
        propertySchemaBuilder = propertySchemaBuilder.Required(requiredParameterNames);

        var option = new System.Text.Json.JsonSerializerOptions()

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set ParameterType explicitly (e.g. typeof(string), typeof(int)) for every parameter
  2. Derive contracts from actual .NET methods so ParameterType is populated automatically
  3. Validate contracts before registration: assert Name != null && ParameterType != null for all parameters

Example fix

// before
new FunctionParameter { Name = "city" } // ParameterType null

// after
new FunctionParameter { Name = "city", ParameterType = typeof(string), Description = "City name", IsRequired = true }
Defensive patterns

Strategy: validation

Validate before calling

bool ContractTypesValid(FunctionContract c) =>
    (c.Parameters ?? []).All(p => p.ParameterType is not null);

Type guard

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

Try / catch

catch (ArgumentException ex) when (ex.Message.Contains("ParameterType cannot be null"))
{
    logger.LogError("Parameter {Param} of {Fn} lacks a type", currentParam, fn.Name);
    throw;
}

Prevention

When it happens

Trigger: A hand-built FunctionParameter with Name set but ParameterType left null; contract JSON deserialization where the type field was dropped; custom IFuntionContractTypeProvider implementations returning null types.

Common situations: Dynamic function registration where contracts are assembled at runtime; schema-driven tool definitions that carry only names and descriptions; upgrading AutoGen versions that changed how parameter types are resolved.

Related errors


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