Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Kernel function arguments cannot be of type Array<Enum> #5451

Closed
EdenTanami opened this issue Mar 12, 2024 · 3 comments · Fixed by #5928
Closed

.Net: Kernel function arguments cannot be of type Array<Enum> #5451

EdenTanami opened this issue Mar 12, 2024 · 3 comments · Fixed by #5928
Assignees
Labels
core plugin Anything related to core plugins .NET Issue or Pull requests regarding .NET code

Comments

@EdenTanami
Copy link

Describe the bug
Kernel function with argument of type Enum[] throws on invocation.

I am writing a kernel function that finds toys with the following signature:
public string FindToy(Color[] colors) {...}
public enum Color { Blue, Red, Green }
Then I ask in the chat question to invoke my function.

  • This is the parameters schema sent to openAI sdk:
    {{"type":"object","required":["colors"],"properties":{"colors":{"type":"array","items":{"enum":["Blue","Red","Green"]}}}}}
  • This is the arguments in the model responses' ToolCalls:
    "{"colors":["Blue","Red"]}"
  • When invoking in this function SK throws this exception:
    Function failed. Error: Object of type 'System.String' cannot be converted to type 'SKCopilotToolkit.Sample.SampleMockScenario.Plugins.ToysPlugin+Color[]'.

To Reproduce
Steps to reproduce the behavior:

  1. Register the function with the following signature to the kernel:
    [KernelFunction("FindToy")] public string FindToy(Color[] colors)
  2. Ask the chat completion service for toys in some colors
  3. examine the errors in the logs

Expected behavior
The function will be called with array of Enum values [Blue, Red]

Platform

  • OS: Windows
  • IDE: Visual Studio,
  • Language: C#
  • Source: SK NuGet package version 1.5.0
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage labels Mar 12, 2024
@stephentoub
Copy link
Member

cc: @SergeyMenshykh

@SergeyMenshykh
Copy link
Member

Hi @EdenTanami, thank you for reporting this issue. Here are two options you can use until a long-term solution is found:

  1. Register the JsonStringEnumConverter converter for your enum. For example - [JsonConverter(typeof(JsonStringEnumConverter))]
  2. Modify your function parameter type from Color[] to string[].

@SergeyMenshykh
Copy link
Member

SergeyMenshykh commented Mar 13, 2024

[Context] Option 1 will not work in cases where there is no access to the enum to mark it with the attribute. A possible solution could be allowing consumer code to specify JsonSerializerOptions for controlling tool call argument deserialization behavior.

@markwallace-microsoft markwallace-microsoft added core plugin Anything related to core plugins and removed triage labels Mar 15, 2024
github-merge-queue bot pushed a commit that referenced this issue Apr 21, 2024
…5928)

### Motivation and Context

Close #5451 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
LudoCorporateShark pushed a commit to LudoCorporateShark/semantic-kernel that referenced this issue Aug 25, 2024
…icrosoft#5928)

### Motivation and Context

Close microsoft#5451 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core plugin Anything related to core plugins .NET Issue or Pull requests regarding .NET code
Projects
Archived in project
4 participants