Python: Add workflow composition via add_workflow() API#3296
Python: Add workflow composition via add_workflow() API#3296moonbox3 wants to merge 3 commits intomicrosoft:mainfrom
add_workflow() API#3296Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements workflow composition via a new add_workflow() API that enables combining high-level orchestration patterns (ConcurrentBuilder, SequentialBuilder) with simple logical IDs. This addresses issue #1795 by allowing users to extend and compose high-level builders without writing custom orchestrator executors.
Changes:
- Added
add_workflow()method to WorkflowBuilder for composing orchestration patterns - Implemented logical ID resolution in edge methods (
add_edge(),set_start_executor()) to automatically wire entry/exit points - Enhanced type validation to support terminal executors using
yield_output()in composed workflows
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| python/packages/core/agent_framework/_workflows/_workflow_builder.py | Implements core add_workflow() method with logical ID resolution, prefixing logic, and workflow mapping tracking |
| python/packages/core/agent_framework/_workflows/_concurrent.py | Adds _to_builder() method to extract internal WorkflowBuilder structure for composition |
| python/packages/core/agent_framework/_workflows/_sequential.py | Adds _to_builder() method to extract internal WorkflowBuilder structure for composition |
| python/packages/core/agent_framework/_workflows/_executor.py | Adds _clone_with_id() method for creating executor copies with prefixed IDs during composition |
| python/packages/core/agent_framework/_workflows/_edge.py | Adds _with_prefix() method to all edge group types for ID prefixing during composition |
| python/packages/core/agent_framework/_workflows/_validation.py | Enhances type validation to fall back to workflow_output_types when connecting terminal executors |
| python/packages/core/tests/workflow/test_workflow_composition.py | Comprehensive unit tests covering composition functionality, ID resolution, and edge cases |
| python/samples/getting_started/workflows/composition/workflow_composition.py | Demonstrates API usage with two examples: simple composition and pre/post-processing |
| docs/decisions/00XX-workflow-composability.md | Detailed design document explaining motivation, API design, alternatives considered, and implementation phases |
python/samples/getting_started/workflows/composition/workflow_composition.py
Show resolved
Hide resolved
python/samples/getting_started/workflows/composition/workflow_composition.py
Show resolved
Hide resolved
There was a problem hiding this comment.
nit: Don't forget to update document number instead of 00XX.
|
|
||
| --- | ||
|
|
||
| ## Implementation Phases |
There was a problem hiding this comment.
nit: I think this section is unnecessary, but feel free to decide.
| registration: _EdgeRegistration | ||
| | _FanOutEdgeRegistration | ||
| | _SwitchCaseEdgeGroupRegistration | ||
| | _MultiSelectionEdgeGroupRegistration | ||
| | _FanInEdgeRegistration, | ||
| prefix: str, | ||
| ) -> ( | ||
| _EdgeRegistration | ||
| | _FanOutEdgeRegistration | ||
| | _SwitchCaseEdgeGroupRegistration | ||
| | _MultiSelectionEdgeGroupRegistration | ||
| | _FanInEdgeRegistration |
There was a problem hiding this comment.
nit: Maybe worth type alias.
|
We're going to support workflow composition via subworkflows right now. |
Motivation and Context
WorkflowBuilder.add_workflow()for composing high-level orchestration patterns (ConcurrentBuilder, SequentialBuilder) with simple logical IDs_to_builder()to ConcurrentBuilder and SequentialBuilder (and others) for extracting internal graph structureadd_edge()andset_start_executor()(e.g.,add_edge("analysis", "summary")automatically wires exit to entry points)workflow_output_typeswhenoutput_typesis empty, supporting composed workflows with terminal executorsExample
Design decisions located in attached design doc.
Note on other builders:
_to_builder()has not been added to GroupChatBuilder, HandoffBuilder, or MagenticBuilder yet. These builders have more complex internal structures where entry/exit points are less straightforward. We want to validate the API design with ConcurrentBuilder and SequentialBuilder first before extending to these patterns. This is tracked as Phase 3 in the design doc.Description
Contribution Checklist