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
codeCaused 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
java - Assistant
I'll start by exploring the repository to understand the structure and locate the relevant files. Let's first check the current directory and find the file mentioned in the issue:
langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgenticServices.java. - Assistant
Let me open this file and understand its structure.
- Assistant
Let me look at the
createAgenticSystemmethod to understand the issue. - Assistant
Let me look at the
createAgenticSystemmethod that takesAgentConfiguratoras it's mentioned in the issue. - Assistant
Let me look at the
configureAgentmethod and how tool providers are registered. - Assistant
Now let me look at the
configureAgentmethod inDeclarativeUtil.java. - Assistant
Let me examine the DeclarativeUtil.java file to understand how configureAgent works and how tools are being registered.
- Assistant
Let me continue reading the file to see the rest of the
configureAgentmethod. - Assistant
Now let me search for the
activate_skilltool to understand where it's being added. This is likely part of the Skills/ToolProvider implementation. - Assistant
Let me look at how
agentBuilderis used and how the tool provider is set up. - Assistant
Now let me look at the
agentBuilder(Class<T>)method: - Assistant
Now let me look at how the AgentBuilder handles tool providers. Let me find the AgentBuilder class:
- Assistant
Let me check the AgentBuilder class to understand how tool providers are set up and if there's duplication happening there.
- Assistant
Let me look at the
ToolServiceto understand where the error occurs: - Assistant
Let me check the ToolService class to understand the error.
- Assistant
Now I understand the error! The issue is in
ToolService.addToolswhere it checks for duplicate tool names. Let me look at how Skills creates its ToolProvider to understand theactivate_skilltool: - Assistant
Now I need to understand how the
createAgenticSystemmethod is registering tool providers. Let me look at the AgentBuilder class to see how it handles tool providers: - Assistant
Let me now look at the
createComposedAgentmethod to see how it handles sub-agents: - Assistant
Now let me look at the
createAgenticSystemmethod that takesAgentConfigurator: - Assistant
Let me now look at the
build()method in AgentBuilder to see how it processes tool providers: - Assistant
Let me look at the
setupToolsmethod in AgentBuilder: - Assistant
Let me now look at the
Skills.javafile to understand how it creates a tool provider: - Assistant
Now I need to understand the exact flow when using
createAgenticSystem. Let me look more closely at howconfigureAgentworks and if there's any duplication: - Assistant
Now I need to understand the exact flow when using
createAgenticSystem. Let me check if there's any automatic registration of tool providers. Let me look at the Skills class more carefully: - Assistant
Let me look at
ToolService.createContextFromStaticToolsAndProvidersmore carefully: - Assistant
Now I need to understand why the tool provider is being registered multiple times. Let me look at the AgentBuilder.build() method more carefully to see if there's any issue with how it handles tool providers:
- Assistant
Let me look at how AiServices handles tool providers:
- Assistant
Let me check the AiServices.toolProviders() method:
- Assistant
Let me look at the ToolService.toolProviders() method:
- Assistant
Let me look at the ToolService.toolProviders method:
- Assistant
Let me look at how
AgentInvocationHandlerworks to see if it might be duplicating tools: - Assistant
Let me examine the AgentInvocationHandler to see if it's duplicating tool registrations.
- Assistant
Let me look at the AgentBuilder constructor:
- Assistant
Let me look at the AgentBuilder constructor to understand how it initializes.
- Assistant
Let me look at the constructor to see if it adds any tools automatically.
- Assistant
Now I see the issue! In the
AgentBuilderconstructor, it callsconfigureAgent(agentServiceClass, this)on line 142. This adds the tool provider from@ToolProviderSupplierto the builder. But then increateAgenticSystem, when it falls back to creating an agent withagentBuilder(agentServiceClass), it creates a newAgentBuilderinstance which already callsconfigureAgentin its constructor. ThencreateAgenticSystemcallsconfigureAgentagain, causing the tool provider to be registered twice!Let me verify this by looking at the exact flow:
- Assistant
Now I see the problem! When
agentBuilder(agentServiceClass)is called, it creates an AgentBuilder which in its constructor callsconfigureAgent(agentServiceClass, this)(line 142). This adds the tool provider from@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which adds the tool provider a second time!Let me verify this by looking at both
configureAgentcalls: - Assistant
Now I understand the issue! The problem is that the
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which adds the tool provider from@ToolProviderSupplier. Then increateAgenticSystem, it callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which adds the same tool provider a second time!Let me look at how
AgentBuilder.build()processes these tool providers to understand where the duplication occurs: - Assistant
Let me check the build method to see how toolProviders is passed to aiServices
- Assistant
Let me search for other places that call
new AgentBuilderoragentBuilder: - Assistant
Let me try a different approach:
- Assistant
Let me now look at how
buildSubagentmethods work in AgenticServices: - Assistant
Let me verify what happens in the constructor. Let me see the exact flow:
- Assistant
Now I understand the issue! Let me trace through the flow:
AgenticServices.createAgenticSystem(FooAgent.class)is called- This calls
createAgenticSystem(FooAgent.class, declarativeChatModel(FooAgent.class), AgentConfigurator.empty()) - Since there's no composed agent annotation, it falls through to:
java
var agentBuilder = agentBuilder(agentServiceClass); configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator); agent = agentBuilder.build(); agentBuilder(agentServiceClass)creates a newAgentBuilder, which in its constructor callsconfigureAgent(agentServiceClass, this)(line 142)- This adds the ToolProvider from
@ToolProviderSupplierto the agent builder - Then
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called AGAIN - This adds the SAME ToolProvider a second time!
Let me verify this by looking at both calls:
- Assistant
Now I understand the issue! The problem is clear:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which adds theToolProviderfrom@ToolProviderSupplier- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)which adds the SAMEToolProvideragain!
This causes the duplicate
activate_skilltool error. Let me verify this by looking at how tools are added in AgentBuilder: - Assistant
Now I understand the root cause. Let me trace through the execution:
AgenticServices.createAgenticSystem(FooAgent.class)createComposedAgent()returnsnull(no workflow annotations)agentBuilder(FooAgent.class)createsnew AgentBuilder<>(FooAgent.class)- AgentBuilder constructor calls
configureAgent(agentServiceClass, this)→ adds ToolProvider from@ToolProviderSupplier createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)→ adds SAME ToolProvider again!
The ToolProvider is added twice, causing the duplicate
activate_skilltool error. Let me now look at how to fix this.Let me first check if there's a way to verify my understanding:
- Assistant
Now let me understand the full picture. I need to see how
createAgenticSystemhandles the simple agent case. Let me also look at howagentBuilder.build()usesagentConfigurator: - Assistant
The
agentConfiguratoris not used inAgentBuilder. Let me look at howconfigureAgentinDeclarativeUtiluses theagentConfiguratorparameter. - Assistant
Now I understand the issue clearly. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which adds the ToolProvider. ThencreateAgenticSystemcallsconfigureAgentagain with different parameters (chatModel, agentConfigurator), which adds the same ToolProvider a second time.The
agentConfiguratoris only used at line 148 of DeclarativeUtil as a callback:agentConfigurator.configurator().accept(...).The fix should be to avoid calling
configureAgenttwice. Let me look at the exact code to understand what needs to change: - Assistant
Let me look at how the
agentBuilder.tools()methods work in AgentBuilder: - Assistant
Let me look at the full private
configureAgentmethod to understand all the things it does: - Assistant
Now I have a complete understanding of the issue. The problem is clear:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)(line 142) which adds theToolProviderfrom@ToolProviderSupplier- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)which adds the SAMEToolProvidera second time
This causes the "Duplicated definition for tool: activate_skill" error.
Let me check if there are existing tests to understand the expected behavior:
- Assistant
Let me look at the existing test to understand how it's used:
- Assistant
Now I have a clear understanding. The issue is:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)→ adds ToolProvider- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)→ adds SAME ToolProvider again
The fix should be to avoid the double registration. Let me look at the approach I should take. The simplest and most correct fix is to not call
configureAgenttwice - the AgentBuilder constructor already does it. But we need thechatModelandagentConfiguratorto still be applied.Let me look at how we can solve this. I'll check if there's a way to add an internal method to AgentBuilder that resets its tool state:
- Assistant
Now I understand the issue completely. The fix should be to ensure that when
configureAgentis called twice (once in the AgentBuilder constructor and once increateAgenticSystem), it doesn't add duplicate tool providers. The simplest and most correct approach is to remove the duplicateconfigureAgentcall fromcreateAgenticSystemand handle the chatModel and agentConfigurator differently.Let me look at the approach I should take:
- Assistant
Now I have a clear understanding of the issue. Let me create a fix. The problem is:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)(line 142) which adds the ToolProvidercreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)(line 338) which adds the SAME ToolProvider again
The fix is to avoid the double registration. Since
createAgenticSystemusesagentBuilder(agentServiceClass)which already callsconfigureAgent, the second call is redundant for tools/toolProviders. The second call only adds thechatModelparameter and applies theagentConfigurator.Let me implement the fix:
- Assistant
Now I understand the issue clearly. Let me look at what happens with the tools when
build()is called: - Assistant
Now I have a clear understanding of the issue. Let me explain the root cause:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)(line 142) which adds theToolProviderfrom@ToolProviderSuppliercreateAgenticSystemthen callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)(line 338) which adds the SAMEToolProvideragain
The fix should ensure we don't call
configureAgenttwice. Since theAgentBuilderconstructor already configures everything viaconfigureAgent, the call increateAgenticSystemis redundant for tool registration. The only things thatcreateAgenticSystem's call adds beyond what the constructor does are:- The
chatModelparameter (as fallback if no@ChatModelSupplieris found) - The
agentConfiguratorcallback
Let me implement the fix. The simplest and most correct approach is to not call
configureAgentfromcreateAgenticSystemsince the AgentBuilder constructor already does it. But we need to handlechatModelandagentConfigurator.Let me look at the approach more carefully:
- Assistant
Now I have a complete understanding of the issue. Let me think about the best fix approach.
The problem is clear:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which adds theToolProvidercreateAgenticSystemthen callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)which adds the SAMEToolProvideragain
The most correct fix is to not call
configureAgenttwice. Since theAgentBuilderconstructor already callsconfigureAgent, the second call increateAgenticSystemis redundant for tools/toolProviders. The only things the second call adds are thechatModelparameter and theagentConfiguratorcallback.Let me implement the fix. I'll modify
createAgenticSystemto avoid the redundantconfigureAgentcall: - Assistant
Wait, I need to be more careful. Let me check what
configureAgentdoes beyond just adding tools and tool providers. It also handles the chat model, content retriever, retrieval augmentor, chat memory, listener, and agentConfigurator. Let me verify that my fix handles the chat model correctly: - Assistant
Now I see a potential issue with my fix. The
configureAgentmethod handles the chat model as a fallback - it only uses the provided chatModel if the agent doesn't have@ChatModelSupplieror@StreamingChatModelSupplier. My current fix unconditionally sets the chatModel which would override the @ChatModelSupplier. Let me fix this: - Assistant
Wait, I need to be more careful with the chat model handling. The
configureAgentmethod only uses the provided chatModel as a fallback if there's no@ChatModelSupplieror@StreamingChatModelSupplierannotation. My current fix would override the @ChatModelSupplier. Let me fix this: - Assistant
Now let me check if the
createSubagentmethod has the same issue: - Assistant
I see the same pattern in
createSubagent. Let me fix that too: - Assistant
Let me apply the same fix to
createSubagent: - Assistant
I see there's a duplicate line. Let me fix that:
- Assistant
Let me verify the full
createSubagentmethod: - Assistant
Let me check if there are any other places in the code that have the same pattern:
- Assistant
Let me search for other calls to configureAgent in AgenticServices.java:
- Assistant
Good, both
configureAgentcalls have been removed. Now let me check if the import forconfigureAgentis still needed (it may still be used elsewhere): - Assistant
The
configureAgentimport is no longer used. Let me clean it up: - Assistant
Let me now verify the changes look correct. Let me check the current state of the file:
- Assistant
Now let me also check if there are any other callers of
configureAgentfromDeclarativeUtil: - Assistant
Let me check if the import is still needed:
- Assistant
Good, the import is no longer needed. Let me remove it:
- Assistant
Now let me check that we have the right imports for the new code. We need
getAnnotatedMethodOnClassandStreamingChatModelSupplier: - Assistant
I need to add the
StreamingChatModelSupplierimport. Let me check if it's already imported: - Assistant
Let me check the existing declarative imports:
- Assistant
I need to add the
StreamingChatModelSupplierimport. Let me check if it exists: - Assistant
Let me add the missing import:
- Assistant
Now let me run the existing tests to make sure my fix doesn't break anything:
- Assistant
Let me compile the changed module to verify there are no compilation errors:
- Assistant
The compilation succeeded. Now let me run the existing tests to verify my fix doesn't break anything:
- Assistant
Let me also check if there are integration tests that specifically test
createAgenticSystem: - Assistant
Now let me also check the Skills tests to make sure they still pass:
- Assistant
All tests pass. Now let me verify the complete diff of my changes:
- Assistant
Let me now also check if there's a
ChatModelSupplierimport for thegetAnnotatedMethodOnClasscall in the fix: - Assistant
Let me run the tests one more time to make sure everything is solid:
- Assistant
Let me also check other modules that might be affected:
- Assistant
All tests pass. Let me now verify the final state of my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.