Skip to content

Python: Flow custom kwargs to agents via Workflow SharedState - #2894

Merged
Evan Mattson (moonbox3) merged 5 commits into
microsoft:mainfrom
moonbox3:flow-workflow-kwargs
Dec 17, 2025
Merged

Evan Mattson (moonbox3) merged 5 commits into
microsoft:mainfrom
moonbox3:flow-workflow-kwargs

Conversation

@moonbox3

@moonbox3 Evan Mattson (moonbox3) commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor

Motivation and Context

This PR enables passing custom keyword arguments (for example, custom_data, user_token, or others) from workflow.run_stream() or run() directly to agent @ai_function tools.

Description

Fixes an issue where currently we aren't able to to propagate kwargs through workflow invocations to tools that may depend on certain input data (outside that of which the model would generate).

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings December 16, 2025 05:58
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Dec 16, 2025
@moonbox3 Evan Mattson (moonbox3) added squad: workflows workflows Usage: [Issues, PRs], Target: Workflows and removed documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs labels Dec 16, 2025
@markwallace-microsoft

Mark Wallace (markwallace-microsoft) commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/_workflows
   _agent_executor.py1592783%26, 93, 105–107, 137–138, 140–141, 156–157, 208–209, 211–212, 243–245, 255–257, 259, 263, 267, 271–272, 293
   _const.py60100% 
   _magentic.py100334066%47, 52, 74–83, 88, 92–103, 276, 281, 300, 302, 318, 326–335, 485, 489, 503, 509, 524, 604, 617, 634, 643–644, 646–648, 650, 661, 799–803, 808, 847, 894, 930–932, 934, 942–945, 949–952, 1086–1087, 1104, 1106–1107, 1115, 1151, 1160–1162, 1182, 1239, 1259, 1262, 1294, 1297, 1301–1302, 1315, 1325–1329, 1335, 1361–1365, 1373–1377, 1381–1382, 1385–1390, 1396–1398, 1401–1402, 1406–1407, 1418–1419, 1421–1422, 1427–1429, 1433–1434, 1436–1437, 1441–1442, 1444–1448, 1452–1454, 1458–1459, 1467, 1475, 1490, 1502, 1514–1517, 1540–1547, 1549–1552, 1562–1563, 1572–1573, 1578–1580, 1611, 1637, 1651, 1667, 1683, 1755, 1767–1768, 1800, 1836, 1841–1842, 1844, 1848–1850, 1881–1883, 1889–1891, 1894–1896, 1898, 1900, 1904, 1908, 1911, 1916, 1919–1924, 1927, 1929, 1934, 1938, 1941–1942, 1944–1946, 1949, 1953, 1958, 1961, 1966–1967, 2027–2028, 2033, 2037, 2039–2043, 2046–2048, 2051, 2054, 2061–2063, 2077, 2082–2083, 2310–2311, 2490–2491, 2493, 2507, 2512, 2515, 2571, 2582, 2593–2595, 2608–2609, 2614, 2627–2630, 2645–2647, 2662–2669, 2671, 2674–2675, 2677–2678, 2686, 2694–2695, 2697–2699, 2701–2704, 2708–2716, 2720–2721, 2724–2728, 2730–2731, 2733–2736, 2738–2741, 2743–2745, 2747–2748, 2750–2752, 2768–2771, 2783–2786, 2799–2802, 2806
   _workflow.py2501892%94, 264–266, 268–269, 287, 313, 315, 416, 696, 730, 735, 738, 757–759, 824
TOTAL16777263084% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2505 148 💤 0 ❌ 0 🔥 54.928s ⏱️

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces the ability to pass custom keyword arguments (e.g., user tokens, API configuration) from workflow invocation methods (run_stream() and run()) through to @ai_function tools via the **kwargs pattern. This enables tools to access contextual data without requiring it to be part of the model's prompt or conversation history.

Key changes:

  • Added **kwargs parameter to workflow run() and run_stream() methods that stores kwargs in SharedState for agent access
  • Modified agent executors to retrieve and propagate kwargs from SharedState to agent invocations
  • Extended Magentic workflow pattern to support kwargs flow through _MagenticStartMessage

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
python/packages/core/agent_framework/_workflows/_const.py Added WORKFLOW_RUN_KWARGS_KEY constant for SharedState key used to store and retrieve workflow kwargs
python/packages/core/agent_framework/_workflows/_workflow.py Added **kwargs to run() and run_stream() methods, storing kwargs in SharedState via run_kwargs parameter
python/packages/core/agent_framework/_workflows/_agent_executor.py Modified agent executor to retrieve kwargs from SharedState and pass them to agent run() and run_stream()
python/packages/core/agent_framework/_workflows/_magentic.py Extended _MagenticStartMessage and MagenticWorkflow to support kwargs flow; added message deduplication tests
python/packages/core/tests/workflow/test_workflow_kwargs.py Added comprehensive tests for kwargs flow through Sequential, Concurrent, and GroupChat workflows
python/packages/core/tests/workflow/test_magentic.py Added tests for message deduplication in magentic workflows (unrelated to kwargs feature)
python/samples/getting_started/workflows/state-management/workflow_kwargs.py New sample demonstrating kwargs flow to @ai_function tools with Sequential workflow
python/samples/getting_started/workflows/README.md Added reference to new workflow kwargs sample in state-management section

@markwallace-microsoft Mark Wallace (markwallace-microsoft) added the documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs label Dec 16, 2025
@nmccann

Copy link
Copy Markdown

This is great to see, as I ran into a similar problem recently. I ended up "resolving" it by having an executor that dynamically switched between different tools (I only had a boolean parameter) but it was rather messy.

I do have a question however - it seems like shared context isn't conserved for sub-workflows, and so this wouldn't be able to pass parameters to AI functions used by any sub-workflows. Would it make sense to create a separate issue for conserving shared context for sub-workflows, so that they could benefit from this?

@moonbox3

Copy link
Copy Markdown
Contributor Author

This is great to see, as I ran into a similar problem recently. I ended up "resolving" it by having an executor that dynamically switched between different tools (I only had a boolean parameter) but it was rather messy.

I do have a question however - it seems like shared context isn't conserved for sub-workflows, and so this wouldn't be able to pass parameters to AI functions used by any sub-workflows. Would it make sense to create a separate issue for conserving shared context for sub-workflows, so that they could benefit from this?

Thanks for the feedback, Noah McCann (@nmccann). I will create a new issue to track this and to make sure we handle the context properly for sub-workflows.

@moonbox3

Copy link
Copy Markdown
Contributor Author

Noah McCann (@nmccann) created #2918 to track this.

@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Dec 16, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 16, 2025
@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Dec 17, 2025
Merged via the queue into microsoft:main with commit 6adcac2 Dec 17, 2025
24 checks passed