How to Handle a Failed Step in a Microsoft Agent Framework Workflow in C#
When a step in a Microsoft Agent Framework workflow throws an exception, InProcessExecution.RunAsync doesn't throw it to your code: the run reports the failure as events and ends. This guide runs a two-step workflow with a number and with text, and reads the failure from the events and from the run's status.
Syntax
await using Run <run> = await InProcessExecution.RunAsync(<workflow>, <input>);
foreach (WorkflowEvent <workflowEvent> in <run>.OutgoingEvents)
{
switch (<workflowEvent>)
{
case ExecutorFailedEvent <failed>:
// <failed>.ExecutorId is the step that failed, <failed>.Data its exception
break;
case WorkflowErrorEvent <error>:
// <error>.Exception is the error the workflow reports
break;
}
}
RunStatus <status> = await <run>.GetStatusAsync();
| Part | What it is |
|---|---|
<workflow>, <input> | The workflow to run, and the value its first step receives |
OutgoingEvents | Every event the run emitted |
ExecutorFailedEvent | Raised when a step's handler fails. ExecutorId names the step, and Data holds the exception |
WorkflowErrorEvent | Raised when the workflow encounters an error. Exception holds it, if there is one |
GetStatusAsync() | Returns the run's status, a RunStatus value |
Microsoft's guide to workflow events describes the two failure events in four words each: ExecutorFailedEvent is Executor encounters an error, and WorkflowErrorEvent is Workflow encounters an error. Agent Framework 1.28's documentation adds that an ExecutorFailedEvent's exception may be null if no exception was thrown.
The status says how the run stopped. These two values appear in this guide:
| Status | Meaning, from Agent Framework 1.28's documentation |
|---|---|
Idle | The run has halted and has no outstanding requests |
Ended | No further events will be emitted, and no messages can be sent to it. |
The others are PendingRequests, a run halted with at least one outstanding request, Running, and NotStarted, which only happens in lockstep mode.
The Example
A console app called NumberDoubler has a workflow with two steps: ParseNumber turns text into a number with int.Parse, and DoubleNumber doubles it. The app runs the workflow twice, with "21" and with "abc", which isn't a number, and prints the output, any failure and the run's status each time. There's no AI model in it, so nothing needs an Azure account.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0; this guide used 10.0.404.
Step 1: Create a Console App
In a terminal, in the folder where you keep your projects, create a console app, move into it and add Agent Framework's workflows package:
dotnet new console -o NumberDoubler
cd NumberDoubler
dotnet add package Microsoft.Agents.AI.Workflows --version 1.28.0
dotnet new console -o NumberDoublercreates a console app in a newNumberDoublerfolder, andcdmoves into it.dotnet add packageadds Microsoft.Agents.AI.Workflows, Agent Framework's workflows package.--versionkeeps you on the version this guide used.
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/NumberDoubler/NumberDoubler.csproj:
Restore succeeded.
…
info : PackageReference for package 'Microsoft.Agents.AI.Workflows' version '1.28.0' added to file '/Users/you/source/NumberDoubler/NumberDoubler.csproj'.
info : Generating MSBuild file /Users/you/source/NumberDoubler/obj/NumberDoubler.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /Users/you/source/NumberDoubler/obj/project.assets.json
log : Restored /Users/you/source/NumberDoubler/NumberDoubler.csproj (in 1.46 sec).
This workflow's steps are plain functions, so the project needs only this package: the source generator package is for class-based executors.
Step 2: Build a Two-Step Workflow
Replace everything in Program.cs with:
using Microsoft.Agents.AI.Workflows;
Func<string, int> parse = text => int.Parse(text);
Func<int, int> twice = number => number * 2;
ExecutorBinding parseNumber = parse.BindAsExecutor("ParseNumber");
ExecutorBinding doubleNumber = twice.BindAsExecutor("DoubleNumber");
Workflow workflow = new WorkflowBuilder(parseNumber)
.AddEdge(parseNumber, doubleNumber)
.WithOutputFrom(doubleNumber)
.Build();
parseturns text into a number withint.Parse, which throws for text that isn't a number.twicedoubles a number.BindAsExecutor("ParseNumber")turns a function into an executor, a step of the workflow, with the id in quotes.AddEdge(parseNumber, doubleNumber)sends whatParseNumberreturns toDoubleNumber, andWithOutputFrom(doubleNumber)makes the result ofDoubleNumberthe workflow's output.
Step 3: Run the Workflow and Read the Failure Events
Add this below the workflow:
foreach (string input in new[] { "21", "abc" })
{
Console.WriteLine($"Input: {input}");
// RunAsync doesn't throw when a step fails: the run reports it as events instead.
await using Run run = await InProcessExecution.RunAsync(workflow, input);
foreach (WorkflowEvent workflowEvent in run.OutgoingEvents)
{
switch (workflowEvent)
{
case WorkflowOutputEvent output:
Console.WriteLine($" Output: {output.Data}");
break;
case ExecutorFailedEvent failed:
Console.WriteLine($" {failed.ExecutorId} failed: {failed.Data?.GetType().Name}: {failed.Data?.Message}");
break;
case WorkflowErrorEvent error:
Console.WriteLine($" The run failed: {error.Exception?.GetType().Name}: {error.Exception?.Message}");
break;
}
}
Console.WriteLine($" Status: {await run.GetStatusAsync()}");
}
- The outer loop runs the workflow once for each input.
InProcessExecution.RunAsyncruns the workflow and hands back aRun. Its documentation: The workflow will run until its first halt, and the returned Run will capture all outgoing events. There's notry/catcharound it, for the reason in the comment.OutgoingEventsholds every event the run emitted, and theswitchpicks out three kinds. AWorkflowOutputEventcarries the output inData. AnExecutorFailedEventcarries the failed step's id inExecutorIdand its exception inData, and?.guards against that exception beingnull. AWorkflowErrorEventcarries the workflow's error inException.GetStatusAsync()reads the run's status after the events.
Step 4: Run the App
dotnet run
Input: 21
Output: 42
Status: Idle
Input: abc
ParseNumber failed: FormatException: The input string 'abc' was not in a correct format.
The run failed: TargetInvocationException: Error invoking handler for System.String
Status: Ended

