Skip to content

.NET: [Bug]: Tool Call Error in AGUI Mapping (WorkflowAsAgent with Session) 📌 Description #6511

@Mohanr1122

Description

@Mohanr1122

Description

When using AGUI mapping with WorkflowAsAgent and enabling session handling, the tool call fails with an error. This issue occurs during execution when the agent attempts to invoke a tool within the workflow context.

Code Sample

Client:
AGUIChatClient chatClient = new(httpClient, "https://localhost:7091");
ChatClientAgent baseAgent = chatClient.AsAIAgent(
    name: "AGUIAssistant",
    instructions: "You are a helpful assistant.");
AgentSession? session = await baseAgent.CreateSessionAsync(conversationId);
await foreach (AgentResponseUpdate update in baseAgent.RunStreamingAsync(chatMessage, session, cancellationToken: default)){ ...Logic }

Server:
var openAIOptions = new OpenAIClientOptions() { Endpoint = githubEndpoint };
var chatClient = new OpenAIClient(new ApiKeyCredential(token), openAIOptions)
    .GetChatClient(modelId).AsIChatClient();
[Description("Approve the expense report.")]
static string ApproveExpenseReport(string expenseReportId)
{
    return $"Expense report {expenseReportId} approved";
}
AITool[] tools = [AIFunctionFactory.Create(ApproveExpenseReport)];

ChatClientAgent baseAgent = chatClient.AsAIAgent(
    name: "AGServerAssistant",
    instructions: "You are a helpful assistant in charge of approving expenses",
    tools: tools);

ChatClientAgent claimsAgent = chatClient.AsAIAgent(
    name: "AGClaimsAssistant",
    instructions: "You are a helpful assistant in charge of approving claims and providing informarion",
    tools: claimstools);
ChatClientAgent triageAgent = chatClient.AsAIAgent(
    name: "AGUITriageAssistant",
    instructions: "You are a helpful assistant in charge of triaging expense reports and determining if they need approval or not. If an expense report needs approval, call the ApproveExpenseReport function with the expense report id. Otherwise, respond with 'No approval needed'.");

var workflowAgent = AgentWorkflowBuilder.CreateHandoffBuilderWith(triageAgent)
    .WithHandoff(triageAgent, baseAgent)
    .WithHandoff(triageAgent, claimsAgent)
    .WithHandoff(baseAgent, triageAgent)
    .WithHandoff(claimsAgent, triageAgent)
    .Build();

var mainAgent = workflowAgent.AsAIAgent(name: "ReportAgent", includeExceptionDetails: true,
    includeWorkflowOutputsInResponse: true)
    .AsBuilder()
    .Build();

app.MapAGUI("/", mainAgent);

Error Messages / Stack Traces

System.Text.Json.JsonException
  HResult=0x80131500
  Message='T' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.DeserializeAsObject(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpanAsObject(ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpanAsObject(ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize(String json, JsonTypeInfo jsonTypeInfo)
   at Microsoft.Agents.AI.AGUI.Shared.ChatResponseUpdateAGUIExtensions.DeserializeResultIfAvailable(ToolCallResultEvent toolCallResult, JsonSerializerOptions options)
   at Microsoft.Agents.AI.AGUI.Shared.ChatResponseUpdateAGUIExtensions.ToolCallBuilder.EmitToolCallResult(ToolCallResultEvent toolCallResult, JsonSerializerOptions options)
   at Microsoft.Agents.AI.AGUI.Shared.ChatResponseUpdateAGUIExtensions.<AsChatResponseUpdatesAsync>d__2.MoveNext()
   at Microsoft.Agents.AI.AGUI.Shared.ChatResponseUpdateAGUIExtensions.<AsChatResponseUpdatesAsync>d__2.MoveNext()
   at Microsoft.Agents.AI.AGUI.Shared.ChatResponseUpdateAGUIExtensions.<AsChatResponseUpdatesAsync>d__2.System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(Int16 token)
   at Microsoft.Agents.AI.AGUI.AGUIChatClient.AGUIChatClientHandler.<GetStreamingResponseAsync>d__9.MoveNext()
   at Microsoft.Agents.AI.AGUI.AGUIChatClient.AGUIChatClientHandler.<GetStreamingResponseAsync>d__9.MoveNext()
   at Microsoft.Agents.AI.AGUI.AGUIChatClient.AGUIChatClientHandler.<GetStreamingResponseAsync>d__9.System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(Int16 token)
   at Microsoft.Extensions.AI.FunctionInvokingChatClient.<GetStreamingResponseAsync>d__42.MoveNext()
   at Microsoft.Extensions.AI.FunctionInvokingChatClient.<GetStreamingResponseAsync>d__42.MoveNext()
   at Microsoft.Extensions.AI.FunctionInvokingChatClient.<GetStreamingResponseAsync>d__42.System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(Int16 token)
   at Microsoft.Agents.AI.AGUI.AGUIChatClient.<GetStreamingResponseAsync>d__4.MoveNext()
   at Microsoft.Agents.AI.AGUI.AGUIChatClient.<GetStreamingResponseAsync>d__4.MoveNext()
   at Microsoft.Agents.AI.AGUI.AGUIChatClient.<GetStreamingResponseAsync>d__4.System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(Int16 token)
   at Microsoft.Agents.AI.ChatClientAgent.<RunCoreStreamingAsync>d__30.MoveNext()
   at Microsoft.Agents.AI.ChatClientAgent.<RunCoreStreamingAsync>d__30.MoveNext()
   at Microsoft.Agents.AI.ChatClientAgent.<RunCoreStreamingAsync>d__30.MoveNext()
   at Microsoft.Agents.AI.ChatClientAgent.<RunCoreStreamingAsync>d__30.System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(Int16 token)
   at Microsoft.Agents.AI.AIAgent.<RunStreamingAsync>d__33.MoveNext()
   at Microsoft.Agents.AI.AIAgent.<RunStreamingAsync>d__33.MoveNext()
   at Microsoft.Agents.AI.AIAgent.<RunStreamingAsync>d__33.System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(Int16 token)
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\dhiya\source\repos\AgentWorkflowSession\AgentWorkflowSession\Program.cs:line 58
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\dhiya\source\repos\AgentWorkflowSession\AgentWorkflowSession\Program.cs:line 58
   at Program.<Main>(String[] args)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
JsonReaderException: 'T' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.

Package Versions

Microsoft.Agents.AI 1.10.0, Microsoft.Agents.AI.Workflows 1.10.0,

.NET Version

net10

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions