Skip to content

.NET: [Bug]: RC1's AIAgent.RunAsync<T> does not work with FunctionCallingMiddleware #4118

Description

@rwjdk

Description

If you use the new .RunAsync on AIAgent together with FunctionCalling middleware, the output is not structured output JSON

Working Code

AIAgent agent1 = new AzureOpenAIClient(
    new Uri(endpoint),
    new ApiKeyCredential(key))
    .GetChatClient(deploymentName)
    .AsAIAgent(instructions: "You are good at telling jokes.", name: "Joker");

AgentResponse<Joke> response = await agent1.RunAsync<Joke>("Tell a joke");
string joke1 = response.Result.TheJoke;

Non-working Code

AIAgent agent2 = new AzureOpenAIClient(
        new Uri(endpoint),
        new ApiKeyCredential(key))
    .GetChatClient(deploymentName)
    .AsAIAgent(instructions: "You are good at telling jokes.", name: "Joker")
    .AsBuilder()
    .Use(FunctionCallMiddleware) //Only Difference from working code
    .Build();

AgentResponse<Joke> response2 = await agent2.RunAsync<Joke>("Tell a joke");
//BUG: This Response is not JSON, but plain text
string joke2 = response2.Result.TheJoke;
// Objects and middleware for full sample
async ValueTask<object?> FunctionCallMiddleware(AIAgent callingAgent, FunctionInvocationContext context, Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> next, CancellationToken cancellationToken)
{
    StringBuilder functionCallDetails = new();
    functionCallDetails.Append($"- Tool Call: '{context.Function.Name}'");
    if (context.Arguments.Count > 0)
    {
        functionCallDetails.Append($" (Args: {string.Join(",", context.Arguments.Select(x => $"[{x.Key} = {x.Value}]"))}");
    }

    Console.WriteLine(functionCallDetails.ToString());

    return await next(context, cancellationToken);
}
public class Joke
{
    public required string TheJoke { get; set; }
}
Image

Code Sample

See above

Error Messages / Stack Traces

System.Text.Json.JsonException: ''W' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'

(or similar, but normal JSON Deserialization error)

Package Versions

1.0.0-rc1

.NET Version

.NET10

Additional Context

No response

Metadata

Metadata

Labels

.NETUsage: [Issues, PRs], Target: .NetbugUsage: [Issues], Target: all issues (Legacy, prefer issue type: bug)

Type

Fields

No fields configured for Bug.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions