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 agentic module structure:
Assistant
Let me look at the skills module:
Assistant
Let me look at the AgentBuilder and related classes:
Assistant
Let me look at AgentInvocationHandler.java to understand how it sets up the agent:
Assistant
Let me look at the rest of AgenticServices.java to understand how sub-agents are built:
Assistant
Let me look at the ToolService:
Assistant
Let me look at the AgentBuilder constructor:
Assistant
Now let me look at the
configureAgentmethod (with 2 args) - the one called from the AgentBuilder constructor:Assistant
Now I understand the bug. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)(2-arg), and thencreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)(4-arg) which adds the tool provider twice.Let me check the tests in the agentic module:
Assistant
Let me look at the DeclarativeAgentIT to understand how the
createAgenticSystemis tested:Assistant
Now let me look at what tests exist for
Skills:Assistant
Let me write a reproduction script:
Assistant
Let me check the pom.xml for the agentic module:
Assistant
Let me first understand the issue better by looking at the various entry points:
Assistant
Let me look at how AgentBuilder is used elsewhere:
Assistant
Let me first write a small reproducer to verify the issue:
Assistant
Let me first verify the issue by creating a small test in a temporary location:
Assistant
Let me create a temporary test to reproduce the bug:
Assistant
Let me check the maven setup:
Assistant
Let me look at the
validateAgentClassmethod inAgentUtil:Assistant
Now let me look at the
ToolProviderclass to see if it has anequalsmethod:Assistant
Let me check how
AgentBuilderis used in other code paths:Assistant
Let me find all uses of the 2-arg
configureAgent:Assistant
Let me check if the direct
new AgentBuilder<>()use case is used anywhere:Assistant
Now I understand the bug. Let me look at the
AgentBuildermore carefully to confirm the duplicate registration:Assistant
Let me look at the existing tests to understand how to set up a test:
Assistant
Let me check what
ToolProviderfrom the skills module does, then verify the test setup:Assistant
Let me try to build the agentic module:
Assistant
Now let me write a test to reproduce the bug:
Assistant
Now let me look at the actual JAR for the agentic module:
Assistant
Let me now write a simple Java test to reproduce the bug:
Assistant
Now let me write a test that reproduces the issue:
Assistant
Now let me write a test that reproduces the actual issue with
createAgenticSystem:Assistant
Let me modify the test to actually call the agent method:
Assistant
I've successfully reproduced the bug. Now let me implement the fix. I'll modify the
AgentBuilderto allow skipping theconfigureAgentcall, and modifycreateAgenticSystemandcreateSubagentto use this.First, let me look at the current
AgentBuilderconstructor:Assistant
Now let me implement the fix. I'll add a new package-private constructor that takes a
boolean autoConfigureparameter:Assistant
Now let me implement the fix. I'll modify the
AgentBuilderto add a new package-private constructor that takes aboolean autoConfigureparameter:Assistant
Let me use a different approach - direct file editing:
Assistant
Let me use the
editcommand to modify theAgentBuilder.javafile:Assistant
There's a duplicate
this.nameline. Let me fix it:Assistant
The constructor is now fixed. Now let me update the
AgenticServices.javato use the new constructor:Assistant
Now let me verify the changes and rebuild:
Assistant
The
new AgentBuilder<T, ?>doesn't work because of theTreference. Let me use the raw type:Assistant
Let me use the explicit type:
Assistant
Let me check the public constructor more carefully:
Assistant
Let me try a different approach - using the raw type:
Assistant
Let me try a different approach by looking at the actual issue with type inference:
Assistant
Let me look at the actual
createAgenticSystemmethod:Assistant
Let me add a static factory method to
AgentBuilder:Assistant
Let me try a different approach. I'll add a static factory method to
AgentBuilder:Assistant
Let me revert and use a simpler approach. The issue is that the public static method has a known return type. Let me look at the original
agentBuildermethod and make the new approach similar:Assistant
The static factory method is still there. Let me remove it more carefully:
Assistant
I broke the public constructor. Let me restore it:
Assistant
Let me revert the file and start over:
Assistant
Assistant
Let me take a cleaner approach. I'll refactor the
configureAgentmethod to split it into two parts:Assistant
Let me look at how the existing
agentBuildermethod is structured:Assistant
Let me add a new public static method to
AgenticServicesthat creates anAgentBuilderwith the option to skip auto-configure:Assistant
Now let me modify the
AgenticServices.javato use the new approach. I'll modify theagentBuildermethod to be flexible:Assistant
Let me take a step back and think about the cleanest approach. I'll refactor the code to separate concerns:
Assistant
Let me take the cleanest approach. I'll modify the
AgentBuilderconstructor to NOT callconfigureAgentautomatically. ThecreateAgenticSystemandcreateSubagentwill call it explicitly:Assistant
OK, let me commit to a clean approach. I'll extract the chat model and configurator logic into a separate method, and modify
createAgenticSystemandcreateSubagentto call only this new method:Assistant
Let me take the simplest possible approach. I'll modify
createAgenticSystemandcreateSubagentto not callconfigureAgentagain, and instead just set the chat model and apply the configurator. First, let me look at how to check if a chat model annotation exists:Assistant
OK, let me take the cleanest approach. I'll just modify the
createAgenticSystemandcreateSubagentto not callconfigureAgentagain. Instead, I'll have the constructor do the work, and the create methods only set the chat model and apply the configurator.First, let me modify
DeclarativeUtilto extract the chat model fallback and configurator into separate methods:Assistant
OK, let me commit to a simple, clean fix. I'll modify the
AgentBuilderconstructor to not callconfigureAgentautomatically, and always call it fromcreateAgenticSystemandcreateSubagent:Assistant
Let me take the cleanest approach - extract the chat model and configurator logic into a separate method:
Assistant
OK, I've been overthinking this. Let me just implement a clean, simple fix. I'll add a helper method to
DeclarativeUtiland modifycreateAgenticSystemandcreateSubagent:Assistant
The insertion went into the wrong place. Let me fix this:
Assistant
Good, the file is back to the original state. Now let me insert the new method at the right place:
Assistant
Good, the new method is inserted. Let me add a blank line before the private method:
Assistant
Let me revert and try a different approach:
Assistant
Let me verify the compilation:
Assistant
Now let me update
AgenticServices.javato use the new method:Assistant
Now let me build and test the fix:
Assistant
Now let me test the fix with the reproducer:
Assistant
The fix works! Now let me also test with the
Skillsmodule to verify theactivate_skilltool doesn't cause duplicates:Assistant
Excellent! The fix works for both cases. Now let me also verify the original test case from the issue description works:
Assistant
All four test cases pass. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All 45 tests pass. Now let me also run the skills tests:
Assistant
All 77 skills tests pass. Now let me also run the integration tests that don't require API keys:
Assistant
All tests pass. Now let me also check that the
ToolProviderissue is fixed for the case where the same provider is used multiple times in sub-agents. Let me create a test that verifies the fix works for sub-agents:Assistant
The sub-agent case works too. Let me now look at the changes I made:
Assistant
Let me fix the extra blank line:
Assistant
Now let me verify the compilation again:
Assistant
All tests pass. Let me also check that the fix handles edge cases well. Let me also test the case where there's no
@ToolProviderSupplierannotation:Assistant
Let me also test the direct use case to make sure backward compatibility is maintained:
Assistant
Backward compatibility is maintained. Let me look at the final state of the changes:
Assistant
Let me clean up the temporary test files:
Assistant
Now let me also verify the fix works with the original issue's example (using
SkillswithcreateAgenticSystem):Assistant
The fix works for the exact case from the issue. Let me clean up:
Assistant
Now let me verify the final state of the changes:
Assistant
Let me run the tests one more time to make sure everything passes:
Assistant
All 45 tests pass. Let me also run the skills tests:
Assistant
All 77 skills tests pass. Let me also run the langchain4j core tests to make sure nothing is broken:
Assistant
All tests pass. Let me review the changes one more time to make sure everything is clean:
Assistant
Assistant
The changes are clean and minimal. Let me submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.