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; }
}
Code Sample
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
Description
If you use the new .RunAsync on
AIAgenttogether with FunctionCalling middleware, the output is not structured output JSONWorking Code
Non-working Code
Code Sample
Error Messages / Stack Traces
Package Versions
1.0.0-rc1
.NET Version
.NET10
Additional Context
No response