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 get an overview.
- Assistant
Good, I found the file. Let's also check for the error message about duplicate definition for tool: activate_skill. Let's search for that in the codebase.
- Assistant
Let me examine the key files involved in this bug.
- Assistant
Let me first understand the repository structure and find the relevant files mentioned in the issue.
- Assistant
Let me read the AgenticServices.java file to understand how
createAgenticSystemworks. - Assistant
Let me look at the full AgenticServices.java file, particularly the
createAgenticSystemmethod: - Assistant
Let me see the whole file.
- Assistant
Let me search for the
createAgenticSystemmethod: - Assistant
Let me read the
createAgenticSystemmethod implementations: - Assistant
Let me look at the
AgentBuilderclass and how tool providers are handled: - Assistant
Let me examine the AgentBuilder.java file to understand how tools are configured and how tool providers are handled.
- Assistant
Now let me look at
DeclarativeUtil.configureAgentmethod which is called in both places: - Assistant
Let me check where
AgentBuilderis constructed in the codebase: - Assistant
Let me check line 597 and 100:
- Assistant
Let me check the AgentUtil line 100:
- Assistant
Let me now look at the full
createAgenticSystemwith 4 parameters andcreateSubAgentto understand the pattern better: - Assistant
Let me check the full
configureAgentin DeclarativeUtil more carefully: - Assistant
Now let me understand the full picture. Let me look at the
AgentBuilderconstructor and thecreateSubAgentmethod: - Assistant
Now I see the issue clearly. Let me trace through the code path:
createAgenticSystem(FooAgent.class)callscreateAgenticSystem(agentServiceClass, agentConfigurator)- Which calls
createAgenticSystem(agentServiceClass, declarativeChatModel(agentServiceClass), agentConfigurator) - In that method, since FooAgent doesn't have @SequenceAgent, @LoopAgent, etc.,
createComposedAgentreturns null - Then it creates
agentBuilder = agentBuilder(agentServiceClass)which callsnew AgentBuilder<>(agentServiceClass)- this constructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSupplierand adds the tool provider - Then it calls
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)AGAIN, which processes@ToolProviderSuppliera second time!
This causes the same ToolProvider (which includes
activate_skill) to be added twice. Let me verify this by looking at the AgentBuilder constructor: - Assistant
Let me look at the AgentBuilder constructor to see where
configureAgentis called: - Assistant
Let me trace through the exact issue. First, let me see how the AgentBuilder constructor calls configureAgent:
- Assistant
Now I understand the issue clearly. Let me trace through the code path to confirm the bug:
createAgenticSystem(FooAgent.class)callscreateComposedAgentwhich returns null- Then it calls
agentBuilder(agentServiceClass)which createsnew AgentBuilder<>(agentServiceClass) - The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSupplierand adds the tool provider to the builder - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)AGAIN which processes@ToolProviderSuppliera second time, adding the same tool provider twice
This causes the
activate_skilltool to be registered twice. Let me also look at the same pattern increateSubAgent: - Assistant
Now let me look at
UntypedAgentBuilderto see if it has a similar pattern: - Assistant
Let me look at the full AgentBuilder constructor to understand all the initialization that happens:
- Assistant
Let me also look at the
ToolService.javato understand the duplicate detection: - Assistant
Now I have a clear picture of the bug. Let me check all the callers of
agentBuilder()and the AgentBuilder constructor: - Assistant
Now I understand the complete picture. The issue is:
createAgenticSystemcallsagentBuilder(agentServiceClass)which createsnew AgentBuilder<>(agentServiceClass)- The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- this processes@ToolProviderSupplierand adds the tool provider - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again - this processes@ToolProviderSuppliera second time
The same double-configuration happens in
createSubagent. Let me now create a fix. The approach is to add a package-private constructor toAgentBuilderthat doesn't auto-configure, and use it in internal methods to avoid double configuration. - Assistant
Now I have a clear understanding. Let me look for any tests related to the issue:
- Assistant
Let me look at the existing test for skills to understand the pattern:
- Assistant
Let me look at how Skills creates the tool provider:
- Assistant
Now I have a clear understanding of the bug. The issue is:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSupplierand adds the tool provider- Then
createAgenticSystem(andcreateSubagent) callconfigureAgent(...)again, which processes@ToolProviderSuppliera second time, causing duplicate tool registration
The fix is to avoid the double registration. Let me look at the code to understand the flow better and implement the fix:
- Assistant
Let me look at the
setupToolsmethod to understand how tools are registered: - Assistant
Let me look at the setupTools method to understand how tools and tool providers are registered:
- Assistant
Let me look at the full setupTools method:
- Assistant
Now I have a thorough understanding. Let me create a reproduction test first to confirm the issue, then implement the fix.
- Assistant
Now I have a clear understanding of the issue. Let me trace through the exact problem:
createAgenticSystem(FooAgent.class)→ callsagentBuilder(agentServiceClass)which createsnew AgentBuilder<>(agentServiceClass)- The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- this processes@ToolProviderSupplierand adds the tool provider (includingactivate_skill) - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)AGAIN - this processes@ToolProviderSuppliera second time, causing duplicateactivate_skill
The same issue exists in
createSubagent. The fix: removeconfigureAgentfrom theAgentBuilderconstructor, and instead call it explicitly in the factory methods that need it. - Assistant
Now I understand the root cause. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)at line 141, which processes@ToolProviderSupplierand adds the tool provider. Then increateAgenticSystem(line 337-338),agentBuilder(agentServiceClass)creates the builder (triggering the constructor'sconfigureAgent), and thenconfigureAgentis called AGAIN, causing duplicate tool registration.The fix: Remove
configureAgentfrom theAgentBuilderconstructor, and call it explicitly in the publicagentBuilder(Class)factory method. Internal methods likecreateAgenticSystemandcreateSubagentwill create the builder directly and callconfigureAgentonly once.Let me implement the fix:
- Assistant
Now I'll implement the fix. First, remove the
configureAgentcall from theAgentBuilderconstructor: - Assistant
Good, the
configureAgentcall has been removed from the constructor. Let me verify: - Assistant
Good, the
configureAgentcall has been removed from the constructor. Now I need to updateAgenticServices.javato callconfigureAgentin theagentBuilder(Class)factory method so the public API still works: - Assistant
Now I need to update the
agentBuilder(Class)method to callconfigureAgent: - Assistant
Now I need to update the
agentBuilder(Class)method in AgenticServices to callconfigureAgentafter creating the builder. Let me edit it: - Assistant
Now I need to update
createAgenticSystemandcreateSubagentto NOT useagentBuilder()(which now callsconfigureAgent), but instead create the builder directly and callconfigureAgentonly once. Let me update them: - Assistant
Let me update
createAgenticSystemto usenew AgentBuilder<>(agentServiceClass)directly instead ofagentBuilder(agentServiceClass): - Assistant
I see there's a duplicate
if (agent == null) {line. Let me fix that: - Assistant
Let me fix the duplicate if block:
- Assistant
Good. Now I need to update the
createSubagentmethod as well. Let me find and update it: - Assistant
Now let me update
createSubagentto usenew AgentBuilder<>(subgentClass)instead ofagentBuilder(subgentClass): - Assistant
I see there's a duplicate return statement. Let me fix that:
- Assistant
Now I need to check if the
configureAgentimport inAgentBuilder.javais still needed (it's used in theagentBuildermethod inAgenticServices.javabut not inAgentBuilder.javaanymore): - Assistant
The
configureAgentimport inAgentBuilder.javais no longer used. Let me remove it: - Assistant
The import of
configureAgentinAgentBuilder.javais no longer needed since we removed the call from the constructor. Let me remove it. - Assistant
I see there's a duplicate import line. Let me fix that:
- Assistant
Good, the imports are now clean. Let me verify the changes look correct by checking the final state of both files:
- Assistant
Good - no more references to
configureAgentinAgentBuilder.java. Let me verify the AgenticServices.java changes: - Assistant
Good, the changes look correct. Let me verify all the key areas are properly updated:
- Assistant
Let me also check
AgentUtil.javaline 100 whereagentBuilderis also used: - Assistant
Good,
AgentUtil.javausesAgenticServices.agentBuilder(c).build()which now correctly callsconfigureAgentonce.Let me now verify the complete picture by checking the
UntypedAgentBuildertoo - it extendsAgentBuilderand its constructor callssuper(UntypedAgent.class): - Assistant
Good,
UntypedAgentBuilderextendsAgentBuilderand callssuper(UntypedAgent.class). Since we removedconfigureAgentfrom the constructor, this is fine -UntypedAgentis for untyped agents where the user manually configures everything.Now let me build the project and run the relevant tests to verify the fix works:
- Assistant
Good, compilation succeeded. Let me now run the tests in the agentic module to verify:
- Assistant
Let me check the test results more carefully:
- Assistant
Let me run the tests with more verbose output to see if they pass:
- Assistant
All 45 tests pass. Let me also run the skills module tests to make sure we haven't broken anything:
- Assistant
All tests pass. Let me now create a simple test to verify the fix specifically addresses the duplicate tool provider issue:
- Assistant
Let me look at the git diff to review all changes:
- Assistant
The diff looks correct. Let me also run the tests one more time to make sure everything is stable:
- Assistant
Let me run the tests again with more verbose output to confirm the results:
- Assistant
All tests pass (77 in skills module, 45 in agentic module). Let me clean up the temp file:
- Assistant
Let me verify the final state of the changes one more time:
- Assistant
Now let me provide my final summary. Let me also verify the key files are in the correct state:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.