microsoft/autogen · error · ArgumentNullException
Value cannot be null. (Parameter 'param.ParameterType')
Error message
Value cannot be null. (Parameter 'param.ParameterType')
What it means
ArgumentNullException ('param.ParameterType') thrown by ToChatTool when a function parameter has a null ParameterType. The extension must call JsonSchemaBuilder.FromType(param.ParameterType), which requires a concrete System.Type to generate the parameter's JSON schema.
Source
Thrown at dotnet/src/AutoGen.OpenAI/Extension/FunctionContractExtension.cs:31
{
/// <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);
propertySchemaBuilder = propertySchemaBuilder.Required(requiredParameterNames);
var option = new System.Text.Json.JsonSerializerOptions()View on GitHub (pinned to 027ecf0a37)
Solutions
- Always set ParameterType when constructing FunctionParameter (e.g. typeof(string))
- Prefer TypeReflector.GetFunctionContract so types come from real method metadata
- Validate the contract (null-check Name and ParameterType for every parameter) before calling ToChatTool
Example fix
// before
new FunctionParameter { Name = "city", ParameterType = null }
// ToChatTool -> ArgumentNullException
// after
new FunctionParameter { Name = "city", ParameterType = typeof(string), IsRequired = true } Defensive patterns
Strategy: validation
Validate before calling
foreach (var p in contract.Parameters ?? [])
{
if (p.ParameterType is null) throw new ArgumentException($"Parameter '{p.Name}' of '{contract.Name}' has no type");
} Type guard
static bool ContractHasTypedParameters(FunctionContract c) => (c.Parameters ?? []).All(p => p.ParameterType is not null);
Try / catch
catch (ArgumentNullException ex) when (ex.ParamName == "param.ParameterType") { /* set ParameterType and rebuild tool */ } Prevention
- Always set ParameterType when authoring FunctionParameter
- Use TypeReflector for reflection-based contracts
- Validate contracts before ToChatTool
When it happens
Trigger: A FunctionParameter with ParameterType == null inside a FunctionContract passed to ToChatTool — typically from reflection over generic/obfuscated code or hand-constructed contracts missing the type.
Common situations: Hand-built FunctionContract objects; custom TypeReflector replacements; methods with dynamic/COM parameters where reflection yields null types; partially-initialized contract objects.
Related errors
- Parameter name cannot be null
- Value cannot be null. (Parameter 'functionContract.Name')
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/f320fbdc41b88edd.
Report an issue: GitHub.