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
Let me start by exploring the repository structure to understand the codebase and locate the relevant files mentioned in the issue.
Assistant
Good, I found the file. Let me examine it to understand the issue:
Assistant
Now let me see the full
createAgenticSystemmethod. The file was truncated, so let me see the relevant parts:Assistant
Now let me look at the
configureAgentmethod which is being called from thecreateAgenticSystemmethod:Assistant
Let me look at the DeclarativeUtil class to understand how
configureAgenthandles ToolProvider registration:<tool_call> <function=bash_terminal> <parameter=command> find /langchain4j -type f -name "DeclarativeUtil.java" 2>/dev/null | head -5 </tool_call>
Assistant
Let me search for the DeclarativeUtil file again more carefully.
Assistant
Now let me look at the DeclarativeUtil file to understand how it handles ToolProvider registration:
Assistant
Now I can see the issue. In
DeclarativeUtil.configureAgent(), there is this code at lines 62-68:getAnnotatedMethodOnClass(agentType, ToolProviderSupplier.class) .ifPresent(method -> { checkArguments(method); checkReturnType(method, ToolProvider.class); agentBuilder.toolProvider(invokeStatic(method)); });The issue is that when
configureAgentis called (for instance increateAgenticSystem), it adds the tool provider viaagentBuilder.toolProvider(invokeStatic(method)). If there are multiple agents or if the method is called multiple times, this would add duplicate tool providers.Now, let me look at the
agentBuilder.toolProvider()method to understand how tool providers are registered and how deduplication should work. Let me also understand how theactivate_skilltool gets added to tool providers.<tool_call> <function=bash_terminal> <parameter=command> find /langchain4j -type f -name "AgentBuilder.java" 2>/dev/null | head -5 </tool_call>
Assistant
Let me properly call the bash_terminal function to search for AgentBuilder.java
Assistant
Let me read the AgentBuilder class to understand how it handles tool providers.
Assistant
Now I need to understand how the ToolProvider contributes the
activate_skilltool. Let me search for where this tool is created or registered:<tool_call> <function=bash_terminal> <parameter=command> grep -r "activate_skill" /langchain4j --include="*.java" 2>/dev/null | head -30 </tool_call>
Assistant
Let me search properly using bash_terminal function.
Assistant
Let me look at the Skills.java file to understand how the
activate_skilltool is created:Assistant
Now I understand the issue. Looking at the code:
- In
Skills.java, thecreateToolProvidermethod always adds theactivate_skilltool to the tool provider. - In
DeclarativeUtil.configureAgent(), when aToolProviderSuppliermethod is found, it callsagentBuilder.toolProvider(invokeStatic(method)). - The problem is that when
createAgenticSystemis called, it may callconfigureAgentmultiple times, leading to the same tool provider being added multiple times.
Let me look at where
configureAgentis called to understand the duplicate registration issue:<tool_call> <function=bash_terminal> <parameter=command> grep -n "configureAgent" /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgentBuilder.java </tool_call>
- In
Assistant
Let me properly use the bash_terminal function to search for configureAgent calls in AgentBuilder.java.
Assistant
The error says the file doesn't exist. Let me verify the correct path:
<tool_call> <function=bash_terminal> <parameter=command> ls -la /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/ </tool_call>
Assistant
Let me properly use the bash_terminal function to list the files in the directory.
Assistant
The AgentBuilder.java file is in the
agentsubdirectory. Let me find it:<tool_call> <function=bash_terminal> <parameter=command> ls -la /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/agent/ </tool_call>
Assistant
I need to properly use the bash_terminal function to get the listing of the agent directory.
Assistant
Now let me search for configureAgent calls in AgentBuilder.java:
Assistant
Now I can see that
configureAgentis called in theAgentBuilderconstructor at line 141. Let me look at lines around 141 to understand how it's being used:<tool_call> <function=bash_terminal> <parameter=command> sed -n '130,155p' /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/agent/AgentBuilder.java </tool_call>
Assistant
I need to properly use the bash_terminal function to view lines 130-155 in the AgentBuilder.java file.
Assistant
Now let me understand the flow. In
createAgenticSystem, there are two paths:- Path 1:
createComposedAgentis called which returns an agent - Path 2: If the agent is null,
agentBuilder(agentServiceClass)is called, and thenconfigureAgentis called on that builder.
The issue is that when
agentBuilder(agentServiceClass)is called, theAgentBuilderconstructor already callsconfigureAgentinternally (line 141). Then, if that agent is null,configureAgentis called again at line 338 inAgenticServices.java:configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);This second call to
configureAgentwould add the tool provider again, causing duplicates.Let me look at the full
createAgenticSystemmethod to verify this understanding:<tool_call> <function=bash_terminal> <parameter=command> sed -n '328,360p' /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgenticServices.java </tool_call>
- Path 1:
Assistant
I need to use the bash_terminal function to view lines 328-360 in AgenticServices.java.
Assistant
Now I can see the problem. When
agentBuilder(agentServiceClass)is called (line 337), theAgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)internally (line 141 in AgentBuilder.java).Then, if
agent == null,configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again (line 338), which would add the tool provider again.The fix should be to avoid calling
configureAgentagain increateAgenticSystemsince theAgentBuilderconstructor already does that. Let me check if there's a reason whyconfigureAgentis being called with the additional parameters (chatModel, agentConfigurator) that the constructor call doesn't have.Looking at the two overloads:
configureAgent(agentServiceClass, this)- called in constructorconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)- called after creating builder
Assistant
I need to use bash_terminal to view the createComposedAgent method to understand when it returns null.
Assistant
Assistant
I need to use the bash_terminal function to view the configureAgent method to understand the current implementation and find the right place to add the check for ToolProvider deduplication.
Assistant
Let me look at the method signature of configureAgent to understand all its parameters:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '48,55p' /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
Assistant
I need to use the bash_terminal function to view lines 48-55 in DeclarativeUtil.java to understand the configureAgent method signature.
Assistant
Assistant
Let me use bash_terminal to find the exact line number where the toolProviders field is declared in AgentBuilder.java.
Assistant
Assistant
Let me use bash_terminal to view the first 160 lines of AgentBuilder.java to understand its structure and see where to add the flag.
Assistant
Now I understand the issue clearly. The fix should be to:
- Add a flag to
AgentBuilderto track ifconfigureAgentwas already called - Modify
configureAgentto skip adding ToolProvider/ChatModelSupplier if already configured (but still apply agentConfigurator)
Let me first implement the fix in
AgentBuilder.javaby adding theconfiguredflag:- Add a flag to
Assistant
Let me add the
configuredflag after thetoolProvidersdeclaration:<tool_call> <function=bash_terminal> <parameter=command> sed -i '121a\ private boolean configured = false;' /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/agent/AgentBuilder.java </tool_call>
Assistant
I need to use the bash_terminal function to add the
configuredflag to AgentBuilder.java.Assistant
Now let me verify the change:
Assistant
The flag is added. Now I need to:
- Set this flag to true after the first
configureAgentcall in the constructor - Modify
configureAgentto skip ToolProvider/ChatModelSupplier setup if already configured
Let me first update the constructor to set the flag after
configureAgent:- Set this flag to true after the first
Assistant
Now I need to add
configured = true;after theconfigureAgentcall:Assistant
Let me verify the change:
Assistant
Now I need to modify
DeclarativeUtil.configureAgent()to skip the ToolProvider and ChatModelSupplier setup if the builder was already configured. Let me first view the full method:Assistant
Now I need to modify this method to check if the builder was already configured, and if so, skip the ToolProvider and ChatModelSupplier setup. But I still want to apply the agentConfigurator.
Looking at the issue description again, the problem is specifically about ToolProvider duplicates. The chatModel handling is separate - if a chatModel is provided externally and there's no supplier, it should be used. This is different from adding the same ToolProvider twice.
Let me think about this more carefully:
- ToolProvider duplicates are the main issue - the
activate_skilltool is added twice - ChatModel handling is different - if a chatModel is provided externally, it should be used when there's no supplier
The fix should be to:
- ToolProvider duplicates are the main issue - the
Assistant
I need to use the bash_terminal function to view the DeclarativeUtil.java file and make the necessary modifications.
Assistant
Now I understand the issue clearly. The
configureAgentmethod is called twice - once in theAgentBuilderconstructor and once increateAgenticSystem. This causes the to be added twice, leading to duplicate tool definitions.Assistant
Let me use the bash_terminal function to view the configureAgent method and make the necessary modifications to skip ToolProvider and ChatModelSupplier setup if already configured.
Assistant
Now I need to modify the
configureAgentmethod to:- Skip ToolProvider setup if already configured
- Skip ChatModelSupplier setup if already configured
Let me use the edit command to make these changes. The key is to check
agentBuilder.configuredbefore adding ToolProvider and ChatModelSupplier.Actually, looking at the code more carefully, I realize that the fix should be:
- Check if
agentBuilder.configuredis true - If true, skip ToolProvider and ChatModelSupplier setup
- But still apply the agentConfigurator
Let me create the modified version of the method. I'll need to be careful with the indentation.
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/declarative_util_fix.txt << 'EOF' EOF sed -n '54,155p' /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
Assistant
Let me use the bash_terminal function properly to view the current state of the configureAgent method and then make the necessary modifications.
Assistant
Now I understand the structure. I need to modify the
configureAgentmethod to check if the builder was already configured and skip the ToolProvider and ChatModelSupplier setup if it was.The fix should:
- Add a check
if (!agentBuilder.configured)before adding ToolProvider - Add a check
if (!agentBuilder.configured)before handling ChatModelSupplier
This way, the first call (in constructor) will set up ToolProvider and ChatModelSupplier, and subsequent calls will skip them but still apply the agentConfigurator.
Let me make these changes using the edit command.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '66,74p' /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
- Add a check
Assistant
I need to use the bash_terminal function to view the specific lines for ToolProvider handling and then modify them to check if the builder was already configured.
Assistant
Now I'll modify the ToolProvider section to check if the builder was already configured:
Assistant
Let me verify the change was applied correctly:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '66,80p' /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
Assistant
Let me check if the sed command worked correctly by viewing the file.
Assistant
The sed command didn't work correctly - it duplicated the content. Let me restore the file from git and try a different approach.
Assistant
Good, the file was restored. Now let me also restore the AgentBuilder.java file and start fresh with a proper edit approach.
Assistant
Let me verify the files are restored:
Assistant
Now let me make the changes properly using the edit command. First, let me add the
configuredflag to AgentBuilder.java:Assistant
Good, the
configuredflag was added. Now let me set it to true after theconfigureAgentcall in the constructor:Assistant
Now let me modify the
configureAgentmethod inDeclarativeUtil.javato check if the builder was already configured before adding ToolProvider and ChatModelSupplier. First, let me view the current state of the method:Assistant
Assistant
I'm still looking at the wrong file. Let me navigate to the DeclarativeUtil.java file first.
Assistant
Now let me modify the ToolProvider section to check if the builder was already configured. I'll wrap it in a condition:
Assistant
Let me try a different approach using sed to add the condition:
Assistant
Now I need to add a closing brace after line 73 (after the ToolProvider section). Let me first check the current state of the file.
Assistant
The sed command didn't work correctly. Let me restore the file and try a different approach:
Assistant
Let me use the open command to view the file and then use a proper edit approach.
Assistant
Let me use the bash_terminal function with a simpler approach - write the fix to a temporary file and then use cat to replace the section.
<tool_call> <function=bash_terminal> <parameter=command> cat << 'SED_SCRIPT' > /tmp/fix_declarative.sed 68s/.*/ if (!agentBuilder.configured) {/ 73s/ });/ });\n }/ SED_SCRIPT cat /tmp/fix_declarative.sed </tool_call>
Assistant
Let me use sed with the -i flag to modify the file directly. I'll try a simpler approach by first viewing the file structure and then applying the fix step by step.
Assistant
Let me verify the change was applied correctly:
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && sed -n '68,80p' langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
Assistant
Let me check if the sed command was applied correctly by viewing the modified lines.
Assistant
The ToolProvider section now has the
if (!agentBuilder.configured)check. Now I need to also handle the ChatModelSupplier section to avoid duplicate tool definitions. Let me first view the ChatModelSupplier section:<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && sed -n '103,145p' langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
Assistant
Let me view the ChatModelSupplier section of the file to understand its structure and determine if I need to add the configured check there too.
Assistant
Assistant
I need to use the bash_terminal function properly to view the modified file content.
Assistant
Assistant
I need to use the bash_terminal function properly to compile the code and check for any compilation errors.
Assistant
I need to change the access modifier of
configuredfromprivateto package-private so thatDeclarativeUtilcan access it. Let me fix this:Assistant
Now let me try to compile again to see if there are any other errors.
Assistant
Let me check the package structure of both files:
Assistant
The two files are in different packages. Since I removed the
privatemodifier, it defaults to package-private (friendly), which means only classes in the same package can access it. I need to either:- Make
configuredpublic, or - Add a getter method in AgentBuilder
Let me make it public:
- Make
Assistant
Now let me try to compile again.
Assistant
The code compiles successfully. Now let me verify the changes by viewing the modified files:
Assistant
Let me see the full diff to verify the changes are correct:
Assistant
The changes look correct:
- Added
public boolean configured = false;to AgentBuilder - Set
this.configured = true;after theconfigureAgentcall in the constructor - Added
if (!agentBuilder.configured)check around the ToolProviderSupplier handling in DeclarativeUtil
Now I need to also handle the ChatModelSupplier to ensure the external chatModel is used when there's no supplier. Let me think about this more carefully.
Looking at the issue description again, the main problem is the duplicate
activate_skilltool definition. This is caused by the ToolProvider being added twice. The ChatModelSupplier handling might be redundant but should still work correctly.- Added
Assistant
Let me search for test files related to the langchain4j-agentic module to understand how to test the fix.
Assistant
Let me check if there are any tests for AgenticServices:
<tool_call> <function=bash_terminal> <parameter=command> find /langchain4j -name "Test.java" -path "/langchain4j-agentic/" | xargs grep -l "AgenticServices" 2>/dev/null | head -5 </tool_call>
Assistant
Let me search for tests that use AgenticServices to understand how to test the fix.
Assistant
Let me run the existing tests to make sure my changes don't break anything:
Assistant
The tests seem to be running. Let me wait for them to complete and check the results:
Assistant
There's no specific test for DeclarativeUtil. Let me run the full test suite for the langchain4j-agentic module:
Assistant
All 45 tests pass. Now let me also run the tests for the skills module to make sure the fix doesn't break the Skills functionality:
Assistant
All tests pass. Now let me create a simple test case to verify that the fix works for the reported issue. Let me create a test file that simulates the issue:
Assistant
Now let me verify that the fix is complete. Let me also check if there's a way to test this more directly. But first, let me review the changes I made to ensure they're correct.
The fix I implemented:
- Added
public boolean configured = false;toAgentBuilderto track if the builder has been configured - Set
this.configured = true;after theconfigureAgentcall in the constructor - Added
if (!agentBuilder.configured)check around the ToolProviderSupplier handling inDeclarativeUtil.configureAgent
This ensures that when
createAgenticSystemis called:- The
AgentBuilderconstructor callsconfigureAgent, which adds the ToolProvider - The
configuredflag is set to true - In
createAgenticSystem, the secondconfigureAgentcall skips the ToolProvider setup becauseconfiguredis true - The external chatModel is still used (if there's no @ChatModelSupplier)
- The agentConfigurator is still applied
This should fix the duplicate
activate_skilltool error.- Added
Assistant
I need to use the bash_terminal function to view the git diff and verify the changes.
Assistant
The changes look correct. Let me verify the formatting is correct by viewing the modified sections:
Assistant
The formatting is not ideal - the opening brace is on the same line as the condition. Let me fix the formatting to make it more readable:
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && git checkout -- langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
Assistant
Let me apply the fix with proper formatting:
Assistant
Let me verify the formatting:
Assistant
The sed command duplicated the condition. Let me restore and try a different approach.
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && git checkout -- langchain4j-agentic/src/main/java/dev/langchain4j/agentic/declarative/DeclarativeUtil.java </tool_call>
Assistant
The sed command didn't work correctly. Let me try a different approach using the edit command to properly format the code.
Assistant
The file is now in a bad state. Let me restore it and apply the fix properly:
Assistant
Let me apply the fix using a different approach - I'll create a patch file and apply it:
Assistant
Let me try a different approach - use Python to make the edit:
Assistant
Let me try with python:
Assistant
Let me try using perl instead:
Assistant
Let me verify the change was applied correctly:
Assistant
The formatting looks correct now. Let me verify that the code compiles:
Assistant
The code compiles. Let me also run the tests to make sure the fix doesn't break anything:
Assistant
Let me check the test results:
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && mvn test -pl langchain4j-agentic 2>&1 | grep -E "(BUILD|Tests run|Failures|Errors)" | tail -10 </tool_call>
Assistant
I need to use the bash_terminal function to run the tests and check the results.
Assistant
All 45 tests pass. Let me also run the tests for the skills module to make sure the fix doesn't break the Skills functionality:
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && mvn test -pl langchain4j-skills 2>&1 | grep -E "(BUILD|Tests run|Failures|Errors)" | tail -10 </tool_call>
Assistant
Let me run the skills module tests to verify the fix doesn't break the Skills functionality.
Assistant
All 77 tests in the skills module pass as well. Now let me verify the final state of the changes:
<tool_call> <function=bash_terminal> <parameter=command> cd /langchain4j && git diff </tool_call>
Assistant
I need to verify the final state of the changes by viewing the git diff.
Assistant
The changes look correct. Let me now verify that the fix addresses the issue described in the bug report:
-
Issue: When creating an agentic system with
AgenticServices.createAgenticSystem(), the application throws anIllegalConfigurationException: Duplicated definition for tool: activate_skillbecause the ToolProvider is registered multiple times. -
Root cause: In
createAgenticSystem, whenagentBuilder(agentServiceClass)is called, theAgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which adds the ToolProvider. Then, if the agent is null (which happens for simple@Agentmethods),configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again, which adds the ToolProvider again. -
Fix: I added a
configuredflag toAgentBuilderthat is set totrueafter the first call. In , the ToolProvider setup is now guarded by , so the ToolProvider is only added once.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.