Skip to content

Commit cc6bd2d

Browse files
authored
Merge pull request #797 from MicrosoftDocs/main
Merge main to live
2 parents 4d71395 + 5e8a7ae commit cc6bd2d

27 files changed

Lines changed: 1309 additions & 24 deletions

agent-framework/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ items:
1818
href: user-guide/workflows/TOC.yml
1919
- name: Hosting
2020
href: user-guide/hosting/TOC.yml
21+
- name: DevUI
22+
href: user-guide/devui/TOC.yml
2123
- name: Integrations
2224
items:
2325
- name: AG-UI

agent-framework/tutorials/workflows/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
- name: Handle requests and responses in workflows
1010
href: requests-and-responses.md
1111
- name: Checkpointing and resuming workflows
12-
href: checkpointing-and-resuming.md
12+
href: checkpointing-and-resuming.md
13+
- name: Register factories to workflow builder
14+
href: workflow-builder-with-factories.md

agent-framework/tutorials/workflows/agents-in-workflows.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Agents in Workflows
3-
description: Learn how to integrate agents into workflows using Agent Framework.
3+
description: Learn how to integrate agents into workflows.
44
zone_pivot_groups: programming-languages
55
author: TaoChenOSU
66
ms.topic: tutorial

agent-framework/tutorials/workflows/checkpointing-and-resuming.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Checkpointing and Resuming Workflows
3-
description: Learn how to implement checkpointing and resuming in workflows using Agent Framework.
3+
description: Learn how to implement checkpointing and resuming in workflows.
44
zone_pivot_groups: programming-languages
55
author: TaoChenOSU
66
ms.topic: tutorial
@@ -676,3 +676,8 @@ if __name__ == "__main__":
676676
For the complete working implementation, see the [Checkpoint with Resume sample](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/checkpoint/checkpoint_with_resume.py).
677677

678678
::: zone-end
679+
680+
## Next Steps
681+
682+
> [!div class="nextstepaction"]
683+
> [Learn about using factories in workflow builders](./workflow-builder-with-factories.md)

agent-framework/tutorials/workflows/requests-and-responses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Handle Requests and Responses in Workflows
3-
description: Learn how to handle requests and responses in workflows using Agent Framework.
3+
description: Learn how to handle requests and responses in workflows.
44
zone_pivot_groups: programming-languages
55
author: TaoChenOSU
66
ms.topic: tutorial

agent-framework/tutorials/workflows/simple-concurrent-workflow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Create a Simple Concurrent Workflow
3-
description: Learn how to create a simple concurrent workflow using Agent Framework.
3+
description: Learn how to create a simple concurrent workflow.
44
zone_pivot_groups: programming-languages
55
author: TaoChenOSU
66
ms.topic: tutorial

