ag2 icon indicating copy to clipboard operation
ag2 copied to clipboard

[Bug]: Tools and parameters_json_schema

Open alecsolder opened this issue 7 months ago • 3 comments

Describe the bug

When you provide parameters_json_schema for a tool, it sets self._func_schema which prevents the agent.register_for_llm()(self) call from happening, meaning that it doesn't get registered as a tool, it gets registered as a function. This prevents dependency the code path where context_variables get swapped to a Depends.

At its core, I think the point of that param is to allow for the user to define a set of additional metadata for parameters that AG2 might not support or know about, in the context of what I was working on, this is Enums.

The other issue is that when the Depends for context_variables is added, it re-creates a tool

if __CONTEXT_VARIABLES_PARAM_NAME__ in current_tool.tool_schema["function"]["parameters"]["properties"]:
            # We'll replace the tool, so start with getting the underlying function
            tool_func = current_tool._func

            # Remove the Tool from the agent
            name = current_tool._name
            description = current_tool._description
            agent.remove_tool_for_llm(current_tool)

            # Recreate the tool without the context_variables parameter
            tool_func = self._modify_context_variables_param(current_tool._func, context_variables)
            tool_func = inject_params(tool_func)
            new_tool = ConversableAgent._create_tool_if_needed(
                func_or_tool=tool_func, name=name, description=description, parameters_json_schema=current_tool.parameters_json_schema
            )

            # Re-register with the agent
            agent.register_for_llm()(new_tool)

And doesn't pass in the parameters_json_schema again.

One solution could be to make additional metadata on parameters more of a first class thing and allow for modification of tools in place rather than needing to make new ones, but if you're going to that extreme then it likely makes sense to pull the dependency injection stuff out of GroupChatExecutor and all other places and into Tools possibly.

Steps to reproduce

No response

Model Used

No response

Expected Behavior

No response

Screenshots and logs

No response

Additional Information

No response

alecsolder avatar May 12 '25 02:05 alecsolder

Hi @alecsolder: Thanks for reporting this issue! We’ve noted it and will tag it for further attention.

harishmohanraj avatar May 12 '25 02:05 harishmohanraj

Related to #1946

Lancetnik avatar Aug 06 '25 20:08 Lancetnik

@aakash232

randombet avatar Sep 19 '25 03:09 randombet