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 error when usingAgenticServices.createAgenticSystem(). The error occurs becausecreateAgenticSystemregisters non-dynamicToolProviderinstances multiple times, causing theactivate_skilltool to be registered multiple times.Let me start by exploring the repository structure to understand the codebase.
Assistant
Good, I found the main file. Let me examine the AgenticServices.java file to understand the
createAgenticSystemmethod.<tool_call> <function=bash_terminal> <parameter=command> cat /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgenticServices.java </tool_call>
Assistant
The cat command didn't return output properly. Let me try reading the file with open or a different approach.
Assistant
It looks like the open command didn't work as expected. Let me try using the open command properly.
Assistant
The open command requires proper syntax. Let me try the correct format:
open [OPTIONS] [FILE] [LINE_NUMBER].Assistant
The path is relative but we're already at the root. Let me check the directory structure.
Assistant
Now I understand the structure. The directory is
langchain4j-agenticdirectly under/langchain4j. Let me read the AgenticServices.java file:Assistant
Let me search specifically for the
createAgenticSystemmethod and also look at the relevant ToolService code:Assistant
Let me view the createAgenticSystem method and surrounding context:
Assistant
Now let me look at the ToolService to understand how tools are registered and where the duplicate error occurs:
Assistant
Let me look at the ToolService.java file to understand how tools are registered.
Assistant
Now I need to see the
addToolsandcreateContextFromStaticToolsAndProvidersmethods that are in the middle of the file (the output was truncated):Assistant
Now I understand the issue better. The
addToolsmethod throws an error when a tool with the same name is already registered. Let me find where ToolProviders are being registered multiple times in the agentic code:Assistant
Let me examine the DeclarativeUtil.java to see how ToolProviders are being processed.
Assistant
Let me look at the AgentBuilder.java to understand how tools are being registered:
Assistant
Now let me find where the
activate_skilltool is being added:Assistant
Let me look at the Skills.java file to understand how
activate_skilltool is being added:Assistant
Now I understand the architecture better. Let me look at how the AiServiceContext and AiServices handle tool providers:
Assistant
Let me now look at the test files for the declarative agent to understand the scenario better:
Assistant
Now let me trace the bug. The error occurs in
ToolService.addToolswhen a tool is registered twice. Let me understand the flow ofcreateAgenticSystemmore carefully:Assistant
Assistant
Let me use the proper command format.
Assistant
Assistant
The user wants me to use the proper bash command syntax. Let me search for usages of AgentBuilder.
Assistant
Now I understand the issue. Let me verify the exact cause by looking at the code flow:
agentBuilder(agentServiceClass)creates an AgentBuilder- AgentBuilder constructor calls
configureAgent(agentServiceClass, this)- processes @ToolProviderSupplier - Then
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called AGAIN - processes @ToolProviderSupplier again
This causes the same tool provider to be registered twice, leading to the duplicate
activate_skilltool error.Let me implement the fix:
Assistant
Let me view the exact section of the file I need to modify:
Assistant
Now I understand the issue clearly. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, processing@ToolProviderSuppliera second time. This causes the same tool provider to be registered twice, leading to duplicateactivate_skilltool registration.Let me implement the fix by modifying the
createAgenticSystemmethod to not callconfigureAgentagain:Assistant
The
ChatModelSupplierimport is already present in the file. Now I need to see what imports I need for the fix.Let me look at the import statement for
DefaultDeclarativeAgentCreationContext:Assistant
Let me create a simple reproduction script to verify the bug first.
Assistant
I need to check the pom.xml to understand the dependencies and then create a test to reproduce the issue.
Assistant
Assistant
Let me check if agentPostProcessor is used in configureAgent:
Assistant
OK, agentPostProcessor is not used in configureAgent. But looking at the original code in createAgenticSystem, the configureAgent method is called, which only uses the configurator part of AgentConfigurator.
Assistant
Let me check for StreamingChatModelSupplier.
Assistant
Let me check the StreamingChatModelSupplier import status and implement the fix:
Assistant
Now I have a clear understanding of the issue. Let me implement the fix:
- The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which re-processes all annotations - This causes
@ToolProviderSupplierto be processed twice, creating duplicate tool providers - When both tool providers are registered, they both return the same
activate_skilltool, causing the duplicate definition error
The fix is to not call
configureAgenttwice increateAgenticSystem. Instead, only apply the chatModel (if needed) and the custom configurator.- The
Assistant
Now I need to implement the fix. The issue is clear:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSupplier- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, processing@ToolProviderSuppliera second time - This causes duplicate tool registration
The fix is to NOT call
configureAgentagain increateAgenticSystem, but instead:- Apply the chatModel if the agent doesn't have
@ChatModelSupplieror@StreamingChatModelSupplier - Apply the custom configurator
Assistant
Now I understand the issue. Let me implement the fix. The problem is that
createAgenticSystemcallsconfigureAgentwhich re-processes all annotations (including@ToolProviderSupplier) after theAgentBuilderconstructor has already calledconfigureAgent. This causes the same tool provider to be registered twice.Assistant
Good, I've added the import for StreamingChatModelSupplier. Now let me modify the createAgenticSystem method to not call configureAgent again.
Assistant
Now I need to modify the
createAgenticSystemmethod to not callconfigureAgentagain. TheAgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this), so calling it again causes the duplicate registration.The fix is to:
- Not call
configureAgentagain (to avoid duplicate tool registration) - Apply the custom configurator from
agentConfigurator - Set the chatModel if neither
@ChatModelSuppliernor@StreamingChatModelSupplieris present
Let me implement this fix:
- Not call
Assistant
Let me replace the lines 338-339 (the configureAgent call) with the fix. The AgentBuilder constructor already calls configureAgent(agentServiceClass, this), so I need to remove the second call and instead:
- Apply the chatModel if neither @ChatModelSupplier nor @StreamingChatModelSupplier is present
- Apply the custom configurator
Let me use the edit command to replace the problematic section.
Assistant
Let me use sed to replace the specific line:
Assistant
Now let me use sed to replace the configureAgent call with the fix:
Assistant
Now I need to replace the
configureAgentcall on line 338. Let me create a Python script to do the replacement more reliably:Assistant
Let me try with python:
Assistant
Let me use sed to perform the replacement:
Assistant
Let me verify the changes were applied correctly:
Assistant
The fix has been applied. Let me verify the import was added correctly:
Assistant
Wait, I need to reconsider the fix. The original code checks for both
@ChatModelSupplierand@StreamingChatModelSupplierbefore setting the chatModel. My fix only checks for@ChatModelSupplier. Let me update the fix to check for both:Assistant
The fix needs to check for both
@ChatModelSupplierand@StreamingChatModelSupplier, not just@ChatModelSupplier. Let me update the fix:Assistant
Let me check if the replacement worked properly and update the condition to use both variables.
Assistant
I need to also update the condition to check both
hasChatModelSupplierandhasStreamingChatModelSupplier:Assistant
Let me verify the fix is complete and correct:
Assistant
Now let me verify that the code compiles correctly:
Assistant
The code compiles successfully. Let me verify the complete fix by looking at the diff:
Assistant
Good, the diff looks correct. The fix:
- Adds the
StreamingChatModelSupplierimport - Replaces the duplicate
configureAgentcall with:- Check for
@ChatModelSupplierand@StreamingChatModelSupplier - Only set chatModel if neither annotation is present
- Apply the custom configurator
- Check for
Now let me run the existing tests to make sure the fix doesn't break anything:
- Adds the
Assistant
Let me check if the tests ran successfully by looking at the output:
Assistant
The tests were skipped due to environment variables (OPENAI_API_KEY, etc.). Let me check if there are other tests that might not need external services:
Assistant
Let me run the unit tests that don't require external services:
Assistant
All unit tests pass. Now let me create a simple test to verify the fix resolves the original issue:
Assistant
I need to compile and run the test to verify the fix works. Let me try to compile it with the necessary dependencies.
Assistant
Let me check if the test file was created properly and try to compile it using the project's build system. I'll try a different approach - maybe create a simple test in the test directory.
Assistant
Let me try a different approach to verify the fix. I'll look at the existing tests to see if I can run them with the fix, or create a simpler verification method.
Assistant
Now let me also check if my fix properly handles the case when
chatModelis null and neither@ChatModelSuppliernor@StreamingChatModelSupplieris present. The original code would throw an error in this case. Let me check my fix:Assistant
I need to think about edge cases and make sure my fix handles them properly.
-
When
chatModelis null and neither @ChatModelSupplier nor @StreamingChatModelSupplier is present:- The original code would throw an error in configureAgent with
allowNullChatModel=false - My fix sets
agentBuilder.chatModel(null)and then build() would throw an error in validateChatModel()
- The original code would throw an error in configureAgent with
-
When @ChatModelSupplier is present:
- The constructor's configureAgent call sets the chatModel from the method
- My fix doesn't override it (correct behavior)
-
When @StreamingChatModelSupplier is present:
- The constructor's configureAgent call sets the streamingChatModel from the method
-
Assistant
I see there's another call to
configureAgenton line 612 of AgenticServices.java. Let me check if it has the same issue:Assistant
I see there's another call to
configureAgenton line 612 that has the same issue. Let me check the context more carefully:Assistant
I found another instance of the same bug! The
createSubagentmethod has the same issue. Let me fix it:Assistant
Let me check the AgentConfigurator class to see what fields it has:
Assistant
I need to fix the same issue in
createSubagentmethod. TheagentBuilder(subgentClass)constructor callsconfigureAgent(subgentClass, this)internally, and thenconfigureAgent(subgentClass, chatModel, agentBuilder, agentConfigurator)is called again, causing duplicate tool registration.I need to apply the same fix: replace the
configureAgentcall with direct handling of chatModel and configurator.But first, let me check what imports are needed for this method and what fields are available in AgentConfigurator.
Looking at line 285, the AgentConfigurator has:
configurator: Consumer<DeclarativeAgentCreationContext<?>>- But I also see
subAgentResolver()is used, so there must be another field.
Let me check the full AgentConfigurator record definition.
Assistant
Now I need to fix the same issue in
createSubagentmethod. The problem is the same:agentBuilder(subgentClass)constructor callsconfigureAgentinternally, and thenconfigureAgentis called again, causing duplicate tool registration.Assistant
Now I need to apply the same fix to the
createSubagentmethod which has the same issue. Let me verify the fix was correctly applied first:Assistant
The fix for
createAgenticSystemis complete. Now let me also check if thecreateSubagentmethod has the same issue. Looking at line 612, it also callsconfigureAgentafteragentBuilder()which would cause the same duplicate registration problem.Assistant
Let me verify the current state of my fix and check if there are any remaining issues.
Assistant
Let me verify the current state of the fix and ensure it's complete:
Assistant
Looking at the issue more carefully, I notice there's another method
createSubagentthat has the same problem - it also callsagentBuilder()(which internally callsconfigureAgentin the constructor) and then callsconfigureAgentagain. Let me fix that as well:Assistant
Let me apply the same fix to the
createSubagentmethod which has the same duplicateconfigureAgentcall issue:Assistant
Let me apply the same fix to the
createSubagentmethod which has the same issue:Assistant
Let me verify that the fix was applied to the createSubagent method as well.
Assistant
The sed replacement didn't work because it doesn't handle newlines well. Let me use a different approach to fix the createSubagent method as well.
Assistant
Let me try using perl to fix the createSubagent method:
Assistant
The Perl command executed without errors. Let me verify that the fix was applied to the createSubagent method.
Assistant
The fix for
createSubagentmethod was applied successfully. Now let me verify that the code compiles correctly:Assistant
The compilation error is because
AgentBuilder<?, ?>doesn't match the generic type parameter. Let me check how the first fix handles this:Assistant
The compilation error is because in
createSubagent, theagentBuildervariable is of typeAgentBuilder<?, ?>which doesn't match the generic type parameter inDefaultDeclarativeAgentCreationContext<T>. I need to cast it properly.Assistant
The compilation error is because
agentBuilderincreateSubagentis typed asAgentBuilder<?, ?>which doesn't match the generic type parameter inDefaultDeclarativeAgentCreationContext<T>. I need to cast it properly.Assistant
The compilation error is because
agentBuilderincreateSubagentis typed asAgentBuilder<?, ?>which doesn't match the generic type parameter inDefaultDeclarativeAgentCreationContext<T>. I need to cast it properly.Assistant
Let me verify the fix was applied correctly and then compile:
Assistant
The fix for
createSubagentmethod has been applied. Now I need to verify the code compiles correctly.Assistant
The compilation error persists because of the wildcard type issue. Let me try a different approach using a raw type or casting:
Assistant
Let me compile again to check if the fix works:
Assistant
The code now compiles successfully. Let me verify the final changes:
Assistant
The fix looks complete. Let me run the tests to verify everything works correctly:
Assistant
All 45 tests pass. Let me now check if there are any other places in the codebase that might have the same issue:
Assistant
Let me check if there are any other places that might have the same issue by looking at AgentUtil.java:
Assistant
This usage in AgentUtil.java only calls
agentBuilder(c).build()without a separateconfigureAgentcall, so it's fine - theAgentBuilderconstructor handles everything.Now let me verify the final state of the fix and make sure everything is correct:
Assistant
The user has reminded me of the format for my responses. I need to include a tool call first, then my reasoning. Let me verify the fix is complete and submit it.
Assistant
Let me verify the final state of the fix and run tests one more time to ensure everything is working correctly before submitting.
Assistant
The compilation and tests ran. Let me verify all tests passed:
Assistant
All 45 tests pass successfully. Let me provide a summary of the changes made and then submit.
Summary of Changes:
The bug was caused by
configureAgentbeing called twice:- First in the
AgentBuilderconstructor (viaconfigureAgent(agentServiceClass, this)) - Then again in
createAgenticSystemandcreateSubagentmethods
This caused
@ToolProviderSuppliermethods to be processed twice, resulting in the same tool provider being registered twice. When both providers returned the sameactivate_skilltool, theIllegalConfigurationException: Duplicated definition for tool: activate_skillerror was thrown.The Fix:
-
createAgenticSystemmethod: Replaced the duplicateconfigureAgentcall with:- Checking if
@ChatModelSupplieror@StreamingChatModelSupplieris present - Only setting
chatModelif neither annotation is present - Applying the custom configurator directly (without re-processing annotations)
- Checking if
- First in the
Assistant
The user wants me to submit my changes. Let me use the correct format for the submit command.
Assistant
The user is asking me to submit my changes. Let me use the correct format for the submit command.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.