agent-framework/tutorials/workflows/simple-sequential-workflow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Create a Simple Sequential Workflow
3-
description: Learn how to create a simple sequential workflow using Agent Framework.
3+
description: Learn how to create a simple sequential workflow.
44
zone_pivot_groups: programming-languages
55
author: TaoChenOSU
66
ms.topic: tutorial
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: Register Factories to Workflow Builder
3+
description: Learn how to register factories to the workflow builder.
4+
zone_pivot_groups: programming-languages
5+
author: TaoChenOSU
6+
ms.topic: tutorial
7+
ms.author: taochen
8+
ms.date: 09/29/2025
9+
ms.service: agent-framework
10+
---
11+
12+
# Register Factories to Workflow Builder
13+
14+
Up to this point, we've been creating executor instances and passing them directly to the `WorkflowBuilder`. This approach works well for simple scenarios where you only need a single workflow instance. However, in more complex cases you may want to create multiple, isolated instances of the same workflow. To support this, each workflow instance must receive its own set of executor instances. Reusing the same executors would cause their internal state to be shared across workflows, resulting in unintended side effects. To avoid this, you can register executor factories with the `WorkflowBuilder`, ensuring that new executor instances are created for each workflow instance.
15+
16+
## Registering Factories to Workflow Builder
17+
18+
::: zone pivot="programming-language-csharp"
19+
20+
Coming soon...
21+
22+
::: zone-end
23+
24+
::: zone pivot="programming-language-python"
25+
26+
To register an executor factory to the `WorkflowBuilder`, you can use the `register_executor` method. This method takes two parameters: the factory function that creates instances of the executor (of type `Executor` or derivation of `Executor`) and the name of the factory to be used in the workflow configuration.
27+
28+
```python
29+
class UpperCase(Executor):
30+
def __init__(self, id: str):
31+
super().__init__(id=id)
32+
33+
@handler
34+
async def to_upper_case(self, text: str, ctx: WorkflowContext[str]) -> None:
35+
"""Convert the input to uppercase and forward it to the next node."""
36+
result = text.upper()
37+
38+
# Send the result to the next executor in the workflow.
39+
await ctx.send_message(result)
40+
41+
class Accumulate(Executor):
42+
def __init__(self, id: str):
43+
super().__init__(id=id)
44+
# Executor internal state that should not be shared among different workflow instances.
45+
self._text_length = 0
46+
47+
@handler
48+
async def accumulate(self, text: str, ctx: WorkflowContext) -> None:
49+
"""Accumulate the length of the input text and log it."""
50+
self._text_length += len(text)
51+
print(f"Accumulated text length: {self._text_length}")
52+
53+
@executor(id="reverse_text_executor")
54+
async def reverse_text(text: str, ctx: WorkflowContext[str]) -> None:
55+
"""Reverse the input string and send it downstream."""
56+
result = text[::-1]
57+
58+
# Send the result to the next executor in the workflow.
59+
await ctx.yield_output(result)
60+
61+
workflow_builder = (
62+
WorkflowBuilder()
63+
.register_executor(
64+
factory_func=lambda: UpperCase(id="UpperCaseExecutor"),
65+
name="UpperCase",
66+
)
67+
.register_executor(
68+
factory_func=lambda: Accumulate(id="AccumulateExecutor"),
69+
name="Accumulate",
70+
)
71+
.register_executor(
72+
factory_func=lambda: reverse_text,
73+
name="ReverseText",
74+
)
75+
# Use the factory name to configure the workflow
76+
.add_fan_out_edges("UpperCase", ["Accumulate", "ReverseText"])
77+
.set_start_executor("UpperCase")
78+
)
79+
```
80+
81+
Build a workflow using the builder
82+
83+
```python
84+
# Build the workflow using the builder
85+
workflow_a = workflow_builder.build()
86+
await workflow_a.run("hello world")
87+
await workflow_a.run("hello world")
88+
```
89+
90+
Expected output:
91+
92+
```plaintext
93+
Accumulated text length: 22
94+
```
95+
96+
Now let's create another workflow instance and run it. The `Accumulate` executor should have its own internal state and not share the state with the first workflow instance.
97+
98+
```python
99+
# Build another workflow using the builder
100+
# This workflow will have its own set of executors, including a new instance of the Accumulate executor.
101+
workflow_b = workflow_builder.build()
102+
await workflow_b.run("hello world")
103+
```
104+
105+
Expected output:
106+
107+
```plaintext
108+
Accumulated text length: 11
109+
```
110+
111+
To register an agent factory to the `WorkflowBuilder`, you can use the `register_agent` method. This method takes two parameters: the factory function that creates instances of the agent (of types that implement `AgentProtocol`) and the name of the factory to be used in the workflow configuration.
112+
113+
```python
114+
def create_agent() -> ChatAgent:
115+
"""Factory function to create a Writer agent."""
116+
return AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
117+
instructions=("You are a helpful assistant.",),
118+
name="assistant",
119+
)
120+
121+
workflow_builder = (
122+
WorkflowBuilder()
123+
.register_agent(
124+
factory_func=create_agent,
125+
name="Assistant",
126+
)
127+
# Register other executors or agents as needed and configure the workflow
128+
...
129+
)
130+
131+
# Build the workflow using the builder
132+
workflow = workflow_builder.build()
133+
```
134+
135+
Each time a new workflow instance is created, the agent in the workflow will be a new instance created by the factory function, and will get a new thread instance.
136+
137+
::: zone-end
138+
139+
## Workflow State Isolation
140+
141+
To learn more about workflow state isolation, refer to the [Workflow State Isolation](../../user-guide/workflows/state-isolation.md) documentation.

agent-framework/tutorials/workflows/workflow-with-branching-logic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Create a Workflow with Branching Logic
3-
description: Learn how to create a workflow with branching logic using Agent Framework.
3+
description: Learn how to create a workflow with branching logic.
44
zone_pivot_groups: programming-languages
55
author: TaoChenOSU
66
ms.topic: tutorial
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- name: Overview
2+
href: index.md
3+
- name: Directory Discovery
4+
href: directory-discovery.md
5+
- name: API Reference
6+
href: api-reference.md
7+
- name: Tracing & Observability
8+
href: tracing.md
9+
- name: Security & Deployment
10+
href: security.md
11+
- name: Samples
12+
href: samples.md

0 commit comments

Comments
 (0)