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.Let me start by exploring the repository structure and understanding the codebase, particularly the
AgenticServicesclass and how tool providers are registered.Assistant
Let me look at the AgenticServices.java file to understand the current implementation.
Assistant
I need to use the correct terminal command format. Let me use bash commands to view and edit files.
Assistant
Let me look at the specific
createAgenticSystemmethod and understand how it configures agents:Assistant
Now let me look at the
DeclarativeUtil.configureAgentmethod to understand how tools are registered:Assistant
Now I understand the issue better. The
configureAgentmethod callsagentBuilder.toolProvider(invokeStatic(method))which invokes the@ToolProviderSuppliermethod. This method creates a newToolProviderinstance each time it's called.Let me look at how the
AgentBuilderhandles tool providers and see if there's deduplication logic:Assistant
Now I need to understand the flow better. Let me look at the issue more carefully. The issue says that
configureAgentis called twice for the same agent - once in theAgentBuilderconstructor and once increateAgenticSystem. Let me verify this:- In
AgentBuilderconstructor,configureAgent(agentServiceClass, this)is called - In
createAgenticSystem, whenagent == null, it callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)
The
@ToolProviderSuppliermethod is invoked each timeconfigureAgentis called, which adds the sameToolProvider(or a new instance with the same tools) to the agent builder's list. Let me check theconfigureAgentmethod calls:- In
Assistant
Now I see the issue clearly. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSupplierand adds tool providers. Then increateAgenticSystem, when the agent is not a composed agent, the code callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again, which adds the same tool providers a second time.Let me look at both
configureAgentoverloads:Assistant
So there are two public overloads:
configureAgent(Class<?> agentType, AgentBuilder<?, ?> agentBuilder)- called fromAgentBuilderconstructorconfigureAgent(Class<?> agentType, ChatModel chatModel, AgentBuilder<?, ?> agentBuilder, AgentConfigurator agentConfigurator)- called fromcreateAgenticSystem
Both eventually call the private method which processes all the suppliers including
@ToolProviderSupplier. The issue is that increateAgenticSystem, theAgentBuilderconstructor already calledconfigureAgentonce, and thencreateAgenticSystemcalls it again.Let me check the
createAgenticSystemmethod flow more closely:Assistant
Now I understand the problem clearly:
-
agentBuilder(agentServiceClass)creates a newAgentBuilder, which in its constructor callsconfigureAgent(agentServiceClass, this)- this is the first call that adds the@ToolProviderSuppliertool provider. -
Then
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)is called again - this is the second call that adds the same@ToolProviderSuppliertool provider again.
This results in duplicate tool provider registration, causing the
activate_skilltool to be registered twice.The fix should be to not call
configureAgentagain increateAgenticSystemsince theAgentBuilderconstructor already does that. But we still need to apply thechatModelandagentConfigurator.Let me look at what the second
configureAgentcall does that's different from the first:-
Assistant
Now I see the issue clearly. When
createAgenticSystemis called:agentBuilder(agentServiceClass)is called, which triggers theAgentBuilderconstructor- The constructor calls
configureAgent(agentServiceClass, this)withnullchatModel andtrueforallowNullChatModel, andAgentConfigurator.empty() - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again
The second call adds duplicate tool providers. The fix should remove the duplicate
configureAgentcall increateAgenticSystem, since theAgentBuilderconstructor already handles all the configuration from annotations. But we still need to:Assistant
Let me look at how the AgentConfigurator is used:
Assistant
Let me check the build methods for composed agents to see how they handle configuration:
Assistant
I can see that
createSubagentalso has the same issue - it callsagentBuilder(subgentClass)which triggers the constructor that callsconfigureAgent, and then callsconfigureAgentagain.The proper fix is to modify the
AgentBuilderconstructor to NOT callconfigureAgent, and instead have all callers explicitly callconfigureAgentafter creating the builder. However, this would be a bigger change and might affect other code paths.A simpler and more targeted fix would be to:
- Track whether the
@ToolProviderSuppliermethod has already been invoked and skip if it has - Or modify
createAgenticSystemto not callconfigureAgentagain, but instead only apply the chat model and agentConfigurator
Let me look at the approach more carefully. The cleanest fix would be to not call
configureAgentfrom the constructor and let the caller do it. But we need to check all places that useagentBuilder():- Track whether the
Assistant
I see there are two places where
agentBuilder()is followed byconfigureAgent():- Line 337-338: in
createAgenticSystem - Line 597-598: in
createSubagent
The fix should be to not call
configureAgentin theAgentBuilderconstructor, and instead have all callers callconfigureAgentexplicitly. Let me check if there are other usages ofAgentBuilderconstructor:- Line 337-338: in
Assistant
Now I understand the issue. Let me look at the flow more carefully:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- processes@ToolProviderSupplierand adds tool providerscreateAgenticSystemthen callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)- processes@ToolProviderSupplieragain, adding duplicate tool providers
This causes duplicate registration of the
activate_skilltool.Let me check if there are any tests related to this:
Assistant
Let me look for tests that use
ToolProviderSupplier:Assistant
I was about to search for ToolProviderSupplier tests when the command failed. Let me try again with the correct syntax.
Assistant
Now I understand the issue clearly. Let me check how to fix this properly. The issue is that
configureAgentis called twice - once in theAgentBuilderconstructor and once increateAgenticSystem. The fix should be to not callconfigureAgentfrom the constructor, since callers should explicitly call it.Let me look at what happens in the builder constructor and see if there's a flag to track if configureAgent was already called:
Assistant
Let me look at the exact structure of the
configureAgentmethod and understand the flow:Assistant
Now I understand the full picture. The solution should be:
-
Remove the
configureAgentcall from theAgentBuilderconstructor - The constructor should NOT callconfigureAgentbecause the caller will do it. -
Update all places that use
AgentBuilderto callconfigureAgentexplicitly - This includes:- (already does it)
-
Assistant
Now let me check if there's a way to add a flag to track whether
configureAgentwas already called. I'll modify theAgentBuilderclass to add this flag:Assistant
Now let me implement the fix. The approach is:
- Add a flag
configuredto track whetherconfigureAgenthas been called - Remove the
configureAgentcall from the constructor - In
build(), ifconfiguredis false, callconfigureAgentwith default parameters - Add a package-private
markAsConfigured()method thatDeclarativeUtil.configureAgentcan call to mark the builder as configured
- Add a flag
Assistant
Now I need to make the changes. Let me first add a flag to track if
configureAgenthas been called, then modify the constructor to not call it, and modify thebuild()method to call it if needed:Assistant
Now I'll make the changes. I need to:
- Add a
configuredfield to track whetherconfigureAgenthas been called - Remove the
configureAgentcall from the constructor - Add a package-private method
markAsConfigured() - Modify
build()to callconfigureAgentif not already configured - Update
DeclarativeUtil.configureAgentto callmarkAsConfigured()
Let me make these changes:
- Add a
Assistant
Good. Now I need to remove the
configureAgentcall from the constructor. Let me find the line number:Assistant
Now let me add the
markAsConfigured()method and update thebuild()method. First, let me find the build method:Assistant
I need to add the call to
configureAgentin thebuild(DefaultAgenticScope)method if not already configured. Let me edit the file:Assistant
The edit didn't work properly. Let me revert and try again:
Assistant
Let me use a different approach - I'll use the
editcommand to make the changes more precisely:Assistant
I need to use a different approach since python3 is not available. Let me use sed commands more carefully to make the changes step by step.
Assistant
I'll make the changes one at a time using sed, being careful with the order:
- First, remove the
configureAgentcall from constructor (line 141) - Add the
configuredfield after line 129 - Add
markAsConfigured()method beforepublic T build()(line 160) - Add the configured check in
build(DefaultAgenticScope)(line 164)
- First, remove the
Assistant
Good. Now let me add the
configuredfield after line 129:Assistant
Good. Now I need to add the
markAsConfigured()method beforepublic T build()at line 160:Assistant
I notice there's an issue - there's an extra
}and the closing brace is wrong. Let me check the original structure:Assistant
The structure looks correct now. Now I need to add the configured check in
build(DefaultAgenticScope). Let me find the exact line:Assistant
Now I need to add the configured check after line 168 (after the method declaration):
Assistant
The command was executed but I didn't see the output. Let me verify the changes were applied correctly.
Assistant
Now I need to update
DeclarativeUtil.configureAgentto callmarkAsConfigured()on the builder after configuring it:Assistant
The issue is that
createAgenticSystemcauses duplicate tool registrations. Looking at the code:AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- first registrationcreateAgenticSystemthen callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)- second registration
The fix is to:
- Add a
configuredflag toAgentBuilderto track ifconfigureAgentwas already called - Remove the
configureAgentcall fromAgentBuilderconstructor - In
build(), if not configured, callconfigureAgentwith defaults - After
configureAgentcompletes, mark builder as configured
I've already:
- Added
configuredfield ✓ - Removed
configureAgentfrom constructor ✓ - Added
markAsConfigured()method ✓ - Added check in
build(DefaultAgenticScope)to callconfigureAgentif not configured ✓
Now I need to update
DeclarativeUtil.configureAgentto callmarkAsConfigured()at the end so that subsequent calls to are skipped.Assistant
I need to update
DeclarativeUtil.configureAgentto:- Check if already configured and return early if so
- Mark as configured at the end
Assistant
Now I need to add a call to
agentBuilder.markAsConfigured()at the end of theconfigureAgentmethod, and also add theisConfigured()method toAgentBuilder. Let me first find where the privateconfigureAgentmethod ends:Assistant
I need to add
agentBuilder.markAsConfigured()before the closing brace of the privateconfigureAgentmethod:Assistant
Now I need to add the
isConfigured()method toAgentBuilder. Let me also changemarkAsConfigured()fromvoid(package-private) to ensure it can be called fromDeclarativeUtil:Assistant
The previous command didn't execute because I forgot to include a tool call. Let me continue by adding the
isConfigured()method toAgentBuilder.Assistant
I need to add an
isConfigured()method toAgentBuilder. Let me check the current state and add it:Assistant
The user wants me to add an
isConfigured()method to theAgentBuilderclass. Let me add it after themarkAsConfigured()method.Assistant
It looks like the file got corrupted during my edits. Let me check the current state and fix it.
Assistant
Good, the
AgentBuilder.javachanges look correct. Let me verify theDeclarativeUtil.javachanges:Assistant
The changes look good so far. Let me verify the rest of the DeclarativeUtil.java to make sure the markAsConfigured() call is at the end.
Assistant
Let me verify the changes compile correctly and then test them.
Assistant
The methods need to be public since
DeclarativeUtilis in a different package. Let me fix that.Assistant
The previous sed command only changed
markAsConfigured()to public. I still need to changeisConfigured()to public as well.Assistant
I need to run a command to verify the changes compiled correctly.
Assistant
The compilation succeeded. Let me verify my changes by reviewing the modified files:
Assistant
Good, the compilation succeeded. Let me verify the changes by reviewing the diff:
Assistant
The changes look correct. Now let me run the existing tests to verify the fix doesn't break anything:
Assistant
The user hasn't provided any new feedback. Let me check what the current state is. I made changes to fix the duplicate tool registration issue. The compilation succeeded. I ran the tests but they exited with code 0 which could mean they passed or were skipped (since they require API keys).
Let me verify the tests actually ran or were skipped, and then continue with validation.
Assistant
The tests were skipped because they require API keys. Let me look for unit tests that don't require external dependencies:
Assistant
Let me create a simple test to verify the fix works:
Assistant
The user hasn't provided any new message, so I need to continue with my work. Let me run the test I created to verify the fix works.
Assistant
The test file has compilation errors. Let me fix it by looking at the correct imports and API usage.
Assistant
The
@Agentannotation is indev.langchain4j.agentic.Agent, notdev.langchain4j.agentic.declarative.Agent. Let me also check for@UserMessage:Assistant
Let me remove my broken test file and write a simpler test that correctly reproduces the issue and validates the fix:
Assistant
Let me create a proper test that validates the fix without needing external dependencies:
Assistant
The user hasn't sent a new message, so this is just a continuation notification. Let me continue with my work - I need to run the test I created to validate the fix works correctly.
Assistant
I need to fix the test file. The
ToolProviderinterface requires returning aToolProviderResult, not just aList. Let me check the correct API.Assistant
I see. Let me look at the
SingleAgentIT.javato see how they properly implementToolProvider:Assistant
Now I understand. The
ToolProviderinterface takes aRequestparameter and returns aToolProviderResult. Let me simplify my test to just verify the core fix - that theconfiguredflag works correctly:Assistant
Let me verify the fix by examining the code changes more carefully and running the existing unit tests.
Assistant
The tests pass. Let me run more tests to ensure the fix doesn't break anything:
Assistant
Let me investigate the failing test:
Assistant
I see the issue. When
configureAgentis called frombuild()withnullchat model and no@ChatModelSupplier, it still callsagentBuilder.chatModel(null)which overwrites any previously set chat model. Let me fix this:Assistant
Looking at the test that's failing, the issue is that when I call
configureAgentfrombuild()without a chat model, it callsagentBuilder.chatModel(null)which overwrites the chat model that was set explicitly. Let me check the logic more carefully.Assistant
I see the issue. When
configureAgentis called frombuild()with a null chat model, it callsagentBuilder.chatModel(null)which overwrites any chat model set explicitly. I need to fix this so it only sets the chat model when it's not null.Assistant
I see the issue now. When
configureAgentis called frombuild()withnullchat model, it callsagentBuilder.chatModel(null)which overwrites any chat model set explicitly by the user. The fix is to only callagentBuilder.chatModel(chatModel)whenchatModelis not null.Assistant
The user is reporting that my last command didn't invoke a tool call. I need to actually run a command. Let me verify the change I just made to the DeclarativeUtil.java file.
Assistant
Good, the change is in place. Now let me run the tests again to verify the fix:
Assistant
The user seems to have just sent an empty message (or a continuation notification). I need to check if the tests passed and continue with my work. Let me check the test results.
Assistant
The user seems to be waiting for me to continue. Let me run the tests again and check the results properly. The previous test run showed some output but I need to see the final result.
Assistant
All 45 tests pass. Let me review the final changes to make sure everything is correct:
Assistant
All 45 tests pass. Let me review the final changes to make sure everything is correct:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.