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.
Assistant
Let me look at the AgenticServices class which is the file mentioned in the issue.
Assistant
Let me look at the full file and find the
createAgenticSystemmethod:Assistant
Let me find the DeclarativeUtil class and the
configureAgentmethod:Assistant
Now let me look at the AgentBuilder class:
Assistant
Let me look at the Skills class to understand the activate_skill tool:
Assistant
Let me look at the ToolProvider class to understand the
isDynamicmethod:Assistant
Let me look at the ToolService.java:
Assistant
Let me look at the
buildSequentialAgentand related methods:Assistant
Let me look at the
AgentBuilderbuildmethod and thesetupToolspart more carefully:Assistant
Let me look at the
ToolService.toolProvidersmethod:Assistant
Let me first try to reproduce the issue to understand it better:
Assistant
Now let me look at the test directory for langchain4j-agentic to see if there are any tests related to tool providers.
Assistant
Let me look at how Skills is integrated in tests:
Assistant
Let me explore the test directory of langchain4j-agentic:
Assistant
Let me look at the DeclarativeAgentIT.java:
Assistant
Let me look at the langchain4j-skills module setup:
Assistant
Now let me try to reproduce the issue. First, let me look at how the issue would manifest by tracing through the code:
Assistant
Let me look at the
AgentBuilder.toolProvidersfield declaration:Assistant
Let me confirm the issue by writing a quick test:
Assistant
Let me trace through the code to understand the bug:
Assistant
Let me look at the issue from the perspective of the user. The fix should be in the
createAgenticSystemmethod or in howconfigureAgentis called. Let me trace through the code one more time:Assistant
Let me check the use of
toolProviderin AgentBuilder:Assistant
Now let me also check the workflow agents builder:
Assistant
Let me check how the workflow agents handle tool providers:
Assistant
Now let me check the
createSubagentmethod, as it might have the same issue:Assistant
Let me first write a reproduction test to confirm the issue:
Assistant
Now let me create a reproduction test to confirm the issue:
Assistant
Good. Maven works. Let me now create a reproduction test in the proper location:
Assistant
I have successfully reproduced the issue. Now let me implement the fix. Looking at the root cause:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which adds the ToolProvider from the supplier method.createAgenticSystemthen callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)AGAIN, which invokes the same supplier method again, creating a new ToolProvider instance.- Both ToolProvider instances are added to the
toolProviderslist (anArrayList, not aSet). - When the agent runs,
ToolServicecallsprovideToolson each provider, both returning the sameactivate_skilltool, causing the duplicate registration error.
Let me look at how to fix this. The best approach is to deduplicate the tool providers at the
AgentBuilderlevel using aSet<Method>to track which supplier methods have been processed:Assistant
Now let me check if
Methodimport is already there in DeclarativeUtil:Assistant
Now let me implement the fix. I'll modify the
AgentBuilderto deduplicate the tool providers based on the supplier method:Assistant
Let me clean up the imports properly:
Assistant
Let me fix the duplicate import:
Assistant
Let me use the
editcommand to properly fix the imports:Assistant
Let me reset the imports and use a different approach:
Assistant
Now the imports are clean. Let me use a more careful approach to edit the file. Let me first look at the exact text to edit:
Assistant
Now let me use the
editcommand to add the HashSet import:Assistant
Now let me look at the exact code I need to modify:
Assistant
Now let me apply the fix. I'll modify the
toolProvidersfield to use aSet<Method>for deduplication and update thetoolProvidermethod:Assistant
Let me use a different approach. Let me look at the field declaration and use the
editcommand:Assistant
Now let me add a new method that takes the supplier method:
Assistant
Let me restore the file and use a different approach:
Assistant
Good, the file is restored. Now let me apply the changes carefully:
Assistant
Let me fix the import order:
Assistant
Now let me check that the new method was added correctly:
Assistant
Now let me check the field declaration:
Assistant
The field wasn't added. Let me add it now:
Assistant
The field still wasn't added. Let me look at the actual line content.
Assistant
Let me check what's happening with grep:
Assistant
Let me try a different approach using sed:
Assistant
Now let me update the
DeclarativeUtil.configureAgentto pass the supplier method:Assistant
Now let me try to compile:
Assistant
The issue is that the new method is package-private but
DeclarativeUtilis in a different package. Let me make it public:Assistant
Compilation successful. Now let me run the reproduction test:
Assistant
The test passes! The fix works. Now let me also verify the fix handles the more complex case with a sub-agent (e.g., SequenceAgent). Let me also think about other edge cases:
Assistant
Let me check the SequenceAgent annotation:
Assistant
Let me fix the test to avoid the name conflict:
Assistant
The first test passes (the fix works). The second test fails because of a test setup issue (the result is null, not because of the duplicate tool issue). Let me update the test:
Assistant
Both tests pass. Now let me also run the existing tests to make sure I haven't broken anything:
Assistant
All tests pass. Now let me verify the test I created is in the right place, and then I'll remove it (since I shouldn't be adding new tests to the repository per the instructions):
Assistant
Let me also check if there are other places where
configureAgentis called:Assistant
Let me also check the workflow agents:
Assistant
Let me view the final state of the changes:
Assistant
I see an extra blank line at the end. Let me fix that:
Assistant
The diff still shows an extra blank line. Let me check the file more carefully.
Assistant
There's an extra blank line. Let me see if it was the original state:
Assistant
Let me check the original line count:
Assistant
The original file had 559 lines, ending with
}\nand a blank line\nat the end. So there are 559 lines and 1 trailing newline (or 558 lines + 1 trailing newline).Wait, let me think about this. If
wc -lreports 559, that means there are 559 newlines. If the file ends with}\n\n, that's 2 newlines. Hmm.Let me look at the actual structure:
Assistant
The original file ends with
}\n}\n(3 newlines at the end: one for}, one for}, and one final newline). So the file is 559 lines with the last line being a blank line.Wait,
wc -lcounts newlines. If the file ends with}\n}\n, that's 2 newlines for the closing braces plus 1 for the final newline = 3 newlines. Butwc -lsays 559.Let me just look at the current file to make sure the structure is correct.
Assistant
The current file has an extra newline. Let me remove it:
Assistant
The diff is now clean. Let me also write a quick test to verify the fix and then remove it:
Assistant
Both tests pass. Now let me remove the test file (since I shouldn't be adding new tests to the repository):
Assistant
Now let me also run the other existing tests in the agentic module to make sure my fix doesn't break anything:
Assistant
All 45 tests pass. Now let me think about the issue from another angle - the issue description says:
The agentic system should run successfully, properly deduplicating tools (including the built-in
activate_skilltool) when registering non-dynamicToolProviderinstances.My current fix only addresses the case where the same supplier method is invoked twice (e.g., when
configureAgentis called twice). But there could be other cases where duplicates might occur. Let me think about this.Let me also check if there are any other places where the
toolProvidermethod is called on the AgentBuilder:Assistant
Let me also check the
langchain4j-skillsmodule:Assistant
All 77 tests in
langchain4j-skillspass. Now let me also check thelangchain4jcore module:Assistant
All 1381 tests in the
langchain4jcore module pass. Now let me also run the tests in thelangchain4j-agenticmodule one more time:Assistant
Let me also verify the imports in AgentBuilder.java:
Assistant
Let me also look at the final state of the new method:
Assistant
Now let me also check the DeclarativeUtil.java:
Assistant
Now let me also run the
mvn installto make sure the whole build works:Assistant
Now let me verify by writing a temporary test and running it to confirm the fix:
Assistant
Both tests pass. Now let me remove the temporary test file:
Assistant
Let me also verify the final state of the changes:
Assistant
Let me also verify the tests one more time to make sure everything works:
Assistant
All 45 tests pass. Let me also check the issue description one more time to make sure I addressed everything:
Issue Description Summary:
- ✅
createAgenticSystemthrowsIllegalConfigurationException: Duplicated definition for tool: activate_skillwhen usingAgenticServices.createAgenticSystem(). - ✅ Bug is caused by
createAgenticSystemregistering non-dynamicToolProviderinstances multiple times. - ✅
activate_skilltool is automatically added for everySkillProviderinstance. - ✅ Fix: properly deduplicating tools when registering non-dynamic
ToolProviderinstances.
Let me also think about what should happen in the edge cases:
- ✅
Assistant
Let me also look at the
toolProviders(Collection<ToolProvider>)andtoolProviders(ToolProvider...)methods to see if they could be affected:Assistant
Let me run all tests one more time to make sure everything passes:
Assistant
All 45 tests pass. Let me also run a final verification of the code:
Assistant
Let me also check that no test files were accidentally left:
Assistant
Let me do a final verification by looking at the changes:
Assistant
Let me verify the final state one more time:
Assistant
Only the two intended source files are modified. Let me now verify by listing the checklist:
-
[x] If an error was reported in the issue description, I have successfully reproduced it.
- I created a reproduction test that triggered the
IllegalConfigurationException: Duplicated definition for tool: activate_skillerror.
- I created a reproduction test that triggered the
-
[x] If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- The reproduction test now passes after applying my fix.
-
[x] I have thought about edge cases and made sure my fix handles them as well.
- Multiple
@ToolProviderSuppliermethods: Each method is tracked separately in theSet<Method>. - Same supplier method in parent/child classes: Since
getAnnotatedMethodOnClassreturns the sameMethodobject, the deduplication works correctly.
- Multiple
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.