Implementation:Microsoft Agent framework Edge Condition Function Pattern
| Property | Value |
|---|---|
| Implementation Name | Edge Condition Function Pattern |
| SDK | Microsoft Agent Framework |
| Repository | Microsoft Agent Framework |
| Type | Pattern Doc (user-defined interface) |
| Domains | Workflow_Engine, Control_Flow |
| Interface | Callable[[Any], bool | Awaitable[bool]]
|
Overview
The Edge Condition Function Pattern describes the user-defined callable interface used to control conditional edge traversal in Microsoft Agent Framework workflows. An edge condition function receives the message data produced by the source executor and returns a boolean (True to traverse the edge, False to skip it). Both synchronous and asynchronous condition functions are supported.
This is a Pattern Doc -- it documents a user-defined interface rather than a specific framework API. The framework defines the contract; users provide the implementation.
Interface Contract
Callable Signature
Callable[[Any], bool | Awaitable[bool]]
Synchronous Form
def condition(data: Any) -> bool:
...
Asynchronous Form
async def condition(data: Any) -> bool:
...
Parameter Details
| Parameter | Type | Description |
|---|---|---|
data |
Any |
The message data emitted by the source executor. In most workflow configurations this is an AgentExecutorResponse, but the type depends on the source executor's output contract. The condition function should be typed to match the expected source output.
|
Return Value
| Return Value | Effect |
|---|---|
True |
The edge is traversed and the target executor is scheduled for execution. |
False |
The edge is skipped; the target executor is not reached via this edge. |
Data Flow
The condition function sits between a source executor and a target executor in the workflow graph:
| Step | Component | Action |
|---|---|---|
| 1 | Source Executor | Completes execution and produces output data. |
| 2 | Workflow Engine | Passes the source output to each outgoing edge's condition function. |
| 3 | Condition Function | Inspects the data and returns True or False.
|
| 4 | Workflow Engine | Traverses edges where the condition returned True; skips edges where it returned False.
|
| 5 | Target Executor | Receives the source output and begins execution (only if the edge was traversed). |
Usage Examples
Simple Value Check
Inspect a boolean field on the executor's structured output to determine whether to proceed:
from agent_framework.workflows import AgentExecutorResponse
def is_approved(data: AgentExecutorResponse) -> bool:
return data.agent_response.value.approved == True
Pydantic Structured Output Check
When the source agent uses a Pydantic model as its structured output type, the condition function can inspect typed fields directly:
from pydantic import BaseModel
from agent_framework.workflows import AgentExecutorResponse
class Decision(BaseModel):
action: str
def route_to_action(data: AgentExecutorResponse) -> bool:
return data.agent_response.value.action == "process"
Async Condition with External Lookup
When the routing decision depends on external state (such as a database or API), use an asynchronous condition function:
async def check_database(data: str) -> bool:
result = await db.check(data)
return result.is_valid
Composing Multiple Conditions
Standard Python techniques can be used to combine conditions into more complex predicates:
from agent_framework.workflows import AgentExecutorResponse
def is_approved(data: AgentExecutorResponse) -> bool:
return data.agent_response.value.approved == True
def is_high_priority(data: AgentExecutorResponse) -> bool:
return data.agent_response.value.priority >= 5
def is_approved_and_high_priority(data: AgentExecutorResponse) -> bool:
return is_approved(data) and is_high_priority(data)
Implementation Guidelines
- Type the
dataparameter -- Use the specific type produced by the source executor (e.g.,AgentExecutorResponse) rather thanAnyto gain type-checking benefits. - Return explicit booleans -- Always return
TrueorFalse. Avoid relying on Python's truthy/falsy coercion, which can lead to subtle routing bugs. - Guard against missing attributes -- If the source executor's output structure may vary, use defensive access patterns (e.g.,
getattr,hasattr, or try/except) inside the condition function. - Minimize side effects -- Condition functions should ideally be pure predicates. If side effects are necessary (e.g., logging), ensure they do not alter the data or the workflow state.
- Keep async conditions lightweight -- Asynchronous conditions introduce I/O latency into the routing path. Use them only when external lookups are genuinely required for the routing decision.
Error Handling
| Scenario | Behavior |
|---|---|
| Condition raises an exception | The workflow engine propagates the exception. Users should handle errors within the condition function to avoid workflow-level failures. |
| Condition returns a non-boolean value | Behavior depends on the workflow engine's coercion rules. It is strongly recommended to return an explicit bool.
|
Related Pages
- Principle:Microsoft_Agent_framework_Edge_Condition_Pattern -- The principle-level documentation for the Edge Condition Pattern.