Skip to content

.Net: Bug: null argument for an optional value-type parameter throws instead of using the default value #14652

Open

Description

Describe the bug

KernelFunctionFromMethod throws ArgumentOutOfRangeException when an argument value is null and the method parameter is a non-nullable value type with a default value, such as int count = 5. The default value is used only when the argument is missing, not when it is present with a null value.

This breaks function calling with FunctionChoiceBehaviorOptions.AllowStrictSchemaAdherence = true. In strict mode the OpenAI connector marks every parameter as required and adds "null" to the type of optional parameters (OpenAIFunction.ToFunctionDefinition). A model can then send null for an optional parameter it doesn't want to set. The connector stores that null in KernelArguments, and the function invocation fails, so the model gets an error result instead of the function output.

To reproduce

var function = KernelFunctionFactory.CreateFromMethod((int count = 5) => count);

// Throws ArgumentOutOfRangeException: Null object cannot be converted to a value type. (Parameter 'count')
var result = await function.InvokeAsync(new Kernel(), new() { ["count"] = null });

Based on the connector code, the same happens when a plugin method such as public string GetItems(string category, int limit = 10) is advertised with strict mode and the model calls it with {"category":"books","limit":null}. I reproduced it with the unit-level example above, not against a live model.

Expected behavior

The function runs with the parameter's default value (5 in the example above), the same as when the argument is omitted.

Platform

  • Language: C#
  • Source: main at 483ffb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codetriage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions