Description
Describe the feature
I'm working on a multi-agent AI service and want to use OpenAI Agents SDK to handle multi-agent related logic. In my AI service, function tools should be created via a custom UI admin panel. Function tool configurations will be stored in the external database, and OpenAI Agents SDK's FunctionTool
s will be built at runtime.
In order to implement it in the way I need, there should be a single on_invoke_tool
function used in every dynamically built FunctionTool
.
The only thing missing in the current implementation (it is also blocking the implementation) is the ability to know the name of the tool that triggered my generic on_invoke_tool
function.
Solution:
Add a new field in the ToolContext
: tool_name
.
So, the ToolContext
class with the new tool_name
, most likely, will look like this:
@dataclass
class ToolContext(RunContextWrapper[TContext]):
"""The context of a tool call."""
tool_name: str <--- new field
"""The name of the tool."""
tool_call_id: str = field(default_factory=_assert_must_pass_tool_call_id)
"""The ID of the tool call."""
@classmethod
def from_agent_context(
cls, context: RunContextWrapper[TContext], tool_name: str, tool_call_id: str
) -> "ToolContext":
"""
Create a ToolContext from a RunContextWrapper.
"""
# Grab the names of the RunContextWrapper's init=True fields
base_values: dict[str, Any] = {
f.name: getattr(context, f.name) for f in fields(RunContextWrapper) if f.init
}
return cls(tool_name=tool_name, tool_call_id=tool_call_id, **base_values)
With this new field, I will be able to know what tool is "running" now and provide tool_name
+ args
to another service processing the Function tool calls. Without the tool_name
, it is impossible to know what tool to process with a generic on_invoke_tool
function and multiple FunctionTool
classes using a single on_invoke_tool
.
I believe this enhancement can be helpful in other projects that need to achieve similar to what I'm trying to achieve.