Line by line:
21went through both steps: the output is42, and the status isIdle, a run that halted with no outstanding requests.abcmadeParseNumberfail. TheExecutorFailedEventnames the step and carries its own exception: aFormatExceptionwith the message fromint.Parse.- The
WorkflowErrorEventcame next, with aTargetInvocationException. Its message,Error invoking handler for System.String, names the type of message the failed handler takes, astring, not the step. - There's no
Outputline, and the status isEnded: this run emits nothing more and takes no more messages.
The program also ran to its end without an unhandled exception. RunAsync returned normally for abc too, and the loop went on to print the status.
What Each Event Tells You
| What you need | Where it is | In the example |
|---|---|---|
| Which step failed | ExecutorFailedEvent.ExecutorId | ParseNumber |
| Why it failed | ExecutorFailedEvent.Data | FormatException: The input string 'abc' was not in a correct format. |
| That the run failed | A WorkflowErrorEvent | TargetInvocationException: Error invoking handler for System.String |
| Whether the run can go on | GetStatusAsync() | Ended |
| The result, when there is one | WorkflowOutputEvent.Data | 42 |
Log the ExecutorFailedEvent: it's the one with the step and the real exception. The WorkflowErrorEvent tells you the run failed, but its message only names a type.
The Complete File
using Microsoft.Agents.AI.Workflows;
Func<string, int> parse = text => int.Parse(text);
Func<int, int> twice = number => number * 2;
ExecutorBinding parseNumber = parse.BindAsExecutor("ParseNumber");
ExecutorBinding doubleNumber = twice.BindAsExecutor("DoubleNumber");
Workflow workflow = new WorkflowBuilder(parseNumber)
.AddEdge(parseNumber, doubleNumber)
.WithOutputFrom(doubleNumber)
.Build();
foreach (string input in new[] { "21", "abc" })
{
Console.WriteLine($"Input: {input}");
// RunAsync doesn't throw when a step fails: the run reports it as events instead.
await using Run run = await InProcessExecution.RunAsync(workflow, input);
foreach (WorkflowEvent workflowEvent in run.OutgoingEvents)
{
switch (workflowEvent)
{
case WorkflowOutputEvent output:
Console.WriteLine($" Output: {output.Data}");
break;
case ExecutorFailedEvent failed:
Console.WriteLine($" {failed.ExecutorId} failed: {failed.Data?.GetType().Name}: {failed.Data?.Message}");
break;
case WorkflowErrorEvent error:
Console.WriteLine($" The run failed: {error.Exception?.GetType().Name}: {error.Exception?.Message}");
break;
}
}
Console.WriteLine($" Status: {await run.GetStatusAsync()}");
}

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Wrapping RunAsync in try/catch to catch a failed step | Nothing is caught: the step's exception arrives as events | Look for ExecutorFailedEvent and WorkflowErrorEvent in the run's events |
Looking only for WorkflowOutputEvent | A failed run looks like a run that produced nothing | Handle the two failure events too |
Logging only the WorkflowErrorEvent | The log says Error invoking handler for System.String, without the step or the real exception | Log the ExecutorFailedEvent's ExecutorId and Data |
Reading Data.Message without ?. | The documentation says Data may be null | Use ?., as the example does |
FAQ
Can starting a run still throw an exception?
Yes, for problems with the run itself rather than with one of its steps. For example, starting a second run on a workflow that another run still owns throws InvalidOperationException, as this fixes post shows. Keep a try/catch for those, and read the events for failed steps.
Can I see every event, not only the failures?
Yes. Print each event in the loop, whatever its type. The guide to seeing every event in a workflow run does that, after the run and while it runs.
Conclusion
A failed step doesn't make InProcessExecution.RunAsync throw. The run reports an ExecutorFailedEvent, with the step's id and its own exception, then a WorkflowErrorEvent, and its status becomes Ended. In the example, 21 gave 42 with the status Idle, and abc gave ParseNumber failed: FormatException with the status Ended. Read the events after every run, and log the step's own exception.
Additional Resources
- Events — the built-in workflow events, including the two failure events.
- ExecutorFailedEvent — the event's
ExecutorIdandDataproperties in the API reference. - Workflow Execution Modes — how
RunAsyncruns a workflow and collects its events.
