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
codeCaused 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
java Assistant
Let me start by understanding the 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 first explore the repository structure to understand the codebase and then find the relevant files.
Assistant
Let me look at the AgenticServices.java file to understand the createAgenticSystem method.
Assistant
I need to use the open command correctly. Let me check the syntax - it seems I should not use the --file option for the open command.
Assistant
Let me focus on the
createAgenticSystemmethod and understand how it handles tool providers. Let me see the full content of the file and find thecreateAgenticSystemmethod:Assistant
Now let me look at the
AgentBuilderclass and theconfigureAgentmethod to understand how tools are registered:Assistant
Now let me look at the
DeclarativeUtilclass to understand howconfigureAgentworks:Assistant
Now I can see the issue! In
DeclarativeUtil.configureAgent(), when a@ToolProviderSuppliermethod is found, it callsinvokeStatic(method)which invokes the static method to get theToolProviderinstance. This is called both:- When
AgentBuilderis created (in the constructor viaconfigureAgent(agentServiceClass, this)) - When
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)
Let me verify this:
- When
Assistant
Now let me look at the
createAgenticSystemmethod more closely:Assistant
Now I understand the bug! Here's the flow:
createAgenticSystem()callsagentBuilder(agentServiceClass)which creates a newAgentBuilder.- The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which registers theToolProviderby invoking the@ToolProviderSuppliermethod. - Then
createAgenticSystem()callsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)which again registers the sameToolProviderby invoking the same@ToolProviderSuppliermethod again.
This results in the same
ToolProviderbeing registered twice, which causes the duplicateactivate_skilltool error.Let me check how the
Skillsclass provides tools and if it's aSkillProvider:Assistant
Now let me look at the fix. The issue is that in
createAgenticSystem, when it's not a composed agent, it:- Creates an
AgentBuilderviaagentBuilder(agentServiceClass)- which callsconfigureAgentin its constructor - Then calls
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again
The solution is to avoid calling
configureAgenttwice. Looking at the code flow:AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- this is the overload withoutchatModelandagentConfiguratorcreateAgenticSystemshould NOT callconfigureAgentagain since theAgentBuilderconstructor already did it
But wait, the
AgentBuilderconstructor uses the overloadconfigureAgent(agentType, agentBuilder)which doesn't pass thechatModeloragentConfigurator. So we need the second call for those. Let me look more closely:- Creates an
Assistant
Both overloads eventually call the same private method that does all the configuration including registering tool providers. The issue is that:
- The first call (from
AgentBuilderconstructor) useschatModel=nullandagentConfigurator=empty() - The second call (from
createAgenticSystem) uses the actualchatModelandagentConfigurator
But both calls add tool providers to the builder, causing duplicates.
The fix should be in
createAgenticSystem. Instead of callingconfigureAgentagain after creating theAgentBuilder, we should only apply thechatModelandagentConfiguratorsettings that weren't applied in the constructor.Let me look at what the constructor version does vs. the full version:
The constructor calls
configureAgent(agentType, agentBuilder)which is:- The first call (from
Assistant
Looking at the issue more carefully, the simplest fix is to not call
configureAgenttwice. TheAgentBuilderconstructor already callsconfigureAgentto pick up all declarative annotations. ThecreateAgenticSystemmethod should just set the chat model and apply the agent configurator separately without re-processing all the annotations.Let me check how the
AgentBuilderhandles the case whenchatModelis null (from the constructor call):Assistant
Now I understand the issue better. The
AgentBuilderconstructor picks up the@ChatModelSupplierannotation (if present) and sets the chat model. If no@ChatModelSupplieris found, it falls through to use thechatModelparameter passed toconfigureAgent, which would benullin the constructor case.Looking at the code flow in
DeclarativeUtil.configureAgent:- The first call (from constructor) uses
chatModel=nullandallowNullChatModel=true, so if no@ChatModelSupplieris found, it just setschatModel(null) - The second call (from
createAgenticSystem) uses the actualchatModel
So the fix should be in
createAgenticSystem: instead of calling again, it should just set the chat model directly on the builder if it wasn't already set by the constructor's call to .- The first call (from constructor) uses
Assistant
Now let me understand the proper fix. Looking at the code flow:
-
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which:- Registers tools from
@ToolsSupplier - Registers tool providers from
@ToolProviderSupplier - Sets up chat model from
@ChatModelSupplierif present, otherwise setschatModel(null) - Registers other components
- Registers tools from
-
then calls which:
-
Assistant
The
modelfield is package-private, so it can be accessed from within the same package. Let me check what the best approach is by looking at how the tests are structured:Assistant
I see the test file uses
@ToolsSupplierannotation, but the bug report mentions@ToolProviderSupplier. Let me search for tests withToolProviderSupplier:Assistant
Command failed exit code: 123
Assistant
Let me now understand the fix. The issue is clear:
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes@ToolProviderSupplierand adds the tool provider- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)which processes@ToolProviderSupplieragain and adds the same tool provider again
The fix should prevent the second
configureAgentcall from re-adding tool providers. The cleanest solution would be to modifycreateAgenticSystemto NOT callconfigureAgentagain but only set the chat model and apply the agent configurator.Let me look at what
configureAgentdoes that we still need in the second call:Assistant
Now I have a clear picture. The
configureAgentmethod does many things:- Register tools from
@ToolsSupplier - Register tool providers from
@ToolProviderSupplier - Set up content retriever, retrieval augmentor, chat memory, etc.
- Set up chat model from
@ChatModelSupplieror use the passedchatModel - Set up agent listener
- Apply
agentConfigurator
The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which is equivalent toconfigureAgent(agentType, null, true, agentBuilder, empty()).Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)which is equivalent toconfigureAgent(agentType, chatModel, false, agentBuilder, agentConfigurator).The issue is that both calls process all annotations, causing duplicates.
The fix: Modify
createAgenticSystemto not callconfigureAgentagain. Instead:- If the builder doesn't have a chat model set (from ), set it directly
- Register tools from
Assistant
Now I need to understand the issue better. The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)withchatModel=nullandagentConfigurator=empty(). This means:- All annotations are processed
- If no
@ChatModelSupplieris present,agentBuilder.chatModel(null)is called - The empty agent configurator is applied
Then
createAgenticSystemcallsconfigureAgentagain with the actualchatModelandagentConfigurator. This causes:- All annotations to be processed again, causing duplicate tool registration
- The chat model to be set correctly (overriding the null)
- The actual agent configurator to be applied
The fix should be to only do what's needed in the second call:
- Set the chat model if it wasn't set by
@ChatModelSupplier - Apply the
agentConfigurator
But we need to be careful. The
AgentBuilderhas private fields, so we can't directly check if a chat model was set. Let me see if we can access them:Assistant
The
modelfield is private. SinceAgenticServicesis in a different package, we can't access it directly. Let me look at a different approach.Assistant
Now I need to understand the proper fix. Let me think about this differently:
The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)which processes all annotations. ThencreateAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)which re-processes all annotations.The fix should be to modify the second call so it only does what's needed:
- Set the chat model if it wasn't already set by
@ChatModelSupplier - Apply the
agentConfigurator
The simplest approach is to add a new method to
DeclarativeUtilthat only sets the chat model and applies the configurator without re-processing annotations. Or, we can modify the call increateAgenticSystemto not useconfigureAgent.Let me look at what needs to be done:
- If
@ChatModelSupplieris not present on the class, and achatModelwas passed, set it on the builder - Apply the
agentConfigurator
- Set the chat model if it wasn't already set by
Assistant
Now I understand the structure better. The
AgentConfiguratorrecord has aconfigurator()method that returns aConsumer<DeclarativeAgentCreationContext<?>>. This is what gets called at the end ofconfigureAgent.The fix is to replace the second
configureAgentcall increateAgenticSystemwith:- Set the chat model on the builder if it wasn't set by
@ChatModelSupplier - Apply the agent configurator directly
Let me implement this fix:
- Set the chat model on the builder if it wasn't set by
Assistant
Now I understand the issue clearly. The fix should replace:
javaconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);with code that:
- Only sets the chat model if
@ChatModelSupplieris not present on the class - Only applies the
agentConfiguratorif it's not empty
Let me write the fix:
- Only sets the chat model if
Assistant
I need to use the edit command to modify the file. Let me first check what's currently open and then use the edit command to make the changes.
Assistant
I need to also add a helper method
hasChatModelSupplier. Let me first look at the imports and see what I need to add.Assistant
Good,
getAnnotatedMethodOnClassis already imported. Now let me also check ifChatModelSupplieris imported:Assistant
Good,
ChatModelSupplieris already imported. Now I'll make the edit using sed:Assistant
Let me use the edit command to replace the problematic code section:
Assistant
Now I'll use the edit command to make the changes. The replacement text needs to:
- Replace the
configureAgentcall with logic that only sets the chat model if needed and applies the agent configurator - Add a helper method
hasChatModelSupplier
- Replace the
Assistant
I need to use the
edittool properly with file path:Assistant
Now I need to add the
hasChatModelSupplierhelper method. Let me find a good place to add it - probably near thedeclarativeChatModelmethod:Assistant
I'll add the helper method right before
declarativeChatModel:Assistant
Let me verify the changes:
Assistant
The changes look good. Now let me verify the file compiles and run any relevant tests. Let me first check what build system this project uses:
Assistant
Great, the code compiles. Now let me run the relevant tests to make sure I didn't break anything. Let me first find tests related to
createAgenticSystem:Assistant
Let me check if there are unit tests (not integration tests requiring external APIs) that I can run:
Assistant
Let me write a simple test to reproduce the bug and verify my fix works:
Assistant
Let me fix the test - I need to use the proper ToolProvider interface:
Assistant
Now I understand. Let me also check
ToolProviderResult:Assistant
I see - the duplicate error is thrown in
ToolProviderResult.Builder.buildFinalToolList(). Let me update the test:Assistant
Let me check the ToolExecutor interface:
Assistant
I see, the ToolExecutor interface takes
(ToolExecutionRequest, Object)not just a generic request. Let me fix the test:Assistant
The test passes. Now let me verify that the bug was actually there before my fix by temporarily reverting the change and running the test:
Assistant
Let me verify the fix by temporarily reverting and confirming the test fails:
Assistant
Hmm, the test still passes without my fix. Let me investigate further - the duplicate error might be happening at a different level. Let me check the ToolService class:
Assistant
I see - the duplicate check is in
ToolService.addTools()when adding tools fromToolProviderinstances. The issue would occur when the sameToolProvideris registered twice and each one provides the same tool.Let me look at what happens during the
AiServices.builder().build()call when tool providers are registered multiple times:Assistant
Let me understand the issue better. When
agentBuilder.toolProvider(...)is called twice with the sameToolProvider, it adds the same provider to the list twice. Then whenAiServicesprocesses them, it callsprovideTools()on each, resulting in duplicate tools.Let me update my test to verify that the ToolProvider is being called twice (which causes the duplicate):
Assistant
Let me check the original code flow more carefully. The issue is that the
ToolProviderinstance is added twice to the list. Let me look at howtoolProvideris handled inAgentBuilder:Assistant
I see - the
toolProvidersis a list, and each call totoolProvider()adds to this list. IfconfigureAgentis called twice, the sameToolProviderwill be added twice to this list.But wait - looking at my test, it passed even without the fix. Let me investigate why. Let me check if there's something special about how
invokeStaticworks - maybe it creates a new instance each time?Looking at the issue more carefully, the bug report says:
"The bug is caused by
createAgenticSystemregistering non-dynamicToolProviderinstances multiple times."The key is that the
@ToolProviderSuppliermethod is static, soinvokeStaticshould return the same result each time it's called. Let me trace through the code:AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which callsinvokeStatic(method)on the@ToolProviderSuppliermethod, adding the result totoolProviderslist.
Assistant
Now let me create a better test that demonstrates the actual issue:
Assistant
The test passes without the fix. Let me investigate the actual issue more carefully. The bug report mentions
Skills.builder().build().toolProvider()- let me examine what's special about it:Assistant
Now I understand the issue better. Looking at the
Skillsclass, it always returns the sameToolProviderinstance (stored inthis.toolProvider). But whenconfigureAgentis called twice, it invokesinvokeStatic(method)twice, and each time gets the sameToolProviderinstance (since it's returned from a static method).Wait, let me trace through the issue again more carefully. The bug report says the error is thrown during execution (
test()), not during creation. Let me re-read the stack trace:codeat 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)Assistant
Interesting! The
ToolService.toolProvidersfield is aLinkedHashSet<ToolProvider>, which should deduplicate by object identity! Let me check:Assistant
So the
ToolService.toolProvidersis aLinkedHashSet, which usesequals()to determine uniqueness. SinceToolProvideris a functional interface (lambda), each invocation ofinvokeStatic(method)would return the same instance (assuming the static method returns the same cached instance likeSkills.toolProvider()does).But wait, if the static method returns a new
ToolProviderinstance each time (like a lambda created inline), then theSetwould not deduplicate them! Let me check:Looking at the issue reproduction code:
Assistant
So each call to
configureAgentcallsinvokeStatic(method)and adds the result toagentBuilder.toolProviderslist. If called twice, the same method is invoked twice, and if it returns a differentToolProviderinstance each time, both get added to the list.Wait, I need to check if
invokeStaticcaches results or something. Let me look:Assistant
Let me continue investigating the issue. I need to find the
invokeStaticmethod implementation.Assistant
I see - the error happens during execution (when
.test()is called), not during creation. My test doesn't invoke the agent! Let me update the test:Assistant
I need to run the test to see if it fails without my fix.
Assistant
Let me find the correct import for AIMessage.
Assistant
The AIMessage class might be in langchain4j-core. Let me search for it there.
Assistant
The AIMessage class might be named differently in this version. Let me check what's in the ChatResponse class to understand how to build a proper response.
Assistant
The
AiMessageclass is indev.langchain4j.data.messagepackage. Let me update my test to use the correct import.Assistant
Let me investigate why the test passes without my fix - I need to trace through the actual execution to understand if the bug exists.
Assistant
The test output confirms:
- After AgentBuilder constructor: 1 tool provider
- After second configureAgent call: 2 tool providers
This proves the bug exists - the
toolProviderslist has 2 entries after the secondconfigureAgentcall. But why doesn't the first test fail?The answer is in
ToolService- it uses aLinkedHashSetwhich deduplicates by object identity. Since my test creates a new lambda each time the static method is called, the two ToolProvider instances are different objects, but they might still be deduplicated if they have the same hash code or something.Wait, looking at the
ToolServicecode again - it's aLinkedHashSet, and for lambdas, the defaultequals()andhashCode()are used, which are based on object identity. So two different lambda instances would be considered different.But the test passes because the mock chat model returns a response without tool calls. Let me check if the duplicate error only occurs when the ToolService actually processes the tools...
Let me look at the ToolService code again to understand when the duplicate check happens:
Assistant
I can see from the test output that the
toolProviderslist has 2 entries after the secondconfigureAgentcall, confirming the bug. Let me create a more targeted test to verify the duplicate tool error actually occurs.Assistant
Now let me apply my fix and verify it resolves the issue.
Assistant
I need to apply my fix to the AgenticServices.java file. Let me replace the problematic code that calls
configureAgenttwice.Assistant
I need to continue applying my fix to the AgenticServices.java file. I've already split the file into two parts. Now I need to create the new code section that replaces the duplicate
configureAgentcall.Assistant
Now let me verify the changes look correct:
Assistant
The fix looks correct. Let me compile and run the tests to verify.
Assistant
The code compiles. Now let me run the test to verify the fix:
Assistant
Good, tests pass. Let me update my test to verify that after the fix, the toolProviders list only has 1 entry:
Assistant
All tests pass. Now let me run the existing tests in the repository to make sure my fix doesn't break anything:
Assistant
All 48 tests pass. Now let me clean up by removing my test file (since I shouldn't modify tests) and verify the git diff to see my final changes:
Assistant
Let me verify the fix is complete by running the existing tests again to make sure I didn't break anything:
Assistant
All 45 tests pass. Let me also verify my fix handles edge cases correctly by checking that the fix is general enough:
-
When
@ChatModelSupplieris present: ThehasChatModelSupplierreturnstrue, so we skip setting the chat model (it was already set by the firstconfigureAgentcall in the constructor). -
When
@ChatModelSupplieris NOT present and a chat model is passed: We set the chat model directly on the builder. -
When
@ChatModelSupplieris NOT present and no chat model is passed: We don't set anything (the builder will throw an error whenbuild()is called if no model is configured - same behavior as before). -
When
agentConfiguratoris not empty: We apply the configurator directly. -
When
agentConfiguratoris empty: We skip the configurator application (same behavior as before, since the constructor already applied an empty one).
Now let me submit my solution.
-
Assistant
Let me provide a final summary of my fix before submitting:
Summary
Bug: When using
AgenticServices.createAgenticSystem(), theconfigureAgent()method was being called twice:- First in the
AgentBuilderconstructor - Then explicitly in
createAgenticSystem()
This caused
@ToolProviderSuppliermethods to be invoked twice, registering the same tool providers twice, leading to the error:IllegalConfigurationException: Duplicated definition for tool: activate_skill.Fix: Modified
createAgenticSystem()to NOT callconfigureAgent()again. Instead, it only:- Sets the chat model on the builder if
@ChatModelSupplieris not present on the class (the constructor'sconfigureAgentcall already processes this annotation) - Applies the
agentConfiguratorif it's not empty
Verification: All 45 existing tests pass.
- First in the
Assistant
The user seems to have submitted an empty code block. Let me check the current state and verify that the fix is properly in place before providing any further guidance.
Assistant
My fix is already in place. Let me submit the changes properly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.