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:
- LangChain4j version: latest agentic snapshot
- LLM(s) used: Qwen 3.6 (via OpenAiChatModel)
- Java version: 25
- Spring Boot version (if applicable): N/A
Additional context The bug is caused by
createAgenticSystemregistering non-dynamicToolProviderinstances multiple times. Theactivate_skilltool is automatically added for everySkillProviderinstance, leading to duplicate registration even with a single agent.Interface
Type: Function Name: createAgenticSystem Path: langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgenticServices.java Input: Class agentServiceClass, AgentConfigurator agentConfigurator Output: T (the created agentic system instance of the requested type) Description: Creates an agentic system from a declarative agent service class, applying the provided configurator to each agent. This overload resolves the default chat model from the agent class and deduplicates tool registrations across the system.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /langchain4j, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand the codebase and then locate the relevant files mentioned in the issue.
The issue is about a duplicate
activate_skilltool definition error when usingAgenticServices.createAgenticSystem(). The bug is caused by registering non-dynamicToolProviderinstances multiple times.Let me first explore the repository structure and then look at the
AgenticServices.javafile.Assistant
Let me explore the AgenticServices.java file first since that's where the createAgenticSystem method is located.
Assistant
Let me search for the
createAgenticSystemmethod specifically:Assistant
Now I need to understand the issue better. Looking at
createAgenticSystemmethod around line 330, when the agent is not a composed agent, it falls through to creating the agent usingagentBuilder. Let me look at theAgentBuilderclass and how it handles tool providers:Assistant
Now let me look at the
DeclarativeUtil.configureAgentmethod to understand how tool providers are configured:Assistant
Now I can see the issue. Looking at the
configureAgentmethod inDeclarativeUtil.java, line 55 callsconfigureAgent(agentType, null, true, agentBuilder, AgenticServices.AgentConfigurator.empty());which is called by the constructor ofAgentBuilder. Then, whencreateAgenticSystemcallsconfigureAgentagain, it would register theToolProvideragain!Let me trace the flow:
createAgenticSystem()is called- It creates an
AgentBuilderviaagentBuilder(agentServiceClass) - In
AgentBuilderconstructor,configureAgent(agentServiceClass, this)is called, which registers tool providers - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again! - This causes the tool provider to be registered twice!
Let me verify this:
Assistant
Now I see the issue clearly:
- In line 337,
agentBuilder(agentServiceClass)creates a newAgentBuilderinstance - The
AgentBuilderconstructor (line 123 in AgentBuilder.java) callsconfigureAgent(agentServiceClass, this)which already registers the tool providers - Then on line 338,
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again, which registers the same tool providers again!
This causes duplicate registration of
ToolProviderinstances, leading to theactivate_skilltool being registered multiple times.Let me look at the AgentBuilder constructor more closely:
- In line 337,
Assistant
Now I understand the issue fully. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)on line 141, which registers tool providers. Then inAgenticServices.createAgenticSystem(), line 338 callsconfigureAgent()again, causing duplicate registration.The fix should be in
AgenticServices.createAgenticSystem()- we should NOT callconfigureAgentagain after creating theAgentBuilder, since the constructor already calls it.Let me verify this understanding by checking if there are tests:
Assistant
Let me look at the test file to understand what kind of agents are being tested and see if any of them use
@ToolProviderSupplier:Assistant
Let me search more broadly for tests related to ToolProviderSupplier:
Assistant
Let me now look at how the fix should be applied. The issue is that
configureAgentis called twice:- Once in the
AgentBuilderconstructor - Once in
createAgenticSystemafter creating the builder
The fix should be to remove the redundant call to
configureAgentincreateAgenticSystem.Let me verify that the
AgentBuilderconstructor already sets up the chat model and configurator properly:- Once in the
Assistant
Looking at the two
configureAgentoverloads:configureAgent(agentType, agentBuilder)- calls the private method withchatModel=null,allowNullChatModel=true, andagentConfigurator=empty()configureAgent(agentType, chatModel, agentBuilder, agentConfigurator)- calls the private method with the actualchatModelandagentConfigurator
So when
createAgenticSystemis called:- First
configureAgentis called inAgentBuilderconstructor withchatModel=nulland empty configurator - Then
configureAgentis called again with the actualchatModelandagentConfigurator
The issue is that:
- Tool providers are registered in BOTH calls
- The ChatModel from the first call (null) is overridden by the second call, which is fine
- But the ToolProvider is added twice, causing the duplicate error
The fix should be to NOT call
configureAgentagain increateAgenticSystemsince theAgentBuilderconstructor already does it. But wait, we need to pass thechatModelandagentConfiguratorsomehow.Let me check if the
AgentBuilderconstructor version can be modified to accept these parameters, or if we should pass them differently:Assistant
I see the same pattern in
createBuiltInAgentExecutorat line 597-598. This would also have the duplicate registration issue.Let me think about the proper fix. The issue is:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- Then
createAgenticSystemandcreateBuiltInAgentExecutorcallconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again
There are two approaches:
- Remove the
configureAgentcall from theAgentBuilderconstructor - but this would break other usages - Remove the
configureAgentcall fromcreateAgenticSystemandcreateBuiltInAgentExecutor- but then we need to passchatModelandagentConfiguratorto the constructor
Looking at it more carefully, I think the best approach is to create a new constructor or factory method for
AgentBuilderthat acceptschatModelandagentConfigurator, or to modify the existingconfigureAgentmethod to be idempotent.Actually, wait - let me re-read the
AgentBuilderconstructor. It callsconfigureAgent(agentServiceClass, this)which is the overload that useschatModel=nulland empty configurator. So the second call increateAgenticSystemwould set the actual chatModel and apply the configurator. But it also re-registers the tool providers.The simplest fix would be to modify the
AgentBuilderconstructor to NOT callconfigureAgent, and let the caller configure the agent. But this might break other code paths.Let me check if there's a way to pass chatModel and agentConfigurator to AgentBuilder:
Assistant
The best fix is to modify the
AgentBuilderconstructor to acceptchatModelandagentConfiguratorparameters, so thatconfigureAgentis only called once with the correct parameters. This way,createAgenticSystemandcreateBuiltInAgentExecutorcan pass the correct values directly to the constructor.Let me look at how this would work:
Assistant
I think the cleanest fix is to modify the
AgentBuilderclass so that it doesn't callconfigureAgentin its constructor when it's being used fromcreateAgenticSystemorcreateBuiltInAgentExecutor.But actually, looking at the code flow more carefully:
agentBuilder(agentServiceClass)creates anAgentBuilderthat already has tool providers registered (from constructor)configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again, which re-registers the same tool providers
The fix should be to remove the duplicate
configureAgentcall fromcreateAgenticSystemandcreateBuiltInAgentExecutor. But we still need to set thechatModeland apply theagentConfigurator.Wait, let me re-check what the constructor
configureAgentcall does vs what the second call does:The first call:
configureAgent(agentType, null, true, agentBuilder, AgenticServices.AgentConfigurator.empty())- Registers ToolsSupplier
- Registers ToolProviderSupplier
- Registers ContentRetrieverSupplier
- Registers RetrievalAugmentorSupplier
- Registers ChatMemoryProviderSupplier
- Registers ChatMemorySupplier
- Registers ChatModelSupplier (with null as fallback)
- Registers AgentListenerSupplier
- Applies empty agentConfigurator
The second call:
configureAgent(agentType, chatModel, false, agentBuilder, agentConfigurator)- Registers ToolsSupplier again
- Registers ToolProviderSupplier again (THIS IS THE BUG)
- Registers ContentRetrieverSupplier again
- Registers RetrievalAugmentorSupplier again
- Registers ChatMemoryProviderSupplier again
- Registers ChatMemorySupplier again
- Registers ChatModelSupplier (or uses provided chatModel)
- Registers AgentListenerSupplier again
- Applies agentConfigurator
The fix should be to not call
configureAgentin theAgentBuilderconstructor, and instead pass the chatModel and agentConfigurator when constructing the builder. But this would change the API significantly.A simpler approach: Add an overloaded constructor or factory method that takes chatModel and agentConfigurator, and don't call
configureAgentin the original constructor. Instead, only configure in the new constructor/method.But wait, let me think about this differently. The issue is that
createAgenticSystemandcreateBuiltInAgentExecutorcall both the constructor (which callsconfigureAgent) AND then callconfigureAgentexplicitly.The simplest fix is to remove the explicit
configureAgentcalls fromcreateAgenticSystemandcreateBuiltInAgentExecutor, and instead have theAgentBuilderconstructor accept thechatModelandagentConfiguratorparameters.Let me implement this fix:
Assistant
Now I understand the issue clearly. The solution is to add a package-private constructor to
AgentBuilderthat acceptsChatModelandAgentConfigurator, and then use that constructor increateAgenticSystemandcreateBuiltInAgentExecutor. The public constructor will continue to work as before.Actually, let me think about this more carefully. The cleanest fix would be:
- Add a new package-private constructor in
AgentBuilderthat takesChatModelandAgentConfigurator - Use this new constructor in
createAgenticSystemandcreateBuiltInAgentExecutor - This new constructor will call
configureAgentwith the proper parameters instead of the default one
Let me implement this fix:
- Add a new package-private constructor in
Assistant
Now let me check the
AgenticServices.AgentConfiguratortype:Assistant
Now I need to understand the fix better. The issue is that when
createAgenticSystemis called:agentBuilder(agentServiceClass)is called, which creates anAgentBuilder- The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- this registers tool providers - Then
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again - this registers the same tool providers again
The fix is to add a new constructor to
AgentBuilderthat takesChatModelandAgentConfigurator, and use it increateAgenticSystemandcreateBuiltInAgentExecutor.Let me implement this fix:
Assistant
Let me use sed instead:
Assistant
I'll use the
editcommand to make the changes. Let me first open the file and make the changes:Assistant
Now I need to make two changes:
-
In
AgentBuilder.java: Add a new package-private constructor that acceptsChatModelandAgentConfigurator, and modify the public constructor to delegate to it with default values. -
In
AgenticServices.java: UpdatecreateAgenticSystemandcreateBuiltInAgentExecutorto use the new constructor instead of callingconfigureAgentseparately.
Let me also add the necessary import for
AgenticServicesinAgentBuilder.java:-
Assistant
I need to add the import for
AgenticServices. Let me make the changes:Assistant
Good, the import was added. Now let me update the constructor. Let me first check the current state of the constructor:
Assistant
The constructor is at lines 132-159. I need to:
- Replace the public constructor to delegate to a new package-private constructor
- Add the new package-private constructor that takes ChatModel and AgentConfigurator
Assistant
The
AgentBuilder.javafile has been updated. Now I need to updateAgenticServices.javato use the new constructor and remove the redundantconfigureAgentcalls.Let me check the current state of
createAgenticSystemandcreateBuiltInAgentExecutor:Assistant
Now I need to update
AgenticServices.javato use the new constructor. I need to change:- Line 337-338: Use the new constructor instead of calling
configureAgentseparately - Line 597-598: Same change for
createBuiltInAgentExecutor
First, let me also check if the
agentBuildermethod needs to be updated:- Line 337-338: Use the new constructor instead of calling
Assistant
I need to add an overloaded
agentBuildermethod that acceptschatModelandagentConfigurator, or I can modify the code directly increateAgenticSystemto create theAgentBuilderwith the new constructor.Let me update
AgenticServices.java:Assistant
Now I need to update
createAgenticSystemto use the new constructor. Let me find the exact lines:Assistant
Now I'll replace these lines to use the new constructor:
Assistant
I see there's a duplicate closing brace at line 339. Let me fix that:
Assistant
Good, that fixed the
createAgenticSystemmethod. Now I need to also update thecreateBuiltInAgentExecutormethod. Let me find it:Assistant
Now I need to check if there are any unused imports in
AgenticServices.java. Since we removed theconfigureAgentcall from the main code path, I should check if the import is still needed:Assistant
The
configureAgentimport is no longer used inAgenticServices.java, so I should remove it. Let me do that.Assistant
Now let me verify the changes are correct by viewing the key parts of both files:
Assistant
Now let me check the
DeclarativeUtil.javato verify theconfigureAgentmethod that handlesallowNullChatModel:Assistant
I see an issue. When the public
AgentBuilderconstructor is used (which now delegates to the new constructor withnullchatModel), it callsconfigureAgent(agentServiceClass, null, this, agentConfigurator). Looking at theconfigureAgentmethod inDeclarativeUtil, the version with 4 parameters callsconfigureAgent(agentType, chatModel, false, agentBuilder, agentConfigurator)which passesallowNullChatModel=false.This means when the public constructor is used (without a chat model), it would fail if no chat model is provided via
@ChatModelSupplier. But the original code usedconfigureAgent(agentType, agentBuilder)which called withallowNullChatModel=true.I need to fix the public constructor to use the original behavior with
allowNullChatModel=true:Assistant
I need to fix the public constructor to use the original behavior. The public constructor should call
configureAgent(agentServiceClass, this)which allows null chat model, while the new constructor callsconfigureAgent(agentServiceClass, chatModel, this, agentConfigurator)which doesn't allow null.Assistant
I need to fix the public constructor to call the original
configureAgent(agentServiceClass, this)which allows null chat model:Assistant
Now I have both constructors:
- The public constructor that calls
configureAgent(agentServiceClass, this)- allows null chat model - The package-private constructor that calls
configureAgent(agentServiceClass, chatModel, this, agentConfigurator)- requires chat model
Now let me try to compile the code to verify the changes are correct:
- The public constructor that calls
Assistant
I need to fix the type inference issue. The compiler cannot infer the type arguments. Let me update the code to explicitly specify the type:
Assistant
The issue is that
varcan't infer the type when there are multiple constructors with different signatures. Let me explicitly specify the type parameter:Assistant
I need to fix the type inference issue by replacing
varwith explicit type. Let me use the replace command:Assistant
continue
Assistant
Now I can see line 335. Let me replace it:
Assistant
Now I need to fix the
@SuppressWarningsannotation - it can't be placed like that inside an if block. Let me fix this properly:Assistant
Now I need to add a helper method
createAgentBuilderwith the@SuppressWarningsannotation. Let me find a good place to add it:Assistant
Let me add the helper method before
declarativeChatModel:Assistant
I see the replace didn't work as expected - line 335-336 still has the old code. Let me check again:
Assistant
The replace command didn't work because the search text didn't match exactly (there was whitespace difference). Let me try again:
Assistant
continue
Let me verify the change and then add the helper method:
Assistant
The replace didn't work. Let me try a different approach - directly edit the specific lines:
Assistant
Now I need to add the helper method
createAgentBuilder. Let me add it beforedeclarativeChatModel:Assistant
I see there's a missing blank line before
private static <T> ChatModel declarativeChatModel. Let me fix that:Assistant
Now I also need to update the
createSubagentmethod that also has the same issue. Let me find it:Assistant
I see line 602 still uses
new AgentBuilder<>(subgentClass, chatModel, agentConfigurator). Let me check if this compiles:Assistant
The compilation errors show that lines 351 and 602 have type inference issues. Let me fix both of them. Line 351 is in the
createAgentBuilderhelper method, and line 602 is increateSubagent. I need to explicitly specify the type parameters.Assistant
The issue is that the
new AgentBuilder<>(...)call cannot infer the type parameters when there are multiple constructors. I need to explicitly specify the type parameterTfor the first type argument.Let me fix line 351 by specifying the type explicitly:
return new AgentBuilder<T>(agentServiceClass, chatModel, agentConfigurator);And for line 602, I need to use
@SuppressWarnings("unchecked")and cast appropriately.Assistant
I see the indentation was lost. Let me fix that and also fix line 602:
Assistant
The previous command didn't work properly. Let me navigate to line 602 and fix it.
Assistant
I see the issue. Line 602 has
Class<?>so we can't use a type parameter. Let me fix it by usingObjectas the type parameter with@SuppressWarnings:Assistant
The previous command seemed to have worked but the response was not properly formatted. Let me now try to compile to see if there are any remaining issues.
Assistant
I see -
AgentBuilderhas 2 type parameters<T, B extends AgentBuilder<T, ?>>. I need to provide both or use raw types with proper casting. Let me fix this by using raw types with explicit casts.Assistant
Now let me fix line 602 with the same pattern:
Assistant
The
@SuppressWarningsannotation on line 602 doesn't work as a local variable annotation in this position. Let me try to compile and see what happens.Assistant
I see the issue - the
AgentBuilderconstructor with 3 parameters is package-private, butAgenticServicesis in a different package (dev.langchain4j.agenticvsdev.langchain4j.agentic.agent). I need to make the constructor public.Assistant
I need to make the 3-parameter constructor public so it can be accessed from
AgenticServiceswhich is in a different package. Let me find and update it:Assistant
I need to make the 3-parameter constructor public. Let me update line 161:
Assistant
Now let me try to compile again:
Assistant
The compilation succeeded. Now let me verify my changes by running the existing tests to make sure nothing is broken.
Assistant
The tests passed. Let me verify by checking if there were any test failures.
Assistant
All 45 tests passed. Let me also review the changes I made to ensure they are correct and summarize them:
Assistant
The previous command was executed successfully. Let me continue by reviewing the git diff to understand the changes made.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →