ISSUE DESCRIPTION [BUG] Duplicate activate_skill tool definition error when using…
- User
ISSUE DESCRIPTION
[BUG] Duplicate
activate_skilltool definition error when usingAgenticServices.createAgenticSystem()Describe the bug When creating an agentic system withAgenticServices.createAgenticSystem(), even with a single agent, the application throws anIllegalConfigurationException: Duplicated definition for tool: activate_skillduring execution, caused by multiple registrations of non-dynamicToolProviderinstances.Log and Stack trace
Caused by: dev.langchain4j.service.IllegalConfigurationException: Duplicated definition for tool: activate_skill at dev.langchain4j.service.tool.ToolService.addTools(ToolService.java:398) at dev.langchain4j.service.tool.ToolService.lambda$createContextFromStaticToolsAndProviders$0(ToolService.java:378) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at dev.langchain4j.service.tool.ToolService.createContextFromStaticToolsAndProviders(ToolService.java:371) at dev.langchain4j.service.tool.ToolService.createContext(ToolService.java:338) at dev.langchain4j.service.DefaultAiServices$1.invoke(DefaultAiServices.java:260) at dev.langchain4j.service.DefaultAiServices$1.invoke(DefaultAiServices.java:154) at jdk.proxy2/jdk.proxy2.$Proxy5.test(Unknown Source) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ... 5 moreTo Reproduce
public class Main { public static void main(String[] args) { var result=AgenticServices.createAgenticSystem(FooAgent.class).test(); // var result=AgenticServices.agentBuilder(FooAgent.class).build().test(); System.out.println(result); } public interface FooAgent { @Agent @UserMessage("Hello") String test(); @ToolProviderSupplier static ToolProvider testTool() { return Skills.builder() .skills(Skill.builder().name("test").description("test").content("test").build()) .build().toolProvider(); } @ChatModelSupplier static ChatModel chatModel() { return OpenAiChatModel.builder() .baseUrl("http://192.168.31.32:8000/v1") .apiKey("none") .modelName("qwen3.6") .logRequests(true) .logResponses(true) .build(); } } }Expected behavior The agentic system should run successfully, properly deduplicating tools (including the built-in
activate_skilltool) when registering non-dynamicToolProviderinstances.Please complete the following information:
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant files. Let's first check the current directory and see what's available.
- Assistant
Let me explore the repository structure to understand the codebase better, especially around the
AgenticServices.createAgenticSystem()method and how tools/providers are being registered. - Assistant
Let me first look at the AgenticServices.java file to understand the createAgenticSystem method and how it registers tools.
- Assistant
Let me search for the
createAgenticSystemmethod specifically. - Assistant
Let me look at the full
createAgenticSystemmethod to understand how it works. - Assistant
Let me look at the
configureAgentmethod and theAgentBuilderclass to understand how tools are registered. - Assistant
Let me look at the DeclarativeUtil.java file to understand how tools are registered.
- Assistant
Now let me look at the
AgentBuilderclass to understand how tools are registered. - Assistant
Let me examine the AgentBuilder.java to understand how tools are registered.
- Assistant
Let me look at the
Skillsclass and howactivate_skilltool is added. Let me search forSkillsandactivate_skill. - Assistant
Let me examine the Skills class to understand how the toolProvider is created:
- Assistant
Now let me check how tools from tool providers are resolved. Let me look at
ToolService: - Assistant
Let me see the rest of the
addToolsmethod: - Assistant
Let me look at the AgentBuilder constructor again:
- Assistant
Let me look at the AgentBuilder constructor more carefully:
- Assistant
Now I understand the issue. Let me look at the
createAgenticSystemmethod more carefully: - Assistant
Let me also look at the
setupToolsmethod in AgentBuilder: - Assistant
Now I understand the issue. Let me check the full flow of how tools are registered when using
createAgenticSystem. The issue is thatconfigureAgentgets called twice: once in theAgentBuilderconstructor, and again explicitly increateAgenticSystem, causing duplicate tool provider registrations. Let me verify this: - Assistant
Let me check how
buildSubAgentuses configureAgent: - Assistant
Now I need to understand how to fix this. The issue is that
configureAgentis called twice: once in theAgentBuilderconstructor and again explicitly. The tool providers get added twice to the list, causing duplicates. Let me look at theconfigureAgentmethod signature: - Assistant
Let me see what the public
configureAgentmethods do and what the private one does: - Assistant
Now I understand the issue. Let me check if there are tests that verify this behavior:
- Assistant
Let me examine a test to understand how the codebase expects things to work:
- Assistant
Now I understand the root cause. Let me trace through the issue:
agentBuilder(agentServiceClass)creates anAgentBuilder, and in its constructor callsconfigureAgent(agentServiceClass, this)which registers all tool providers (including the Skills toolProvider that addsactivate_skill)- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which re-invokes the static@ToolProviderSuppliermethod, creating a new ToolProvider instance with the sameactivate_skilltool, and adding it to the list
This causes duplicate tool registration in
ToolService.Let me verify by looking at the flow more carefully:
- Assistant
Now I understand the issue clearly. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which registers all tool providers. ThencreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which re-invokes the@ToolProviderSuppliermethod and adds a duplicateToolProviderwith the sameactivate_skilltool.Let me check if
buildSubAgenthas the same issue: - Assistant
Let me look at
buildSubAgentandcreateSubagentsmore closely: - Assistant
Now I understand the issue clearly. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which registers tool providers (including the Skillsactivate_skilltool). ThencreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which re-invokes the static@ToolProviderSuppliermethod, creating a new ToolProvider instance with the sameactivate_skilltool, causing duplicates.Let me create a fix. The approach should be to not call
configureAgenttwice. I need to understand what the second call adds beyond the first:- The constructor's call:
configureAgent(agentType, null, true, agentBuilder, AgentConfigurator.empty()) - The explicit call:
configureAgent(agentType, chatModel, false, agentBuilder, agentConfigurator)
The differences are:
- The chat model (null vs actual) - needed for fallback when no
@ChatModelSupplieris present allowNullChatModelflag- The
agentConfigurator- needed for customization
Let me implement the fix:
- The constructor's call:
- Assistant
Let me look at the
AgentBuilder.toolProviderandtoolProvidersmethods more closely, and thetoolProvidersfield: - Assistant
Let me look at the
AgentConfiguratorclass: - Assistant
Let me search more broadly.
- Assistant
Let me try a different search.
- Assistant
Now I have a clear understanding of the issue. Let me implement the fix. The problem is in
AgenticServices.createAgenticSystemwhereconfigureAgentis called twice - once in theAgentBuilderconstructor and once explicitly. The fix is to avoid the duplicate call. - Assistant
Let me also check how
buildSubAgentcalls configureAgent: - Assistant
Let me look at the
DeclarativeAgentCreationContextinterface andDefaultDeclarativeAgentCreationContextclass: - Assistant
Let me look at the full context around line 275:
- Assistant
Now I have a complete understanding of the issue. Let me implement the fix:
Root cause:
configureAgentis called twice:- In the
AgentBuilderconstructor (line 141):configureAgent(agentServiceClass, this) - In
createAgenticSystem(line 338):configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)
Each call invokes the
@ToolProviderSuppliermethod which creates a new ToolProvider instance with theactivate_skilltool, causing duplicates.Fix: Don't call
configureAgenttwice. Instead, only apply the chatModel fallback and agentConfigurator after the constructor'sconfigureAgentcall.First, let me add a method to
AgentBuilderto check if a chat model is already configured: - In the
- Assistant
Now let me implement the fix. I need to add a method to
AgentBuilderto check if a chat model is already configured, then modifycreateAgenticSystemandcreateSubagentto not callconfigureAgenttwice. - Assistant
Now let me add a
hasChatModel()method toAgentBuilderright after thehasNonDefaultChatMemory()method: - Assistant
Now I have a complete understanding of the issue. Let me implement the fix step by step.
First, let me add a
hasChatModel()method toAgentBuilder: - Assistant
Now I have a clear understanding of the issue. Let me trace through the exact problem:
agentBuilder(agentServiceClass)creates anAgentBuilder, whose constructor callsconfigureAgent(agentServiceClass, this)— this adds tool providers (including the Skillsactivate_skilltool)- Then
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again — this re-invokes the@ToolProviderSuppliermethod, creating a NEWToolProviderinstance and adding it to the list - Both tool providers contain the same
activate_skilltool, causing the duplicate error
The fix should prevent calling
configureAgenttwice. Let me implement it: - Assistant
Now let me implement the fix. The approach is to modify
AgenticServicesto not callconfigureAgenttwice. Instead, after theAgentBuilderconstructor (which already callsconfigureAgent), we only need to:- Set the chatModel fallback if not already set by annotation
- Apply the agentConfigurator
Let me first add a method to
DeclarativeUtilthat applies only the chatModel fallback and configurator: - Assistant
Now I understand the issue clearly. Let me trace through the problem:
agentBuilder(agentServiceClass)createsAgentBuilderwhose constructor callsconfigureAgent(agentServiceClass, this)→ this adds tool providers (includingactivate_skill)createAgenticSystemthen callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again → this re-invokes the static@ToolProviderSuppliermethod, creating a NEWToolProviderinstance and adding it again
The fix: Don't call
configureAgenttwice. After the constructor already configured the agent, only apply the chatModel fallback and agentConfigurator.Let me implement this:
- Assistant
Let me look at the full private
configureAgentmethod to understand all the logic: - Assistant
Let me see the rest of the
configureAgentmethod: - Assistant
Now I have a clear understanding. Let me implement the fix. The approach is:
- Add a new method
applyChatModelFallbackAndConfiguratortoDeclarativeUtilthat only applies the chatModel fallback and agentConfigurator (without re-registering tools/providers) - Modify
createAgenticSystemandcreateSubagentto use this method instead of callingconfigureAgentagain
- Add a new method
- Assistant
Now let me implement the fix. I'll add a new method to
DeclarativeUtilthat only applies the chatModel fallback and agentConfigurator (without re-registering tools/providers), and then modifycreateAgenticSystemandcreateSubagentto use it. - Assistant
Now let me add the new method to
DeclarativeUtil. I'll add it right after theconfigureAgentmethod: - Assistant
Now let me add the new method to
DeclarativeUtil. I'll add it right after theconfigureAgentmethod (after line 149): - Assistant
Let me try with python instead.
- Assistant
Let me use the edit command instead. First, let me open the file:
- Assistant
Now let me add the new method after the
configureAgentmethod: - Assistant
I see there are some issues with the edit - there's a missing closing brace for the
configureAgentmethod and a duplicatecheckArgumentsline. Let me fix these: - Assistant
Good, the
DeclarativeUtil.javafile now has the new method. Now I need to modify theAgenticServices.javafile to use this new method instead of callingconfigureAgenttwice. Let me update thecreateAgenticSystemmethod: - Assistant
Now I need to modify the
createAgenticSystemmethod to use the new method. First, let me check the imports: - Assistant
Now I need to add the import for the new method and update the
createAgenticSystemmethod. Let me first add the import: - Assistant
Good, the import is added. Now let me modify the
createAgenticSystemmethod to use the new method: - Assistant
I see there's an extra closing brace on line 341. Let me fix that:
- Assistant
Good, the
createAgenticSystemmethod is now updated. Now I need to update thecreateSubagentmethod as well. Let me find and update it: - Assistant
Let me update the
createSubagentmethod as well: - Assistant
I notice the
agentBuildervariable is missing in thecreateSubagentmethod. Let me check the original code: - Assistant
I see the issue - the
agentBuildervariable declaration was removed. Let me fix this: - Assistant
I see there's a missing closing brace for the
if (agentExecutor != null)block. Let me fix that: - Assistant
Good, the changes look correct. Now let me check if there are any other usages of
configureAgentin the file: - Assistant
Good, the
configureAgentimport is no longer used. Let me remove it: - Assistant
Now let me verify the changes by looking at the key sections:
- Assistant
Let me also verify the DeclarativeUtil changes:
- Assistant
Good. Now let me verify the code compiles correctly:
- Assistant
Let me try a different approach to compile:
- Assistant
Let me check if Maven is available and try to compile:
- Assistant
Let me try to compile the project.
- Assistant
I accidentally removed the
invokeStaticimport when I removed theconfigureAgentimport. Let me fix that: - Assistant
The
invokeStaticimport is missing. Let me add it back: - Assistant
Now let me try compiling again:
- Assistant
The compilation succeeded. Now let me run the existing tests:
- Assistant
Tests passed. Let me also run tests for the skills module to make sure nothing is broken there:
- Assistant
Good, skills tests pass too. Let me also run the agentic-patterns tests:
- Assistant
All tests pass. Now let me create a test script to verify the fix works for the exact scenario described in the issue:
- Assistant
Let me compile and run the test to verify the fix works:
- Assistant
Let me fix the test class:
- Assistant
Let me compile and run the test:
- Assistant
Let me try to compile and run the test using the Maven classpath.
- Assistant
Now let me run the test:
- Assistant
The test passes! The agent was created successfully without the "Duplicated definition for tool" error. Let me now run the existing tests for the agentic module to make sure nothing is broken:
- Assistant
All tests pass. Let me also check if there are any existing tests for the ToolService that might be affected:
- Assistant
Let me also check if
UntypedAgentBuilderneeds similar fixes: - Assistant
Good, the
UntypedAgentBuilderdoesn't callconfigureAgentagain after construction. Let me also check if there are other callers ofconfigureAgentin the codebase that might be affected: - Assistant
Good, the only place
configureAgentis called is in theAgentBuilderconstructor. Let me verify the final state of both files: - Assistant
Let me also verify the full diff of the changes:
- Assistant
The diff looks clean. Let me run the full test suite one more time to make sure everything is working:
- Assistant
Let me also run the skills tests and the agentic-patterns tests to make sure nothing is broken:
- Assistant
All tests pass. Let me also check if there are any other tests that might be affected:
- Assistant
Let me also run the main langchain4j tests to make sure nothing is broken:
- Assistant
Let me check the exit code of the test run:
- Assistant
All tests pass with exit code 0. Let me clean up the temporary test files and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.