-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Description
Describe the bug
Possible problem with KernelJsonSchema generation when creating a KernelFunction from a method with parameters that meet 3 conditions:
- The parameter is an enum EDIT: also affects ints
- It is nullable
- It defaults to null
e.g. Calling KernelPluginFactory.CreateFromObject() on a plugin class containing the following method:
public string NewTool(MyEnumType? optionalEnumParam = null) {...}
Produces a schema containing the following for the optionalEnumParam parameter:
"optionalEnumParam":{"type":["string","null","null"],"enum":["val1","val2","val3",null]}
(note the duplicate "null")
Calls to an OpenAI model in strict mode (and presumably any other provider that validates the schema) with this tool result in the following error:
HTTP 400 (invalid_request_error: invalid_function_parameters)
Parameter: tools[4].function.parameters
Invalid schema for function 'MyPlugin-NewTool': ['string', 'null', 'null'] is not valid under any of the given schemas.
Expected behavior
The schema should have been "myEnumValue":{"type":["string","null"],"enum":["val1","val2","val3",null]}.
(no duplicated "null")
Removing the default null value (= null) creates the expected schema and it works as expected.
Platform
- C#
- Source: Microsoft.SemanticKernel (Version="1.70.0")
Additional context
This problem is fairly easy to work around (removing the defaulting to null), and I'm not sure if nullable parameters are technically recommended/supported - but I had been using this pattern with no problems until switching on strict mode (causes the model API to first validate, and then enforce tool call schemas - it fails at the validation stage)