|
1 | 1 | --- |
2 | 2 | title: AutoGen to Microsoft Agent Framework Migration Guide |
3 | 3 | description: A comprehensive guide for migrating from AutoGen to the Microsoft Agent Framework Python SDK. |
4 | | -author: ekzhu |
| 4 | +author: moonbox3 |
5 | 5 | ms.topic: reference |
6 | | -ms.author: ekzhu |
| 6 | +ms.author: evmattso |
7 | 7 | ms.date: 09/29/2025 |
8 | 8 | ms.service: agent-framework |
9 | 9 | --- |
@@ -1195,85 +1195,134 @@ result = await team.run("Complex research and analysis task") |
1195 | 1195 | **Agent Framework Implementation:** |
1196 | 1196 |
|
1197 | 1197 | ```python |
| 1198 | +from typing import cast |
1198 | 1199 | from agent_framework import ( |
1199 | | - MagenticBuilder, MagenticCallbackMode, WorkflowOutputEvent, |
1200 | | - MagenticCallbackEvent, MagenticOrchestratorMessageEvent, MagenticAgentDeltaEvent |
| 1200 | + MAGENTIC_EVENT_TYPE_AGENT_DELTA, |
| 1201 | + MAGENTIC_EVENT_TYPE_ORCHESTRATOR, |
| 1202 | + AgentRunUpdateEvent, |
| 1203 | + ChatAgent, |
| 1204 | + ChatMessage, |
| 1205 | + MagenticBuilder, |
| 1206 | + WorkflowOutputEvent, |
1201 | 1207 | ) |
| 1208 | +from agent_framework.openai import OpenAIChatClient |
1202 | 1209 |
|
1203 | | -# Assume we have researcher, coder, and coordinator_client from previous examples |
1204 | | -async def on_event(event: MagenticCallbackEvent) -> None: |
1205 | | - if isinstance(event, MagenticOrchestratorMessageEvent): |
1206 | | - print(f"[ORCHESTRATOR]: {event.message.text}") |
1207 | | - elif isinstance(event, MagenticAgentDeltaEvent): |
1208 | | - print(f"[{event.agent_id}]: {event.text}", end="") |
1209 | | - |
1210 | | -workflow = (MagenticBuilder() |
1211 | | - .participants(researcher=researcher, coder=coder) |
1212 | | - .on_event(on_event, mode=MagenticCallbackMode.STREAMING) |
1213 | | - .with_standard_manager( |
1214 | | - chat_client=coordinator_client, |
1215 | | - max_round_count=20, |
1216 | | - max_stall_count=3, |
1217 | | - max_reset_count=2 |
1218 | | - ) |
1219 | | - .build()) |
| 1210 | +# Create a manager agent for orchestration |
| 1211 | +manager_agent = ChatAgent( |
| 1212 | + name="MagenticManager", |
| 1213 | + description="Orchestrator that coordinates the workflow", |
| 1214 | + instructions="You coordinate a team to complete complex tasks efficiently.", |
| 1215 | + chat_client=OpenAIChatClient(), |
| 1216 | +) |
| 1217 | + |
| 1218 | +workflow = ( |
| 1219 | + MagenticBuilder() |
| 1220 | + .participants(researcher=researcher, coder=coder) |
| 1221 | + .with_standard_manager( |
| 1222 | + agent=manager_agent, |
| 1223 | + max_round_count=20, |
| 1224 | + max_stall_count=3, |
| 1225 | + max_reset_count=2, |
| 1226 | + ) |
| 1227 | + .build() |
| 1228 | +) |
1220 | 1229 |
|
1221 | 1230 | # Example usage (would be in async context) |
1222 | 1231 | async def magentic_example(): |
| 1232 | + output: str | None = None |
1223 | 1233 | async for event in workflow.run_stream("Complex research task"): |
1224 | | - if isinstance(event, WorkflowOutputEvent): |
1225 | | - final_result = event.data |
| 1234 | + if isinstance(event, AgentRunUpdateEvent): |
| 1235 | + props = event.data.additional_properties if event.data else None |
| 1236 | + event_type = props.get("magentic_event_type") if props else None |
| 1237 | + |
| 1238 | + if event_type == MAGENTIC_EVENT_TYPE_ORCHESTRATOR: |
| 1239 | + text = event.data.text if event.data else "" |
| 1240 | + print(f"[ORCHESTRATOR]: {text}") |
| 1241 | + elif event_type == MAGENTIC_EVENT_TYPE_AGENT_DELTA: |
| 1242 | + agent_id = props.get("agent_id", event.executor_id) if props else event.executor_id |
| 1243 | + if event.data and event.data.text: |
| 1244 | + print(f"[{agent_id}]: {event.data.text}", end="") |
| 1245 | + |
| 1246 | + elif isinstance(event, WorkflowOutputEvent): |
| 1247 | + output_messages = cast(list[ChatMessage], event.data) |
| 1248 | + if output_messages: |
| 1249 | + output = output_messages[-1].text |
1226 | 1250 | ``` |
1227 | 1251 |
|
1228 | 1252 | **Agent Framework Customization Options:** |
1229 | 1253 |
|
1230 | 1254 | The Magentic workflow provides extensive customization options: |
1231 | 1255 |
|
1232 | | -- **Manager configuration**: Custom orchestrator models and prompts |
| 1256 | +- **Manager configuration**: Use a ChatAgent with custom instructions and model settings |
1233 | 1257 | - **Round limits**: `max_round_count`, `max_stall_count`, `max_reset_count` |
1234 | | -- **Event callbacks**: Real-time streaming with granular event filtering |
| 1258 | +- **Event streaming**: Use `AgentRunUpdateEvent` with `magentic_event_type` metadata |
1235 | 1259 | - **Agent specialization**: Custom instructions and tools per agent |
1236 | | -- **Callback modes**: `STREAMING` for real-time updates or `BATCH` for final results |
1237 | | -- **Human-in-the-loop planning**: Custom planner functions for interactive workflows |
| 1260 | +- **Human-in-the-loop**: Plan review, tool approval, and stall intervention |
1238 | 1261 |
|
1239 | 1262 | ```python |
1240 | 1263 | # Advanced customization example with human-in-the-loop |
| 1264 | +from typing import cast |
| 1265 | +from agent_framework import ( |
| 1266 | + MAGENTIC_EVENT_TYPE_AGENT_DELTA, |
| 1267 | + MAGENTIC_EVENT_TYPE_ORCHESTRATOR, |
| 1268 | + AgentRunUpdateEvent, |
| 1269 | + ChatAgent, |
| 1270 | + MagenticBuilder, |
| 1271 | + MagenticHumanInterventionDecision, |
| 1272 | + MagenticHumanInterventionKind, |
| 1273 | + MagenticHumanInterventionReply, |
| 1274 | + MagenticHumanInterventionRequest, |
| 1275 | + RequestInfoEvent, |
| 1276 | + WorkflowOutputEvent, |
| 1277 | +) |
1241 | 1278 | from agent_framework.openai import OpenAIChatClient |
1242 | | -from agent_framework import MagenticBuilder, MagenticCallbackMode, MagenticPlannerContext |
1243 | | - |
1244 | | -# Assume we have researcher_agent, coder_agent, analyst_agent, detailed_event_handler |
1245 | | -# and get_human_input function defined elsewhere |
1246 | | - |
1247 | | -async def custom_planner(context: MagenticPlannerContext) -> str: |
1248 | | - """Custom planner with human input for critical decisions.""" |
1249 | | - if context.round_count > 5: |
1250 | | - # Request human input for complex decisions |
1251 | | - return await get_human_input(f"Next action for: {context.current_state}") |
1252 | | - return "Continue with automated planning" |
1253 | | - |
1254 | | -workflow = (MagenticBuilder() |
1255 | | - .participants( |
1256 | | - researcher=researcher_agent, |
1257 | | - coder=coder_agent, |
1258 | | - analyst=analyst_agent |
1259 | | - ) |
1260 | | - .with_standard_manager( |
1261 | | - chat_client=OpenAIChatClient(model_id="gpt-5"), |
1262 | | - max_round_count=15, # Limit total rounds |
1263 | | - max_stall_count=2, # Prevent infinite loops |
1264 | | - max_reset_count=1, # Allow one reset on failure |
1265 | | - orchestrator_prompt="Custom orchestration instructions..." |
1266 | | - ) |
1267 | | - .with_planner(custom_planner) # Human-in-the-loop planning |
1268 | | - .on_event(detailed_event_handler, mode=MagenticCallbackMode.STREAMING) |
1269 | | - .build()) |
| 1279 | + |
| 1280 | +# Create manager agent with custom configuration |
| 1281 | +manager_agent = ChatAgent( |
| 1282 | + name="MagenticManager", |
| 1283 | + description="Orchestrator for complex tasks", |
| 1284 | + instructions="Custom orchestration instructions...", |
| 1285 | + chat_client=OpenAIChatClient(model_id="gpt-4o"), |
| 1286 | +) |
| 1287 | + |
| 1288 | +workflow = ( |
| 1289 | + MagenticBuilder() |
| 1290 | + .participants( |
| 1291 | + researcher=researcher_agent, |
| 1292 | + coder=coder_agent, |
| 1293 | + analyst=analyst_agent, |
| 1294 | + ) |
| 1295 | + .with_standard_manager( |
| 1296 | + agent=manager_agent, |
| 1297 | + max_round_count=15, # Limit total rounds |
| 1298 | + max_stall_count=2, # Trigger stall handling |
| 1299 | + max_reset_count=1, # Allow one reset on failure |
| 1300 | + ) |
| 1301 | + .with_plan_review() # Enable human plan review |
| 1302 | + .with_human_input_on_stall() # Enable human intervention on stalls |
| 1303 | + .build() |
| 1304 | +) |
| 1305 | + |
| 1306 | +# Handle human intervention requests during execution |
| 1307 | +async for event in workflow.run_stream("Complex task"): |
| 1308 | + if isinstance(event, RequestInfoEvent) and event.request_type is MagenticHumanInterventionRequest: |
| 1309 | + req = cast(MagenticHumanInterventionRequest, event.data) |
| 1310 | + if req.kind == MagenticHumanInterventionKind.PLAN_REVIEW: |
| 1311 | + # Review and approve the plan |
| 1312 | + reply = MagenticHumanInterventionReply( |
| 1313 | + decision=MagenticHumanInterventionDecision.APPROVE |
| 1314 | + ) |
| 1315 | + async for ev in workflow.send_responses_streaming({event.request_id: reply}): |
| 1316 | + pass # Handle continuation |
1270 | 1317 | ``` |
1271 | 1318 |
|
1272 | 1319 | For detailed Magentic examples, see: |
1273 | 1320 |
|
1274 | 1321 | - [Basic Magentic Workflow](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/orchestration/magentic.py) - Standard orchestrated multi-agent workflow |
1275 | 1322 | - [Magentic with Checkpointing](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/orchestration/magentic_checkpoint.py) - Persistent orchestrated workflows |
1276 | | -- [Magentic Human Plan Update](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/orchestration/magentic_human_plan_update.py) - Human-in-the-loop planning |
| 1323 | +- [Magentic Human Plan Update](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/orchestration/magentic_human_plan_update.py) - Human-in-the-loop plan review |
| 1324 | +- [Magentic Agent Clarification](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/orchestration/magentic_agent_clarification.py) - Tool approval for agent clarification |
| 1325 | +- [Magentic Human Replan](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/orchestration/magentic_human_replan.py) - Human intervention on stalls |
1277 | 1326 |
|
1278 | 1327 | #### Future Patterns |
1279 | 1328 |
|
|
0 commit comments