Repository navigation
.Net: Use parameter default value when a null argument targets a non-nullable value type - #14653
John Karam (johnkaram02) wants to merge 1 commit into
Conversation
…nullable value type When a KernelArguments value is null and the method parameter is a non-nullable value type with a default value, KernelFunctionFromMethod tried to convert null and threw ArgumentOutOfRangeException. OpenAI strict mode makes optional parameters nullable, so models send null for them and the call fails. Fall back to the parameter's default value in that case.
|
John Karam (@johnkaram02) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
1 similar comment
|
John Karam (@johnkaram02) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
🟢 Approval recommended
The narrow condition preserves other argument-handling paths, and the regression test covers the reported failure.
No open issues 🎉
What changed in this PR
Fixes #14652 by using a parameter’s default value when an explicit null targets an optional, non-nullable value type. This supports strict-schema function calls without changing null handling for other parameter types.
Changes:
- Routes eligible null arguments through the existing default-value fallback.
- Adds a regression test verifying that an optional
intreceives its default value.
| File | Description |
|---|---|
| dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromMethodTests1.cs | Tests default-value handling for an explicit null argument. |
| dotnet/src/SemanticKernel.Core/Functions/KernelFunctionFromMethod.cs | Adjusts argument marshaling to use the default for eligible null values. |
Review effort: Balanced
Motivation and Context
KernelFunctionFromMethodthrowsArgumentOutOfRangeExceptionwhen an argument is present with anullvalue and the target parameter is a non-nullable value type with a default value, for exampleint limit = 10.With
AllowStrictSchemaAdherence = true, the OpenAI connector marks optional parameters as required and nullable, so a model can sendnullfor an optional parameter it skips. A plugin method with an optional value-type parameter then fails on that call, and the model receives an error instead of the function result.Fixes #14652
Description
When the argument value is
null, the parameter has a default value, and the parameter type is a non-nullable value type, the marshaler now skips the argument and uses the default value, the same as when the argument is missing.The change only affects the case that currently throws. These cases keep their current behavior:
nullfor a non-nullable value type without a default value still throwsArgumentOutOfRangeException.nullfor a reference type orNullable<T>parameter is still passed asnull.A new unit test,
ItShouldUseDefaultValueWhenNullIsPassedForValueTypeParameterWithDefaultValueAsync, covers the fix. It fails onmainwithArgumentOutOfRangeExceptionand passes with this change. All 1615 tests inSemanticKernel.UnitTestspass.Contribution Checklist