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 analyze this issue. The bug is about duplicate
activate_skilltool definition when usingAgenticServices.createAgenticSystem(). The error occurs because non-dynamicToolProviderinstances are being registered multiple times, causing theactivate_skilltool to be registered more than once.Let me first explore the repository structure to understand the codebase and then find the relevant files.
Assistant
Let me explore the repository structure and find the AgenticServices.java file mentioned in the issue.
Assistant
Good, I found the AgenticServices.java file. Let me open it to understand the current implementation.
Assistant
I need to use the open command correctly without the line_number parameter.
Assistant
Let me try using bash to read the file.
Assistant
Let me find the
createAgenticSystemmethod in the file.Assistant
Now let me look at the
agentBuilderclass and how it configures agents, sincecreateAgenticSystemfalls back to usingagentBuilderwhen it's not a composed agent. The issue says the ToolProvider instances are registered multiple times.Assistant
Now let me look at the DeclarativeUtil to understand how
configureAgentworks and how tool providers are registered.Assistant
Now I understand the issue. Let me look at the
ToolProviderSupplierannotation and how tools are registered. The issue is in theselectMethodfunction inDeclarativeUtil- it recursively searches the class hierarchy and interfaces. Since the same interface can have a@ToolProviderSuppliermethod, if multiple agents use the same interface or if the same method is found multiple times during traversal, the sameToolProvidercould be registered multiple times.Let me look at the error more closely. The issue states the bug is "registering non-dynamic
ToolProviderinstances multiple times". Let me trace through the code to understand when this happens.Assistant
Now let me look at how the
createAgenticSystemmethod builds agents and trace through where the duplicate registration happens. Let me check thebuildSequentialAgentand similar methods that callcreateAgenticSystemrecursively for sub-agents.Assistant
Now I need to understand the actual problem better. Let me look at how the
AgentBuilderconstructor callsconfigureAgent:Assistant
I see! In the
AgentBuilderconstructor (line 140), it callsconfigureAgent(agentServiceClass, this). This internally callsconfigureAgentwhich adds theToolProviderfrom the@ToolProviderSuppliermethod.Then, in
createAgenticSystem(line 338), after creating the agent builder, it callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again. This means the sameToolProvideris being added twice to the same agent builder!Let me verify this:
Assistant
Yes! Here's the bug:
agentBuilder(agentServiceClass)creates anAgentBuilderinstance- Inside
AgentBuilderconstructor,configureAgent(agentServiceClass, this)is called which invokes@ToolProviderSuppliermethods and addsToolProviders to the builder - Then
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again, which invokes the same@ToolProviderSuppliermethods and adds the sameToolProviders again
So the
ToolProviderinstances are registered twice! This causes theactivate_skilltool (which comes fromSkillProvider) to be registered twice, leading to the duplicate tool definition error.Now let me check the first
configureAgentmethod call (inAgentBuilderconstructor) to understand the full signature:Assistant
Now let me also check if there are tests related to this issue that I can use to validate my fix.
Assistant
_commands>
Assistant
Let me check the test file to understand the expected behavior better:
Assistant
Let me look for tests related to
Skillsoractivate_skillto better understand the bug:Assistant
Let me search more broadly:
Assistant
Assistant
I need to include a proper tool call to search for AgentConfigurator in the file.
Assistant
I can see the issue clearly now. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSuppliermethods and adds tool providers. ThencreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which processes the same@ToolProviderSuppliermethods a second time, causing duplicate tool registrations.Let me look at the
createAgenticSystemmethod more closely to understand what needs to be changed:Assistant
Now I understand the issue. The fix should be in
createAgenticSystem- since theAgentBuilderconstructor already callsconfigureAgentto process all supplier methods (including@ToolProviderSupplier), we should not callconfigureAgentagain increateAgenticSystem. We only need to:- Set the chatModel if no ChatModelSupplier was found
- Apply the agentConfigurator
Let me implement this fix:
Assistant
Now I understand the issue clearly. The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)which processes all supplier methods including@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate registration of tools.The fix should avoid calling
configureAgentagain increateAgenticSystem. Instead, we should only:- Set the chatModel if no
@ChatModelSupplierwas found - Apply the
agentConfigurator
Let me implement this fix:
- Set the chatModel if no
Assistant
Let me try a different approach - use sed or a shell script to make the replacement.
Assistant
I need to apply my fix to the AgenticServices.java file. Since python3 is not available, I'll use the edit command to replace the problematic code block.
Assistant
I need to open the file first before editing it. Let me use the correct syntax for the open command.
Assistant
The file is now open. I need to navigate to line 337-339 where the problematic code is, and replace it with my fix.
Assistant
Now I can see the code. I need to replace lines 337-339 with my fix that avoids the duplicate
configureAgentcall.Assistant
Wait, I notice my fix is missing the line that creates the agentBuilder variable. Let me check the current state of the code:
Assistant
I see that my edit accidentally removed the line that creates the
agentBuildervariable. The original code was:var agentBuilder = agentBuilder(agentServiceClass); configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator); agent = agentBuilder.build();And my replacement only has the comments and the build() call, but I forgot to include the
var agentBuilder = agentBuilder(agentServiceClass);line. Let me fix this:Assistant
The fix looks good now. Let me verify the changes by viewing the modified method:
Assistant
The changes look correct. Now let me verify that the code compiles and run any existing tests to make sure the fix doesn't break anything.
Assistant
The code compiles successfully. Now let me look for and run the relevant tests to make sure my fix doesn't break anything.
Assistant
Let me examine one of the test files that uses
createAgenticSystemto understand how it's being used and ensure my fix is correct.Assistant
Let me now run the tests to see if my fix works. First, let me try to compile and run just the unit tests (not integration tests that require API keys).
Assistant
Let me run more tests related to declarative agents:
Assistant
The user hasn't responded yet - let me continue by running the tests to verify my fix works correctly. The previous test run seems to have completed successfully. Let me check if there are any test failures and then verify my fix is complete.
Assistant
All 45 tests pass. Now let me also check if there are any related tests in other modules that might be affected by this change, particularly tests that use
createAgenticSystemwith@ToolProviderSupplier.Assistant
Let me check if there are any tests that use the Skills module mentioned in the issue description, since the bug specifically mentions the
activate_skilltool from the Skills module:Assistant
Let me also check if there's a
createSubagentmethod that might have the same issue, and verify that my fix doesn't break any edge cases:Assistant
I see there's also a
createSubagentmethod at line 604 that callsconfigureAgentafter creating anAgentBuilder. Let me check this method more closely to see if it has the same issue:Assistant
Ah, I see that
createSubagenthas the same issue! It also creates anAgentBuilder(which callsconfigureAgentin its constructor) and then callsconfigureAgentagain. I need to fix this method as well.Assistant
I need to fix the
createSubagentmethod as well, since it has the same duplicateconfigureAgentcall issue. Let me apply the same fix to that method.Assistant
Let me verify that the changes compile correctly and run the tests again to make sure everything still works.
Assistant
The code compiles successfully. Now let me run the tests again to make sure everything still works after my fix to both
createAgenticSystemandcreateSubagentmethods.Assistant
I see the issue. My fix is checking
declarativeChatModel(agentServiceClass) == null, but this only checks for zero-argument@ChatModelSuppliermethods. The failing tests use@ChatModelSuppliermethods with parameters, which are handled differently inconfigureAgent. I need to check if ANY@ChatModelSuppliermethod exists (with or without parameters).Let me look at the test to understand better:
Assistant
I see the issue. My fix only checks for zero-argument
@ChatModelSupplier, but the tests have parameterized@ChatModelSuppliermethods that are already handled by theAgentBuilderconstructor's call toconfigureAgent. So my fix is setting a second chat model, causing conflicts.I need to check if ANY chat model supplier method exists (with or without parameters) before setting the chatModel. Let me look at the
selectMethodfunction to understand how to check for this:Assistant
I need to update my fix to check if ANY
@ChatModelSuppliermethod exists (with or without parameters), not just zero-argument ones. Let me create a helper method or use the existingselectMethodfunction to check for this.Assistant
I see. The
getAnnotatedMethodOnClassfunction can be used to check if a method with a certain annotation exists. Let me update my fix to check for ANY@ChatModelSuppliermethod, not just zero-argument ones:Assistant
Good,
ChatModelSupplieris already imported. Now I need to update my fix to check if ANY@ChatModelSuppliermethod exists (not just zero-argument ones) before setting the chatModel. Let me update the fix:Assistant
I see the issue. The
declarativeChatModel()method only checks for zero-argument@ChatModelSuppliermethods. But if there's a parameterized@ChatModelSupplier, the AgentBuilder constructor'sconfigureAgentalready sets up the chat model provider. I need to check if ANY@ChatModelSuppliermethod exists.Assistant
I see I made a logic error - the condition should be
if (!hasChatModelSupplier(agentServiceClass))since we only want to set the chatModel when there's NO ChatModelSupplier. Let me fix that and add the helper method.Assistant
Now I need to add the
hasChatModelSupplierhelper method. Let me add it after thedeclarativeChatModelmethod:Assistant
Now I need to also update the
createSubagentmethod to use the samehasChatModelSupplierhelper method instead ofdeclarativeChatModel:Assistant
I need to update the
createSubagentmethod to usehasChatModelSupplierinstead ofdeclarativeChatModel:Assistant
Now let me verify that the code compiles and run the tests again to confirm the fix is correct.
Assistant
The code compiles successfully. Now let me run the tests again to make sure all tests pass.
Assistant
All 45 tests pass. Let me verify the final diff to make sure everything looks correct before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.