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

Request: Support Complex C# objects for SKFunction input parameters and output return types #2081

Closed
efficienthacks opened this issue Jul 19, 2023 · 4 comments
Assignees
Labels
kernel Issues or pull requests impacting the core kernel
Milestone

Comments

@efficienthacks
Copy link

efficienthacks commented Jul 19, 2023

I've asked this on the public community office hours and it looks like there isn't anything in development yet for this. This issue is similar to #1195 by @stephentoub , however instead of having multiple input parameters with primitive type, I'd like to define methods that have complex objects (that are easily serialized/deserialized) as input params and return values.

Here is an example:

    [SKFunction]
    //Is there any way to support POCO return types so that it automatically serializes the object?
    public string GetReportsOfAlias([Description("The aliases to get direct reports of"), SKName("input")] string alias)
    {
        //Would have liked to return "IEnumerable<Employee>" instead of a json string
        return JsonSerializer.Serialize(_reports[alias]);
    }

It would be nice to just have:

    [SKFunction]
    public IEnumerable<Employee> GetReportsOfAlias([Description...] string alias)
    {
        return _reports[alias];
    }

and then have the library automatically deserialize the input and serialize the output with JsonSerializer

A full working example is in a Polyglot notebook in case there is another suggestion to go about this: https://gist.github.com/efficienthacks/775bb94d727abbb7aec4b699f981dfac

My actually scenario consists of multiple of these 'native' methods, so without this feature, I'd have to create new methods, change the input/output parameters, then manually deserialize input at the start the method and serialize the output at the end of the method.

@efficienthacks
Copy link
Author

Idea for the auto-serialization of return values: let the user override the way the return values get Converted. e.g. allow some form of customization in SKFunction::GetParameterMarshalerDelegate

Not sure how to auto-deserialize input values yet

@evchaki
Copy link

evchaki commented Aug 2, 2023

@shawncal - can you add the plans for this.

@evchaki evchaki added kernel Issues or pull requests impacting the core kernel and removed triage labels Aug 2, 2023
@nacharya1 nacharya1 added this to the v1 milestone Sep 13, 2023
@dmytrostruk
Copy link
Member

Hi @efficienthacks , in this PR (#2864) we have added FunctionResult/KernelResult models which will allow to return not only strings but complex types as well. Here is usage example:

[Fact]
public async Task ItCanReturnComplexTypeAsync()
{
// Arrange
static MyCustomType TestCustomType(MyCustomType instance) => instance;
var context = this.MockContext("");
context.Variables.Set("instance", "42");
var function = SKFunction.FromNativeMethod(Method(TestCustomType));
// Act
FunctionResult result = await function.InvokeAsync(context);
var actualInstance = result.GetValue<MyCustomType>();
// Assert
Assert.NotNull(actualInstance);
Assert.Equal(42, actualInstance.Value);
}

Could you please confirm if this implementation covers your case?

@dmytrostruk
Copy link
Member

@efficienthacks I will close this issue, since it looks like your scenario is already covered, as mentioned in previous comment, and currently exists in main branch. If current functionality doesn't cover your case, feel free to re-open this issue or create a new one. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel Issues or pull requests impacting the core kernel
Projects
Archived in project
Development

No branches or pull requests

5 participants