Skip to content

Unable to define an AIFunction with a required parameter. #6302

@MikeAlhayek

Description

@MikeAlhayek

Description

I'm trying to create an AIFunction that allows the user to import a JSON recipe. However, during the chat request, I encounter the following error:

Invalid schema for function 'import_recipe': In context=(), 'required' must be an array that includes every key in properties. Extra required key 'recipe' supplied.

It seems a bit misleading, as I am providing the recipe parameter and including it in the required array. Could this be a misinterpretation of the schema requirements, or is there something I'm missing?

Reproduction Steps

Define the AIFunction provided above and invoke it in a chat request using a prompt like the one below.

public sealed class ImportOrchardCoreRecipeTool : AIFunction
{
    public override string Name => "import_recipe";

    public override string Description => "Imports a dynamic OrchardCore JSON recipe";

    public override JsonElement JsonSchema { get; }

    public ImportOrchardCoreRecipeTool()
    {
        var metadata = new JsonObject
        {
            {"type", "object"},
            {"properties", new JsonObject
                {
                    { "recipe", new JsonObject
                        {
                            {"type", "object" },
                            {"description", "A JSON object representing an OrchardCore recipe" },
                        }
                    }
                }
            },
            {"required", new JsonArray("recipe") },
            {"return_type", new JsonObject
                {
                    {"type", "boolean"},
                    {"description", "The result of the import process. True representing a successful import while false failed."},
                }
            },
        };

        JsonSchema = JsonSerializer.Deserialize<JsonElement>(metadata);
    }

    protected override ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken)
    {
        ArgumentNullException.ThrowIfNull(arguments);

        if (!arguments.TryGetValue("recipe", out var data))
        {
            return ValueTask.FromResult<object?>(false);
        }

        var recipe = ConvertToJsonObject(data!);

        if (recipe == null)
        {
            return ValueTask.FromResult<object?>(false);
        }

        Console.WriteLine($"The {Name} function was invoked.");

        return ValueTask.FromResult<object?>(true);
    }

    private static JsonObject? ConvertToJsonObject(object data)
    {
        JsonObject? recipe = null;

        if (data is JsonObject obj)
        {
            recipe = obj;
        }
        else
        {
            string? json;

            if (data is JsonElement jsonElement)
            {
                json = jsonElement.GetString();
            }
            else
            {
                json = data.ToString();
            }

            try
            {
                recipe = JsonSerializer.Deserialize<JsonObject>(json);
            }
            catch { }
        }

        return recipe;
    }
}

Create a OrchardCore content type named "Employee" with fields for First Name, Last Name, Biography, and Email Address.

Expected behavior

I expect that the AI model would invoke my tool by providing recipe as a argument.

Actual behavior

Exception:

Invalid schema for function 'import_recipe': In context=(), 'required' must be an array that includes every key in properties. Extra required key 'recipe' supplied.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

I am using 9.4.0-preview.1.25207.5 packages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions