Python: feat: enable dynamic tool loading by passing tools list in kwargs [progressive exposure]#3398
Python: feat: enable dynamic tool loading by passing tools list in kwargs [progressive exposure]#3398suneetnangia wants to merge 8 commits intomicrosoft:mainfrom
Conversation
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
Enables progressive/dynamic tool exposure by forwarding the active tools list to tool functions via **kwargs, allowing tools to register additional tools during execution.
Changes:
- Pass the current
toolscollection into tool runtime kwargs in both non-streaming and streaming function invocation wrappers. - Extend kwargs-propagation tests to assert the
toolslist is present in tool**kwargs. - Add a new Python sample demonstrating dynamic tool loading at runtime.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_tools.py | Injects the current tools list into custom_args passed to tool execution for dynamic tool registration. |
| python/packages/core/tests/core/test_kwargs_propagation_to_ai_function.py | Adds assertions ensuring tools is propagated in tool **kwargs. |
| python/samples/getting_started/tools/dynamic_tool_loading.py | New sample showing a tool that adds factorial/fibonacci tools during the same agent run. |
python/packages/core/tests/core/test_kwargs_propagation_to_ai_function.py
Outdated
Show resolved
Hide resolved
| # Access tools list directly | ||
| tools_list = kwargs.get("tools") | ||
|
|
||
| if not tools_list: |
There was a problem hiding this comment.
In this sample, if not tools_list: will treat an empty list as missing. Since the framework can legitimately pass an empty tools list, this should distinguish None/missing from an empty list (and ideally validate the type before branching) so the error path isn’t triggered incorrectly.
| if not tools_list: | |
| if tools_list is None: |
There was a problem hiding this comment.
Not sure if the suggestion is valid in this instance.
Since we are checking in the tool itself, framework should never pass is an empty i.e. there should be at least 1 tool?
- Fixed inconsistency where approved tool executions didn't receive kwargs['tools'] - Both async and streaming approval paths now use custom_args_with_tools - This ensures dynamic tool loading works consistently with approvals - Added comprehensive test coverage for tools identity checks - All tests verify tools list is the same object reference for mutation
…into tools-search-tool
Signed-off-by: Suneet Nangia <suneetnangia@gmail.com>
|
Closing in favour of #3877 @markwallace-microsoft, @eavanvalkenburg |
This pull request enhances the agent framework to support dynamic tool loading, enabling tools to add new capabilities during execution. The main changes ensure that the current tools list is passed to tool functions, allowing them to register additional tools on the fly, this allows progressive tools exposure for LLM instead of loading all tools upfront. A new sample is also provided to demonstrate this feature in action.
Framework improvements for dynamic tool loading:
function_invocation_wrapperandstreaming_function_invocation_wrapperto pass the currenttoolslist in thekwargsto tool functions, allowing tools to dynamically add more tools during execution. [1] [2]Testing and sample updates:
mock_get_responseto verify that thetoolslist is present inkwargsand is a list, ensuring correct propagation for dynamic tool loading.dynamic_tool_loading.pydemonstrating how a tool can add new tools (e.g., factorial and fibonacci calculators) to the agent at runtime, and how these tools can be used immediately within the same run.