-
Notifications
You must be signed in to change notification settings - Fork 1.3k
.NET: Added support for polymorphic type as workflow output #4485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peibekwe
wants to merge
4
commits into
main
Choose a base branch
from
peibekwe/workflow-output-bugfix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
14c9012
Added support for polymorphic type as workflow output
peibekwe 4e5558d
Merge branch 'main' into peibekwe/workflow-output-bugfix
peibekwe 5a4a5bb
Update Linq expression to avoid unnecessary allocations.
peibekwe cf9b7c6
Added caching as per PR comment
peibekwe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
276 changes: 276 additions & 0 deletions
276
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/PolymorphicOutputTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
|
|
||
| namespace Microsoft.Agents.AI.Workflows.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for polymorphic output type handling in workflows. | ||
| /// Verifies that executors can return derived types when the declared output type is a base class. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This addresses GitHub issue #4134: InvalidOperationException when returning derived type as workflow output. | ||
| /// </remarks> | ||
| public partial class PolymorphicOutputTests | ||
| { | ||
| #region Test Type Hierarchy | ||
|
|
||
| /// <summary> | ||
| /// Base class used as declared output type. | ||
| /// </summary> | ||
| public class BaseOutput | ||
| { | ||
| public virtual string Name => "BaseOutput"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Derived class returned at runtime. | ||
| /// </summary> | ||
| public class DerivedOutput : BaseOutput | ||
| { | ||
| public override string Name => "DerivedOutput"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Second-level derived class for testing multiple inheritance levels. | ||
| /// </summary> | ||
| public class GrandchildOutput : DerivedOutput | ||
| { | ||
| public override string Name => "GrandchildOutput"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Unrelated class that should NOT be accepted as output. | ||
| /// </summary> | ||
| public class UnrelatedOutput | ||
| { | ||
| public string Name => "UnrelatedOutput"; | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Test Executors | ||
|
|
||
| /// <summary> | ||
| /// Executor that declares BaseOutput as yield type but returns DerivedOutput. | ||
| /// </summary> | ||
| internal sealed class DerivedOutputExecutor() : Executor(nameof(DerivedOutputExecutor)) | ||
| { | ||
| protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder) | ||
| { | ||
| return protocolBuilder.ConfigureRoutes(routeBuilder => | ||
| routeBuilder.AddHandler<string, BaseOutput>(this.HandleAsync)); | ||
| } | ||
|
|
||
| private async ValueTask<BaseOutput> HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| await Task.Delay(10, cancellationToken); | ||
|
|
||
| // Arrange: Return a derived type where the method signature declares the base type | ||
| return new DerivedOutput(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Executor that declares BaseOutput as yield type but returns GrandchildOutput (two levels deep). | ||
| /// </summary> | ||
| internal sealed class GrandchildOutputExecutor() : Executor(nameof(GrandchildOutputExecutor)) | ||
| { | ||
| protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder) | ||
| { | ||
| return protocolBuilder.ConfigureRoutes(routeBuilder => | ||
| routeBuilder.AddHandler<string, BaseOutput>(this.HandleAsync)); | ||
| } | ||
|
|
||
| private async ValueTask<BaseOutput> HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| await Task.Delay(10, cancellationToken); | ||
|
|
||
| // Arrange: Return a grandchild type (two inheritance levels) | ||
| return new GrandchildOutput(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Executor that attempts to return an unrelated type - should fail validation. | ||
| /// This executor intentionally bypasses type safety to test runtime validation. | ||
| /// </summary> | ||
| internal sealed class UnrelatedOutputExecutor() : Executor(nameof(UnrelatedOutputExecutor)) | ||
| { | ||
| protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder) | ||
| { | ||
| return protocolBuilder.ConfigureRoutes(routeBuilder => | ||
| routeBuilder.AddHandler<string, BaseOutput>(this.HandleAsync)); | ||
| } | ||
|
|
||
| private async ValueTask<BaseOutput> HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| // Arrange: Attempt to yield an unrelated type - should throw | ||
| UnrelatedOutput unrelated = new(); | ||
| await context.YieldOutputAsync(unrelated, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| // This line should not be reached | ||
| return new BaseOutput(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Executor that returns the exact declared type (baseline test). | ||
| /// </summary> | ||
| internal sealed class ExactTypeExecutor() : Executor(nameof(ExactTypeExecutor)) | ||
| { | ||
| protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder) | ||
| { | ||
| return protocolBuilder.ConfigureRoutes(routeBuilder => | ||
| routeBuilder.AddHandler<string, BaseOutput>(this.HandleAsync)); | ||
| } | ||
|
|
||
| private ValueTask<BaseOutput> HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken) | ||
| { | ||
| BaseOutput result = new(); | ||
| return new ValueTask<BaseOutput>(result); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Tests | ||
|
|
||
| /// <summary> | ||
| /// Verifies that returning a derived type when the declared output type is a base class succeeds. | ||
| /// This is the main regression test for GitHub issue #4134. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task ReturningDerivedType_WhenBaseTypeIsDeclared_ShouldSucceedAsync() | ||
| { | ||
| // Arrange | ||
| DerivedOutputExecutor executor = new(); | ||
| WorkflowBuilder builder = new WorkflowBuilder(executor).WithOutputFrom(executor); | ||
| Workflow workflow = builder.Build(); | ||
|
|
||
| // Act | ||
| List<WorkflowEvent> events = []; | ||
| await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, "test input"); | ||
| await foreach (WorkflowEvent evt in run.WatchStreamAsync()) | ||
| { | ||
| events.Add(evt); | ||
| } | ||
|
|
||
| // Assert | ||
| events.Should().NotBeEmpty("workflow should produce events"); | ||
|
|
||
| List<WorkflowOutputEvent> outputEvents = events.OfType<WorkflowOutputEvent>().ToList(); | ||
| outputEvents.Should().ContainSingle("workflow should produce exactly one output event"); | ||
|
|
||
| WorkflowOutputEvent outputEvent = outputEvents.Single(); | ||
| outputEvent.Data.Should().BeOfType<DerivedOutput>("output should be the derived type"); | ||
| ((DerivedOutput)outputEvent.Data!).Name.Should().Be("DerivedOutput"); | ||
|
|
||
| // Verify no error events | ||
| List<WorkflowErrorEvent> errorEvents = events.OfType<WorkflowErrorEvent>().ToList(); | ||
| errorEvents.Should().BeEmpty("workflow should not produce error events"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that returning a grandchild type (multiple inheritance levels) succeeds. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task ReturningGrandchildType_WhenBaseTypeIsDeclared_ShouldSucceedAsync() | ||
| { | ||
| // Arrange | ||
| GrandchildOutputExecutor executor = new(); | ||
| WorkflowBuilder builder = new WorkflowBuilder(executor).WithOutputFrom(executor); | ||
| Workflow workflow = builder.Build(); | ||
|
|
||
| // Act | ||
| List<WorkflowEvent> events = []; | ||
| await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, "test input"); | ||
| await foreach (WorkflowEvent evt in run.WatchStreamAsync()) | ||
| { | ||
| events.Add(evt); | ||
| } | ||
|
|
||
| // Assert | ||
| events.Should().NotBeEmpty("workflow should produce events"); | ||
|
|
||
| List<WorkflowOutputEvent> outputEvents = events.OfType<WorkflowOutputEvent>().ToList(); | ||
| outputEvents.Should().ContainSingle("workflow should produce exactly one output event"); | ||
|
|
||
| WorkflowOutputEvent outputEvent = outputEvents.Single(); | ||
| outputEvent.Data.Should().BeOfType<GrandchildOutput>("output should be the grandchild type"); | ||
| ((GrandchildOutput)outputEvent.Data!).Name.Should().Be("GrandchildOutput"); | ||
|
|
||
| // Verify no error events | ||
| List<WorkflowErrorEvent> errorEvents = events.OfType<WorkflowErrorEvent>().ToList(); | ||
| errorEvents.Should().BeEmpty("workflow should not produce error events"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that returning an unrelated type still throws InvalidOperationException. | ||
| /// This ensures the fix doesn't break the existing validation for truly incompatible types. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task ReturningUnrelatedType_WhenBaseTypeIsDeclared_ShouldFailAsync() | ||
| { | ||
| // Arrange | ||
| UnrelatedOutputExecutor executor = new(); | ||
| WorkflowBuilder builder = new WorkflowBuilder(executor).WithOutputFrom(executor); | ||
| Workflow workflow = builder.Build(); | ||
|
|
||
| // Act | ||
| List<WorkflowEvent> events = []; | ||
| await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, "test input"); | ||
| await foreach (WorkflowEvent evt in run.WatchStreamAsync()) | ||
| { | ||
| events.Add(evt); | ||
| } | ||
|
|
||
| // Assert: Should have an error event with InvalidOperationException message | ||
| List<WorkflowErrorEvent> errorEvents = events.OfType<WorkflowErrorEvent>().ToList(); | ||
| errorEvents.Should().ContainSingle("workflow should produce exactly one error event"); | ||
|
|
||
| WorkflowErrorEvent errorEvent = errorEvents.Single(); | ||
| string errorMessage = errorEvent.Data?.ToString() ?? string.Empty; | ||
| errorMessage.Should().Contain("Cannot output object of type UnrelatedOutput"); | ||
| errorMessage.Should().Contain("BaseOutput"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that returning the exact declared type still works (baseline test). | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task ReturningExactType_WhenSameTypeIsDeclared_ShouldSucceedAsync() | ||
| { | ||
| // Arrange: Create an executor that returns the exact declared type | ||
| ExactTypeExecutor executor = new(); | ||
| WorkflowBuilder builder = new WorkflowBuilder(executor).WithOutputFrom(executor); | ||
| Workflow workflow = builder.Build(); | ||
|
|
||
| // Act | ||
| List<WorkflowEvent> events = []; | ||
| await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, "test input"); | ||
| await foreach (WorkflowEvent evt in run.WatchStreamAsync()) | ||
| { | ||
| events.Add(evt); | ||
| } | ||
|
|
||
| // Assert | ||
| events.Should().NotBeEmpty("workflow should produce events"); | ||
|
|
||
| List<WorkflowOutputEvent> outputEvents = events.OfType<WorkflowOutputEvent>().ToList(); | ||
| outputEvents.Should().ContainSingle("workflow should produce exactly one output event"); | ||
|
|
||
| WorkflowOutputEvent outputEvent = outputEvents.Single(); | ||
| outputEvent.Data.Should().BeOfType<BaseOutput>("output should be the exact base type"); | ||
|
|
||
| // Verify no error events | ||
| List<WorkflowErrorEvent> errorEvents = events.OfType<WorkflowErrorEvent>().ToList(); | ||
| errorEvents.Should().BeEmpty("workflow should not produce error events"); | ||
| } | ||
|
|
||
| #endregion | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.