Skip to content

Python: Remove workflow factory methods from WorkflowBuilder in favor of a helper instantiation method #3784

@moonbox3

Description

@moonbox3

Summary

Remove the register_executor(factory_func, ...) and register_agent(factory_func, ...) factory methods from WorkflowBuilder (Python). Instead, introduce a helper method that handles instantiation of executors/agents and then builds the Workflow object directly.

Motivation

The current factory-based approach (register_executor(lambda: ..., name=...), register_agent(create_agent, name=...)) requires managing factory callables throughout the codebase, along with async build methods and deferred initialization patterns. This adds complexity for state isolation that can be achieved more simply.

A helper method that creates executor/agent instances upfront and constructs the Workflow object provides:

  • Easier state isolation — fresh instances are created per call without needing lambda wrappers or factory registries
  • Simpler API — no need to reason about deferred initialization or factory lifecycle
  • Less boilerplate — users write a helper function instead of registering factories on a builder

Current API (to be removed)

# packages/core/agent_framework/_workflows/_workflow_builder.py
WorkflowBuilder.register_executor(factory_func: Callable[[], Executor], name: str | list[str]) -> Self
WorkflowBuilder.register_agent(factory_func: Callable[[], SupportsAgentRun], name: str, ...) -> Self

Used in samples like samples/getting_started/workflows/_start-here/step4_using_factories.py.

Proposed approach

Provide a helper method (e.g. on WorkflowBuilder or as a standalone utility) that:

  1. Instantiates executors and agents
  2. Builds the Workflow object
  3. Returns it ready to run

This keeps state isolation simple — each call to the helper creates fresh instances — without requiring factory management in every piece of code.

Scope

  • packages/core/agent_framework/_workflows/_workflow_builder.py — remove register_executor and register_agent factory methods
  • Update or remove samples/getting_started/workflows/_start-here/step4_using_factories.py
  • Update any tests referencing factory registration (e.g. test_workflow_builder.py)
  • Update the DeclarativeWorkflowBuilder and WorkflowFactory if they depend on factory methods
  • Add the new helper method with documentation and examples

Metadata

Metadata

Assignees

Labels

breaking changeIntroduces changes that are not backward compatible and may require updates to dependent code.pythonworkflowsRelated to Workflows in agent-framework

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions