Skip to content

.NET: Add error handling to workflow samples with agents#3996

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-agent-error-checking
Draft

.NET: Add error handling to workflow samples with agents#3996
Copilot wants to merge 2 commits intomainfrom
copilot/fix-agent-error-checking

Conversation

Copy link
Contributor

Copilot AI commented Feb 17, 2026

Motivation and Context

Workflow samples with agents failed silently when executors encountered errors. No error messages were displayed, no exceptions were thrown, making failures invisible to developers.

Description

Added error event handling to 13 workflow samples using agents. Two patterns applied:

For workflows using InProcessExecution.StreamAsync() + WatchStreamAsync():

await foreach (WorkflowEvent evt in run.WatchStreamAsync())
{
    switch (evt)
    {
        case ExecutorFailedEvent failureEvent:
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"Executor failed [{failureEvent.ExecutorId}]: {failureEvent.Data?.Message ?? "Unknown error"}");
            Console.ResetColor();
            break;

        case WorkflowErrorEvent errorEvent:
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"Workflow error: {errorEvent.Exception?.Message ?? "Unknown error"}");
            Console.ResetColor();
            throw errorEvent.Exception ?? new InvalidOperationException("Workflow encountered an error.");
    }
}

For workflows as agents using RunStreamingAsync():

try
{
    await foreach (var update in workflowAgent.RunStreamingAsync(input))
    {
        // ... process updates
    }
}
catch (Exception ex)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine($"\nWorkflow error: {ex.Message}");
    Console.ResetColor();
    throw;
}

Updated samples:

  • Foundational: 03_AgentsInWorkflows, 04_AgentWorkflowPatterns, 05_MultiModelService, 07_MixedWorkflowAgentsAndExecutors, 08_WriterCriticWorkflow
  • Agents: CustomAgentExecutors, FoundryAgent, GroupChatToolApproval, WorkflowAsAnAgent
  • ConditionalEdges: 01_EdgeCondition, 02_SwitchCase, 03_MultiSelection
  • Concurrent: Concurrent

Errors now display in red and cause proper exit codes instead of silent failures.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.
Original prompt

This section details on the original issue you should resolve

<issue_title>.NET: [Bug]: Workflow Foundational Sample 03_AgentsInWorkflow will fail silently if the agent fails</issue_title>
<issue_description>Errors are not being properly checked in the AgentsInWorkflow sample (and likely other samples).</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix error checking in AgentsInWorkflow sample .NET: Add error handling to workflow samples with agents Feb 17, 2026
Copilot AI requested a review from lokitoth February 17, 2026 15:44
Copilot AI and others added 2 commits February 17, 2026 10:50
Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: Workflow Foundational Sample 03_AgentsInWorkflow will fail silently if the agent fails

3 participants

Comments