diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows/MagenticWorkflowBuilder.cs b/dotnet/src/Microsoft.Agents.AI.Workflows/MagenticWorkflowBuilder.cs index 7c1e801ab1..ad620f6fc1 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows/MagenticWorkflowBuilder.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows/MagenticWorkflowBuilder.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; @@ -140,7 +141,15 @@ public class MagenticWorkflowBuilder(AIAgent managerAgent) } /// - public Workflow Build() => this.ReduceToWorkflowBuilder().Build(); + public Workflow Build() + { + if (this._team.Count == 0) + { + throw new InvalidOperationException("At least one participant must be added before building a Magentic workflow."); + } + + return this.ReduceToWorkflowBuilder().Build(); + } private TaskLimits Limits => new( MaxRoundCount: this._maxRounds, diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs index d8d08bfe7e..86cd7236b6 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs @@ -1165,6 +1165,24 @@ public class MagenticOrchestrationTests fourthResult.Result![0].Text.Should().Contain("Completed after multiple revisions"); } + [Fact] + public void Empty_Team_Build_Throws() + { + // Arrange: No participants added to the builder. + TestReplayAgent manager = new( + [CreatePlanResponse("Facts"), CreatePlanResponse("Plan")], + name: "Manager"); + + MagenticWorkflowBuilder builder = new MagenticWorkflowBuilder(manager) + // No .AddParticipants() — empty team + .RequirePlanSignoff(false); + + // Act & Assert: Build() should throw because the team is empty. + Action buildAction = () => builder.Build(); + buildAction.Should().Throw() + .WithMessage("*participant*"); + } + #region Helper Methods private sealed record WorkflowRunResult(