Skip to content

.NET: Declarative workflows - Gracefully handle agent scenarios when no response is returned#5376

Merged
peibekwe merged 2 commits intomainfrom
peibekwe/invoke-agent-executor-fix
Apr 21, 2026
Merged

.NET: Declarative workflows - Gracefully handle agent scenarios when no response is returned#5376
peibekwe merged 2 commits intomainfrom
peibekwe/invoke-agent-executor-fix

Conversation

@peibekwe
Copy link
Copy Markdown
Contributor

Motivation and Context

Declarative workflow runs that include InvokeAzureAgent actions could crash with:

 Unhandled workflow failure - #<action_id> (InvokeAzureAgent)
 → System.InvalidOperationException: Sequence contains no elements

The crash is deterministic whenever an invoked agent returns an AgentResponse with zero Messages (e.g., a tool-only
turn where the model emits remote_function_call / mcp_call items without a final assistant text message,
content-filtered responses, or streams that close before the terminal assistant message). When that happens, the
executor's unguarded .Last on the empty collection throws, DeclarativeActionExecutor wraps it as Unhandled
workflow failure - #{id} ({ModelType}), and the entire run terminates mid-pipeline — subsequent actions never
execute, so downstream side effects never happen.

Fixes #5375

Description

Replace unguarded .Last / .Single on response-message collections with defensive access that degrades gracefully.

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.

@peibekwe peibekwe self-assigned this Apr 21, 2026
Copilot AI review requested due to automatic review settings April 21, 2026 00:36
@moonbox3 moonbox3 added .NET workflows Related to Workflows in agent-framework labels Apr 21, 2026
@github-actions github-actions Bot changed the title Declarative workflows - Gracefully handle agent scenarios when no response is returned .NET: Declarative workflows - Gracefully handle agent scenarios when no response is returned Apr 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves resilience of declarative workflow executors when an invoked agent returns an AgentResponse/ExternalInputResponse with zero Messages, avoiding deterministic crashes caused by unguarded .Last() / .Single() calls.

Changes:

  • Replace .Last() usages with LastOrDefault() + null-guards across multiple executors to avoid “Sequence contains no elements”.
  • Skip JSON response-object assignment when there is no final message text to parse.
  • Add unit tests covering empty-message responses for RequestExternalInputExecutor.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/RequestExternalInputExecutor.cs Avoids .Last() crash when external input contains no messages.
dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/QuestionExecutor.cs Adds defensive handling around selecting the “input” message from an extracted response.
dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/InvokeAzureAgentExecutor.cs Avoids .Last() crash on resume and guards JSON parsing when last message is missing/empty.
dotnet/src/Microsoft.Agents.AI.Workflows.Declarative.Foundry/AzureAgentProvider.cs Replaces .Single() with explicit cardinality check + clearer exception messages.
dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/ObjectModel/RequestExternalInputExecutorTest.cs Adds coverage for empty-message responses (with and without workflow conversation).

@peibekwe peibekwe marked this pull request as ready for review April 21, 2026 01:32
@peibekwe peibekwe requested review from alliscode and lokitoth April 21, 2026 01:33
@peibekwe peibekwe added this pull request to the merge queue Apr 21, 2026
Merged via the queue into main with commit adcd2d3 Apr 21, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET workflows Related to Workflows in agent-framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: Declarative Workflows - Gracefully handle agent scenarios where no response is returned

5 participants