Skip to content

.NET: [Bug]: InvalidOperationException when returning a derived type as workflow output #4134

Closed

Description

Description

I have the following Console Application that defines a Workflow:

#!/usr/bin/env dotnet

#:sdk Microsoft.NET.Sdk

#:property OutputType=Exe
#:property TargetFramework=net10.0
#:property ImplicitUsings=enable
#:property Nullable=enable
#:property PublishAot=false

#:package Microsoft.Agents.AI.Workflows@1.0.0-rc1
#:package Microsoft.Agents.AI.Workflows.Generators@1.0.0-rc1

using Microsoft.Agents.AI.Workflows;

var sampleExecutor = new SampleExecutor();

var builder = new WorkflowBuilder(sampleExecutor)
    .WithOutputFrom(sampleExecutor);

var workflow = builder.Build();

await using var run = await InProcessExecution.RunStreamingAsync(workflow, "Hello World!");

await foreach (var evt in run.WatchStreamAsync())
{
    if (evt is ExecutorCompletedEvent executorCompleted)
    {
        Console.WriteLine($"{executorCompleted.ExecutorId}: {executorCompleted.Data}");
    }
    else if (evt is WorkflowOutputEvent workflowOutputEvent)
    {
        Console.WriteLine($"Final output for {workflowOutputEvent.ExecutorId}: {workflowOutputEvent.Data}");
    }
    else if (evt is WorkflowErrorEvent executorFailed)
    {
        Console.WriteLine($"Error: {executorFailed.Data}");
    }
}

internal partial class SampleExecutor() : Executor(nameof(SampleExecutor))
{
    [MessageHandler]
    private async ValueTask<BaseClass> HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Received message: {input}");

        await Task.Delay(100, cancellationToken);

        var result = new DerivedClass();
        return result;
    }
}

public class BaseClass
{
    public virtual string GetName() => "BaseClass";
}

public class DerivedClass : BaseClass
{
    public override string GetName() => "DerivedClass";
}

In particular, in the Executor I want to return an instance of DerivedClass, which inherits from BaseClass. The code compiles correctly, and I receive the ExecutorCompletedEvent as expected, but not the WorkflowOutputEvent. Instead, I get an exception:

Error: System.InvalidOperationException: Cannot output object of type DerivedClass. Expecting one of [BaseClass].

Error Messages / Stack Traces

Error: System.InvalidOperationException: Cannot output object of type DerivedClass. Expecting one of [BaseClass].
   at Microsoft.Agents.AI.Workflows.InProc.InProcessRunnerContext.YieldOutputAsync(String sourceId, Object output, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.Workflows.Executor.ExecuteCoreAsync(Object message, TypeId messageType, IWorkflowContext context, WorkflowTelemetryContext telemetryContext, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.Workflows.InProc.InProcessRunner.DeliverMessagesAsync(String receiverId, ConcurrentQueue`1 envelopes, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.Workflows.InProc.InProcessRunner.RunSuperstepAsync(StepContext currentStep, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.Workflows.InProc.InProcessRunner.Microsoft.Agents.AI.Workflows.Execution.ISuperStepRunner.RunSuperStepAsync(CancellationToken cancellationToken)

Package Versions

Microsoft.Agents.AI.Workflows@1.0.0-rc1, Microsoft.Agents.AI.Workflows.Generators@1.0.0-rc1

.NET Version

.NET 10.0.3

Activity

  1. added
    bug
    .NET
    on Feb 20, 2026
  2. added
    triage
    on Feb 20, 2026
  3. ecc-parity-check commented on Feb 23, 2026

    @ecc-parity-check

    Could you help me, how did you make the Executor to work?
    I'm still on RC1 getting the

    Image

    error.

  4. marcominerva commented on Feb 23, 2026

    @marcominerva
    ContributorAuthor

    ecc-parity-check you need to add the Microsoft.Agents.AI.Workflows.Generators package to your solution.

  5. ecc-parity-check commented on Feb 23, 2026

    @ecc-parity-check

    Ohhh, I wish this was mentioned on the docs page... :)
    Thanks Marco Minerva (@marcominerva) !

  6. added
    workflows
    and removed
    triage
    on Feb 23, 2026
  7. moved this from Planned to In Progress in Agent Frameworkon Mar 5, 2026
  8. moved this from In Progress to In Review in Agent Frameworkon Mar 5, 2026
  9. moved this from In Review to Done in Agent Frameworkon Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETbugworkflows

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions