Repository navigation
Closed
Copy link
Labels
.NETUsage: [Issues, PRs], Target: .NetUsage: [Issues, PRs], Target: .NetbugUsage: [Issues], Target: all issues (Legacy, prefer issue type: bug)Usage: [Issues], Target: all issues (Legacy, prefer issue type: bug)
Description
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
See aboveError 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
Reacted by Marco Minerva, Marco Minerva, Totti, nanny07, Mike De Petris, Paolo Botti and Gianluca Gentile
Metadata
Metadata
Assignees
Labels
.NETUsage: [Issues, PRs], Target: .NetUsage: [Issues, PRs], Target: .NetbugUsage: [Issues], Target: all issues (Legacy, prefer issue type: bug)Usage: [Issues], Target: all issues (Legacy, prefer issue type: bug)
Type
Projects
- StatusShow more project fieldsDone
Activity
rwjdk commentedon Feb 20, 2026
If it is any help, I have a home-grown similar implementation that do work together with AIAgent as an extension method, so it is not because it is impossible to do:
https://github.com/rwjdk/AgentFrameworkToolkit/blob/main/src/AgentFrameworkToolkit/AIAgentExtensions.cs