Skip to content

.NET: [Bug]: Agent Skill throws "System.InvalidOperationException" #7989

Description

@s-kip

Description

I'm exploring the examples of the Microsoft Agent Framework and have a question or maybe found a problem or issue. ;)

When I execute the example Agent_Step03_ClassBasedSkills using my local AI (Model qwen3.8-27b) with the OpenAI API, the Agent returns something like this:

Converting units with class-based skills
------------------------------------------------------------
Agent:

The conversion script isn't working, so I calculated these using the conversion table's formula (result = value × factor):

## Marathon: 26.2 miles ␦ kilometers
- **26.2 × 1.60934 = 42.16 km** (42.164708, rounded)

So a marathon is about **42.16 kilometers** - commonly rounded to 42.2 km.

## 75 kilograms ␦ pounds
- **75 × 2.20462 = 165.35 lbs** (165.3465, rounded)

So 75 kilograms is about **165.35 pounds**.

The important part is "The conversion script isn't working, so I calculated these using the conversion table's formula (result = value × factor)"

In the output windows I see a couple of exceptions like this:

Exception thrown: 'System.InvalidOperationException' in Microsoft.Agents.AI.dll
Inline skill scripts expect arguments as a JSON object but received a JSON element of kind 'String'.

Debugging further gives me the AgentInlineSkillScript and there I found

 private static AIFunctionArguments ConvertToFunctionArguments(JsonElement? arguments)
    {
        if (arguments is null ||
            arguments.Value.ValueKind == JsonValueKind.Null ||
            arguments.Value.ValueKind == JsonValueKind.Undefined)
        {
            return [];
        }

        if (arguments.Value.ValueKind != JsonValueKind.Object)
        {
            throw new InvalidOperationException(
                $"Inline skill scripts expect arguments as a JSON object but received a JSON element of kind '{arguments.Value.ValueKind}'.");
        }

        var dict = new Dictionary<string, object?>();
        foreach (var property in arguments.Value.EnumerateObject())
        {
            dict[property.Name] = property.Value;
        }

        return new AIFunctionArguments(dict);
    }

So, no matter what the LLM is using as parameter calling the skill script, as long as this is no JSON-Object, this function throws an exception.

So my question is:
how can I call the skill with the two double values as expected, so that the LLM can use the skill and does not run into a failure?

Code Sample

/// <summary>
    /// Converts a value by the given factor.
    /// </summary>
    [AgentSkillScript("convert")]
    [Description("Multiplies a value by a conversion factor and returns the result as JSON.")]
    private static string ConvertUnits(double value, double factor)
    {
        double result = Math.Round(value * factor, 4);
        return JsonSerializer.Serialize(new { value, factor, result });
    }

Error Messages / Stack Traces

Package Versions

Microsoft.Agents.AI: 1.20.0; Microsoft.Agents.AI.OpenAI: 1.20.0;

.NET Version

.NET 10

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETUsage: [Issues, PRs], Target: .NettriageUsage: [Issues], Target: All issues that still need to be triaged

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions