Python: Progressive Tools Exposure#3877
Conversation
Signed-off-by: Suneet Nangia <suneetnangia@gmail.com>
Signed-off-by: Suneet Nangia <suneetnangia@gmail.com>
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
This PR adds “progressive tools exposure” to the Python agent framework by injecting the current tools list into **kwargs for tools that accept keyword arguments, enabling tools to dynamically add additional tools during the same agent run.
Changes:
- Core: inject a mutable
toolslist into tool runtime kwargs during function invocation (python/packages/core/agent_framework/_tools.py). - Tests: add async coverage validating tools-list propagation, dynamic tool addition, persistence across invocations, and approval-mode behavior (
python/packages/core/tests/core/test_kwargs_propagation_to_ai_function.py). - Samples/docs: add a new sample demonstrating dynamic tool exposure and reference it in the samples README (
python/samples/getting_started/tools/function_tool_dynamic_tool_exposure.py,python/samples/README.md).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_tools.py | Injects the tools list into runtime kwargs passed to tools, enabling in-run mutation for progressive exposure. |
| python/packages/core/tests/core/test_kwargs_propagation_to_ai_function.py | Adds tests confirming tools list is available in kwargs and that mutations persist and work with approvals. |
| python/samples/getting_started/tools/function_tool_dynamic_tool_exposure.py | New sample showing a tool that dynamically registers additional math tools at runtime. |
| python/samples/README.md | Adds the new sample to the tools samples index. |
Signed-off-by: Suneet Nangia <suneetnangia@gmail.com>
Signed-off-by: Suneet Nangia <suneetnangia@gmail.com>
Signed-off-by: Suneet Nangia <suneetnangia@gmail.com>
|
@suneetnangia, friendly reminder — this issue is waiting on your response. Please share any updates when you get a chance. (This is an automated message.) |
eavanvalkenburg
left a comment
There was a problem hiding this comment.
We've had some updates to the way things are passed to functions, now using a context object, we can add this feature in there to make this a bit more firstclass, if you can have a look at that then we can get this in, I like it!
| runtime modification of available tools. | ||
|
|
||
| Run this example with the following cmd (after setting appropriate Azure OpenAI env vars): | ||
| export AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=<your-deployment> && export AZURE_OPENAI_ENDPOINT=<your-endpoint> && uv run python samples/02-agents/tools/function_tool_dynamic_tool_exposure.py |
There was a problem hiding this comment.
needs a update of the sample, but the rest looks good.
| @tool | ||
| def load_maths_tools( | ||
| operation: Annotated[str, "The maths operation category to load (e.g., 'advanced')"], | ||
| **kwargs: Any, |
There was a problem hiding this comment.
we now have the concept of FunctionInvocationContext, I think we should add a field for tools in there, that can be set to the AdditiveToolsList class (and maybe just call it ToolsList).
|
Thank you so much @suneetnangia for driving this feature and for your patience through the reviews! 🙏 As discussed, we've reworked the approach on top of the new |
Motivation and Context
This PR enables loading tools progressively as model (S/LLM) expands on the execution plan, in the same run; in several instances model is frontloaded with too many tools, this results in lower tool selection accuracy (a known concern), bloated context and higher processing costs; this change will allow building solutions where model (S/LLM) will have access to 1000s of tools dynamically without any of the forementioned concerns.
This PR supersedes #3398 (now closed due to major changes in codebase since then)
Description
This pull request introduces dynamic tool loading capabilities to the agent framework, allowing tools to access and modify the list of available tools at runtime. This enables advanced scenarios such as loading new tools based on context or previous tool calls. The implementation is thoroughly tested, and a new sample demonstrates the feature in action.
Core framework enhancements:
**kwargsof tools that accept keyword arguments, allowing tools to inspect or modify the set of available tools during execution. If the tools input is already a list, the same object is used so modifications persist. (python/packages/core/agent_framework/_tools.py) [1] [2]Testing and validation:
kwargsand can inspect it.**kwargsdo not receive the tools list.python/packages/core/tests/core/test_kwargs_propagation_to_ai_function.py)Documentation and samples:
python/samples/getting_started/tools/function_tool_dynamic_tool_exposure.py)python/samples/README.md)Contribution Checklist