Skip to content

.NET & Python: Conditional & Switch-Case Edges Evaluation During Streaming #1805

@joslat

Description

@joslat

Simple Description

Conditional & Switch-Case Edges fire on every streaming chunk, breaking agent workflow routing.
This bug blocks the fundamental promise of agent framework workflows: reliable, condition-based agent orchestration with both simple conditional logic AND complex switch-case routing patterns. It needs immediate architectural attention across both platforms for both edge types.

Scope

Agent-driven workflows only, which stream responses. When combining them with conditional or switch-case edges the issue happens.

Impact

This bug makes it IMPOSSIBLE to implement reliable agent workflows with conditional routing. Both conditional edges and switch-case edges evaluate their conditions on every streaming content chunk instead of once when agents complete, breaking the fundamental promise of workflow routing logic.

### What's Broken
Currently, when using streaming agents with conditional or switch-case edges:
❌ Conditional edges: Conditions evaluate dozens of times per agent (once per streaming chunk)
❌ Switch-case edges: Case predicates fire repeatedly during streaming, causing incorrect routing
❌ Workflow routing becomes unpredictable and unreliable
❌ Debugging workflows is nearly impossible due to condition evaluation spam
❌ Pure agent-workflows with routing logic are fundamentally broken

### What Should Happen
✅ Conditional and switch-case predicates should evaluate exactly once when the agent completes
✅ Streaming chunks should pass through without triggering edge evaluations
✅ Workflow routing should be deterministic and predictable
✅ Enable reliable agent-workflow architectures with complex routing flows

Technical Analysis 🔍

Both Platforms & Both Edge Types Affected
This is a cross-platform architectural flaw affecting both implementations identically:

Python (/agent_framework/_workflows/_edge_runner.py):

# 🐛 BUG in SingleEdgeRunner (conditional edges)
if edge.should_route(message.data):  # Called 10-100+ times per agent!
    await edge.target.send_message(message)

# 🐛 BUG in SwitchCaseEdgeRunner (switch-case edges)  
for edge in self.edges:
    if edge.should_route(message.data):  # Each case evaluated on every chunk!
        await edge.target.send_message(message)
        break

.NET (/Microsoft.Agents.AI.Workflows/Execution/DirectEdgeRunner.cs):

// 🐛 BUG: Both conditional and switch-case edges use same flawed pattern
if (this.EdgeData.Condition(message))  // Called on every streaming chunk!
{
    await this.EdgeData.Target.ExecuteAsync(message, context, cancellationToken);
}

Message Flow Analysis

Current (Broken) Behavior:

Agent starts → Chunk 1 → 🔥 CONDITIONAL FIRES + 🔥 SWITCH CASES EVALUATE
            → Chunk 2 → 🔥 CONDITIONAL FIRES + 🔥 SWITCH CASES EVALUATE  
            → Chunk 3 → 🔥 CONDITIONAL FIRES + 🔥 SWITCH CASES EVALUATE
            → ...      → 🔥 CONDITIONAL FIRES + 🔥 SWITCH CASES EVALUATE
            → Complete → 🔥 CONDITIONAL FIRES + 🔥 SWITCH CASES EVALUATE (again!)

Expected (Fixed) Behavior:

Agent starts → Chunk 1 → (passed through)
            → Chunk 2 → (passed through)  
            → Chunk 3 → (passed through)
            → ...      → (passed through)
            → Complete → ✅ CONDITIONAL/SWITCH EVALUATES ONCE

## Architecture Impact: Pure Agent-Workflows Are Impossible 🚫

  1. This bug prevents implementing pure agent-workflow architectures because:
  2. Conditional routing is unreliable - conditions may be true for some chunks, false for others
  3. Switch-case routing fails - cases fire multiple times with different intermediate states
  4. Agent orchestration fails - you can't reliably route between agents based on their outputs
  5. Complex routing patterns impossible - multi-path workflows become unpredictable
  6. Debugging is impossible - evaluation spam makes workflows untrackable

## Priority: CRITICAL 🎯
This is architecture-breaking and must be fixed to enable reliable agent workflows.

Without this fix:

  • Agent orchestration patterns are impossible
  • Both conditional AND switch-case routing are unreliable
  • The entire streaming + routing paradigm is broken
  • Complex multi-agent workflows cannot be implemented

📋 Acceptance Criteria

  • Conditional edges evaluate exactly once per agent execution
  • Switch-case edges evaluate exactly once per agent execution
  • Streaming chunks pass through without any condition evaluation
  • Python and .NET behave identically for both edge types
  • Existing non-streaming workflows remain unaffected
  • Agent workflow routing becomes deterministic and reliable
  • Complex routing patterns (nested conditionals, multi-case switches) work reliably

Metadata

Metadata

Assignees

Labels

.NETpythonv1.0Features being tracked for the version 1.0 GAworkflowsRelated to Workflows in agent-framework

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions