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 file mentioned in the issue. Let's first check the directory.
- Assistant
Good, found the file. Let's open it to examine its contents.
- Assistant
Let me scroll down to find the
createAgenticSystemmethod and understand the issue better. - Assistant
Let me look at the main
createAgenticSystemmethod with all parameters to understand the flow. - Assistant
Let me examine the
agentBuildermethod and theconfigureAgentmethod that's used when building a single agent. - Assistant
Let me look at the
configureAgentmethod in DeclarativeUtil to see how tools are being added. - Assistant
Let me open that file to examine the
configureAgentmethod. - Assistant
Now let me look at the
ToolServiceto understand how tools are registered and where the duplication happens. - Assistant
Let me look at the ToolService to understand the error.
- Assistant
Now I need to understand where the
activate_skilltool is coming from. Let me search for it. - Assistant
Let me find the
Skillsclass and itstoolProvidermethod to understand what's happening. - Assistant
Let me examine the Skills class to understand how the toolProvider works.
- Assistant
Let me look at the
createToolProvidermethod to understand how the ToolProvider is created. - Assistant
Let me look at the
agentBuilder(Class<T>)method more closely. - Assistant
Let me look at the AgentBuilder class to see if it calls
configureAgentin its build method. - Assistant
Let me examine the AgentBuilder class to understand how it handles tool providers and the build method.
- Assistant
Let me search for the
buildmethod in AgentBuilder. - Assistant
Let me look at the build method more closely to see where tools are being added.
- Assistant
Let me look at the
setupToolsmethod to understand how tools are added. - Assistant
Let me examine the
setupToolsmethod more closely. - Assistant
Now let me look at the
toolProvidermethod in AgentBuilder. - Assistant
Let me look at how
agentBuilder(Class<T>)creates the AgentBuilder - maybe the constructor callsconfigureAgentautomatically. - Assistant
Now I found the issue! In the
AgentBuilderconstructor, it callsconfigureAgent(agentServiceClass, this)at line 141. Then increateAgenticSystem, after creating the agentBuilder, it callsconfigureAgentagain at line 338. This causes the tool provider to be added twice.Let me verify this by looking at the
createAgenticSystemmethod again. - Assistant
I can see the issue. Let me look at the
createSubagentsmethod too. - Assistant
Let me look at the createSubagents method:
- Assistant
Now I understand the root cause of the issue. Let me look at the full
configureAgentmethod in DeclarativeUtil to understand all registrations that happen. - Assistant
Let me understand the root cause. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which registers tools and tool providers. ThencreateAgenticSystemcallsconfigureAgentagain, causing double registration. Let me look at howconfigureAgentis called in the constructor and fromcreateAgenticSystem: - Assistant
Now I understand the root cause. Let me trace through the execution flow:
createAgenticSystemcallsagentBuilder(agentServiceClass)which creates a newAgentBuilder- The
AgentBuilderconstructor (line 141) callsconfigureAgent(agentServiceClass, this)which registers all tool providers - Then
createAgenticSystem(line 338) callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again - This double-registers everything including the tool provider that provides
activate_skill
Let me look at how
agentBuilder(Class)is used elsewhere to understand the impact of changes. - Assistant
Now I have a clear understanding of the bug. Let me also look at line 595-600 in AgenticServices.java to see the
createSubagentmethod: - Assistant
Now I fully understand the issue. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which registers all tool providers. ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate registration of theactivate_skilltool.Let me check what
AgentConfiguratordoes: - Assistant
Now I understand the root cause. The
AgentBuilderconstructor (line 141) callsconfigureAgent(agentServiceClass, this)which registers all@ToolProviderSupplierannotated methods. ThencreateAgenticSystem(line 338) callsconfigureAgentagain, causing double registration of the same tool provider.Let me first create a reproduction script to verify the issue.
- Assistant
Let me look at existing tests for the agentic services to understand how to write a test:
- Assistant
Let me look at what the
AgentBuilderconstructor does and confirm the double registration issue: - Assistant
Now I have a clear picture. The
AgentBuilderconstructor at line 141 callsconfigureAgent(agentServiceClass, this)which registers all tool providers (including theactivate_skilltool from Skills). ThencreateAgenticSystemat line 338 callsconfigureAgentagain, causing duplicate registration.Let me look at what all
configureAgentdoes to understand which parts need the second call: - Assistant
Now I have a clear understanding of the issue. Let me look at all the places where
agentBuilder()is followed byconfigureAgent(): - Assistant
Now I understand the root cause. The
AgentBuilderconstructor (line 141) already callsconfigureAgent(agentServiceClass, this), which registers all tool providers including the one providingactivate_skill. ThencreateAgenticSystem(line 338) callsconfigureAgentagain, causing duplicate registration.Let me also check the
createSubagentmethod at line 597-598, which has the same pattern: - Assistant
Now I have a clear understanding of the bug. The
AgentBuilderconstructor (line 141) callsconfigureAgent(agentServiceClass, this)which registers all tool providers. ThencreateAgenticSystem(line 338) andcreateSubagent(line 598) callconfigureAgentagain, causing duplicate registration of tool providers (including theactivate_skilltool).Let me now implement the fix. The approach is: since the
AgentBuilderconstructor already callsconfigureAgent, the subsequent calls increateAgenticSystemandcreateSubagentshould NOT call the fullconfigureAgentagain. Instead, they should only apply thechatModel(for subagents that need it) and theagentConfigurator. - Assistant
Let me now look at the full picture to understand the exact issue:
- Assistant
Now I understand the full picture. The
AgentBuilderconstructor at line 141 already callsconfigureAgent(agentServiceClass, this)which registers all tool providers (includingactivate_skillfrom Skills). ThencreateAgenticSystemat line 338 andcreateSubagentat line 598 callconfigureAgentagain, causing double registration.The fix should be to not call
configureAgenta second time, since the constructor already does it. Instead, only apply theagentConfiguratorand handle thechatModelfor subagents that don't have their own@ChatModelSupplier.Let me implement the fix:
- Assistant
Let me look at how
agentBuilderis used in AgentUtil: - Assistant
Now I have a complete understanding. Let me trace the exact flow:
createAgenticSystem(FooAgent.class)→agentBuilder(agentServiceClass)createsAgentBuilderwhose constructor callsconfigureAgent(agentServiceClass, this)(line 141)- This calls
DeclarativeUtil.configureAgent(agentType, null, true, agentBuilder, AgentConfigurator.empty())which registers the tool provider from@ToolProviderSupplier - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)(line 338) which callsDeclarativeUtil.configureAgentAGAIN, re-registering the same tool provider
The fix: Don't call
configureAgenta second time increateAgenticSystemandcreateSubagent. TheAgentBuilderconstructor already handles it. We just need to apply theagentConfiguratorseparately. - Assistant
Let me look at
DefaultDeclarativeAgentCreationContextto understand how to apply the agentConfigurator: - Assistant
Now let me implement the fix. I need to modify
createAgenticSystem(line 338) andcreateSubagent(line 598) to not callconfigureAgentagain, and instead only apply theagentConfigurator: - Assistant
Now let me look at the imports to understand what's available:
- Assistant
Now I have all the information I need. Let me implement the fix. The issue is that
AgentBuilderconstructor (line 141) already callsconfigureAgent(agentServiceClass, this)which registers all tool providers. ThencreateAgenticSystem(line 338) andcreateSubagent(line 598) callconfigureAgentagain, causing duplicate registration.The fix: Remove the second
configureAgentcall and only apply theagentConfigurator(and optionally set thechatModelif explicitly provided): - Assistant
I see the edit was applied but the
declarativeChatModelmethod got slightly mangled. Let me check: - Assistant
Let me see the full context around the
declarativeChatModelmethod: - Assistant
I see the issue - the
declarativeChatModelmethod definition got corrupted. Let me fix it: - Assistant
Good. Now I need to also fix the
createSubagentmethod. Let me look at it: - Assistant
Now let me fix the
createSubagentmethod as well: - Assistant
Let me check if the file looks correct after both edits:
- Assistant
I see there's an issue - the
createBuiltInAgentExecutormethod definition got corrupted. Let me check what it should look like: - Assistant
I see the issue - there's a dangling
returnstatement from the original publiccreateBuiltInAgentExecutormethod. Let me fix it: - Assistant
Now let me add the missing blank line between
createSubagentsandcreateSubagent: - Assistant
Good, the file looks correct now. Let me verify the complete changes by looking at both modified sections:
- Assistant
Now let me verify the
createSubagentsection too: - Assistant
Good. Now let me check if there are any remaining usages of
configureAgentin the file that might need the import: - Assistant
The
configureAgentimport is only referenced in comments now. Let me remove the unused import: - Assistant
I see I accidentally created a duplicate import. Let me fix that:
- Assistant
Good. Now let me verify the complete file is syntactically correct by checking for compilation errors:
- Assistant
Let me check if Maven is available and compile the affected module:
- Assistant
There's a type inference issue in
createSubagentbecausesubgentClassisClass<?>. Let me fix this by using raw type like the original code inDeclarativeUtil: - Assistant
Let me try to compile again:
- Assistant
The compilation is successful. Now let me run the existing tests to make sure nothing is broken:
- Assistant
The test failures show an issue: "One and only one of chatModel, streamingChatModel, or their Function variants can be set for agent 'say'." This happens because the
AgentBuilderconstructor already sets the chatModel from@ChatModelSupplier, and then we're setting it again withagentBuilder.chatModel(chatModel), causing a conflict.Let me look at the failing test to understand the issue:
- Assistant
Let me look at how
AgentBuilder.chatModel()works to understand the conflict: - Assistant
Let me look at the chatModel setter and validation logic:
- Assistant
I see the problem. The
AgentBuilderconstructor already sets the chatModel from@ChatModelSupplierannotation (which might set it as achatModelProviderfunction). Then my code sets it again viaagentBuilder.chatModel(chatModel), causing two chatModel configurations. I need to only set the chatModel when the agent doesn't already have a@ChatModelSupplieror@StreamingChatModelSupplier.Let me check what annotations exist:
- Assistant
I see the issue now. When the
AgentBuilderconstructor'sconfigureAgentalready sets a chatModel (from@ChatModelSupplier), my fix incorrectly sets it again. I need to only set the chatModel when the agent doesn't already have one configured from annotations.Let me look at the test to understand the scenario better:
- Assistant
I see the issue now. When the
AgentBuilderconstructor'sconfigureAgentalready configures the chat model from@ChatModelSupplier, my code incorrectly sets it again. The problem is that@ChatModelSupplierwith parameters sets achatModelProviderfunction, and thenagentBuilder.chatModel(chatModel)sets a plainmodelfield - both are non-null, causing the validation error.The fix should only set the chat model when the agent doesn't have a
@ChatModelSupplieror@StreamingChatModelSupplierannotation. Let me update the fix: - Assistant
I need a different approach. The issue is that when the
AgentBuilderconstructor already sets the chat model (from@ChatModelSupplier), my code shouldn't override it. Let me update the fix to only set chatModel when the agent class doesn't have a@ChatModelSupplieror@StreamingChatModelSupplier: - Assistant
I see there's a duplicate
if (agent == null)line. Let me fix that: - Assistant
Good. Now let me also fix the
createSubagentmethod with the same pattern: - Assistant
Let me also fix the
createSubagentmethod with the same pattern: - Assistant
I see there's still duplicate code in the
createSubagentmethod. Let me remove the old duplicate lines: - Assistant
Good. Now let me compile and run the tests again:
- Assistant
Good, compilation succeeds. Now let me run the tests:
- Assistant
All 45 tests pass. Let me also run the Skills tests since that's where the
activate_skilltool comes from: - Assistant
All tests pass. Let me also verify the complete diff of my changes:
- Assistant
The diff looks clean and correct. Let me now run the broader test suite to make sure nothing else is broken:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.