Repository navigation
Python: Flow custom kwargs to agents via Workflow SharedState - #2894
Conversation
There was a problem hiding this comment.
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
**kwargsparameter to workflowrun()andrun_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 |
|
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. |
|
Noah McCann (@nmccann) created #2918 to track this. |
Motivation and Context
This PR enables passing custom keyword arguments (for example,
custom_data,user_token, or others) fromworkflow.run_stream()orrun()directly to agent@ai_functiontools.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