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 exploring the repository structure and understanding the codebase, particularly the files mentioned in the issue.
Assistant
Now I understand. The Skills tool provider has
isDynamic()returningfalsewhen there are no skill-scoped providers (which is the case in the example - only a single skill with content but no toolProviders). WhenisDynamic()isfalse, the tool is registered as a static tool.The error is
Duplicated definition for tool: activate_skill. This happens because the non-dynamic ToolProvider's tools are registered multiple times. Let me look at how ToolService handles this and how the agentBuilder registers tools.Let me look at the AgentBuilder to understand how tools and tool providers are registered.
Assistant
So the AgentBuilder has a list of toolProviders. Let me look at how AiServices handles toolProviders, particularly the ToolService. Let me look at the ToolService to understand the duplicate registration.
Assistant
So the issue is in
addTools- when two tools with the same name are registered, it throws an exception. This happens because theactivate_skilltool is being added twice.Now the question is: why is the tool provider being registered twice? Let me look at how the AgentBuilder registers tools. The issue says "createAgenticSystem registering non-dynamic ToolProvider instances multiple times". Let me look at the AgentBuilder.build() method more carefully.
Assistant
So the AgentBuilder registers the tool providers once. The issue says "createAgenticSystem registering non-dynamic ToolProvider instances multiple times". Let me look at the AgentBuilder.build() method to understand the full picture.
Assistant
The AgentBuilder registers the toolProvider once. But the issue says "createAgenticSystem registering non-dynamic ToolProvider instances multiple times".
Wait - maybe the issue is that the declarative agent configuration (via
configureAgent) adds the tool provider, and then some other code path also adds it. Let me look at how the agentBuilder and the build process handle this.Actually, let me re-read the issue. The example uses
AgenticServices.createAgenticSystem(FooAgent.class).test(). There's a single agent with a@ToolProviderSuppliermethod. So the tool provider is added once viaconfigureAgent.But wait, the issue mentions "multiple registrations of non-dynamic ToolProvider instances". So maybe the activate_skill tool is added multiple times. Let me check: in the example, the Skills builder has only one skill with content but no toolProviders, so returns false (since is empty).
Assistant
No skill-related code in agentic module. So the activate_skill tool comes purely from the Skills tool provider. Let me trace why it's added twice. Let me look at the AiServices.toolProviders method and how it stores them.
Assistant
So the toolProvider is added once via AgentBuilder. The activate_skill is added once. So why is it duplicated?
Wait, maybe the issue is the agentBuilder for the declarative single-agent case isn't the only thing. Let me re-read the issue. The bug is caused by registering non-dynamic instances multiple times. The tool is automatically added for every instance.
Assistant
Only one place. Let me look at the AgentBuilder.build() more thoroughly, from the beginning.
Assistant
I see! Line 141:
configureAgent(agentServiceClass, this);is called in the constructor. This isDeclarativeUtil.configureAgent(Class, AgentBuilder)which configures the agent from declarative annotations - including adding the toolProvider.Then, in
createAgenticSystem:javavar agentBuilder = agentBuilder(agentServiceClass); configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator); // <-- called AGAIN agent = agentBuilder.build();So
configureAgentis called TWICE:- Once in the AgentBuilder constructor (line 141):
configureAgent(agentServiceClass, this)- this adds the toolProvider. - Once again in
createAgenticSystem(line 338):configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)- this adds the toolProvider AGAIN.
That's the duplication! The
toolProviderslist gets the Skills toolProvider added twice, so when ToolService processes the non-dynamic providers, activate_skill is added twice → duplicate.Let me verify this by checking the AgentBuilder constructor.
- Once in the AgentBuilder constructor (line 141):
Assistant
Confirmed. The AgentBuilder constructor calls
configureAgent(agentServiceClass, this)(which is the declarative versionDeclarativeUtil.configureAgent(Class, AgentBuilder)), which adds the tool provider.Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)AGAIN, which adds the tool provider a second time.So the fix is in
createAgenticSystem: it should NOT callconfigureAgentagain since the AgentBuilder constructor already did it. But wait, there's a difference: the constructor'sconfigureAgentusesallowNullChatModel=true, while the one in createAgenticSystem usesallowNullChatModel=false. The chatModel handling differs.Actually, the constructor's configureAgent handles chat model via @ChatModelSupplier. The second configureAgent also handles chat model (and if not provided, falls back to the passed chatModel). So the second call's main purpose is to set the chatModel fallback.
But this double call causes duplicate tool registrations. Let me check the other places that call
configureAgentafteragentBuilder. Let me look at line 597-600.Assistant
Assistant
Confirmed. The constructor calls
DeclarativeUtil.configureAgent(agentServiceClass, this)which isconfigureAgent(agentType, null, true, agentBuilder, AgentConfigurator.empty()).Now let me understand the intent. The constructor's configureAgent sets up the declarative config but with empty configurator and allowing null chat model. Then createAgenticSystem calls the full configureAgent again with the real configurator and chatModel fallback.
Let me look at git blame to see when the constructor call and the createAgenticSystem call were added.
Assistant
Let me look at the recent commit that introduced the AgentConfigurator to understand the intent of the double call.
Assistant
Assistant
Let me look at the test classes that use @ToolProviderSupplier to understand existing behavior. Let me grep across the test dir.
Assistant
So there's no existing test using @ToolProviderSupplier! That's why this bug wasn't caught. Let me check ToolsSupplier usage in tests.
Assistant
ToolsSupplier uses
agentBuilder.tools(tools)which setsobjectsWithTools(a setter). So double-call just overwrites with the same value. No duplicate. That's why it works.Now I'm confident the root cause is the double configureAgent call appending toolProviders twice.
Let me now decide on the fix. I want to avoid the double registration. The cleanest is to refactor so that the declarative suppliers are configured once (in the constructor) and the createAgenticSystem/createSubagent only applies the chatModel fallback and agentConfigurator.
Let me look at the AgentBuilder to see if there's a way to check whether chat model is already configured. Let me look at the fields and whether there are getters.
Assistant
Now, let me think about the cleanest fix. The double call is:
- Constructor: - sets up all suppliers (with empty configurator, no chatModel fallback).
Assistant
Assistant
Assistant
buildAgentFeaturesis in DeclarativeUtil but let me check where it's called. It's not called from AgenticServices. Let me search more broadly.Assistant
Assistant
These extend AbstractServiceBuilder, not AgentBuilder. So they don't call configureAgent in their constructors (they call buildAgentFeatures and configureOutput separately). Their subagents are created via createSubagent. So my change is safe.
Now let me reproduce the bug first. Let me look at how existing tests mock the ChatModel and the test setup.
Assistant
I can write a reproduction test using a fake ChatModel that returns a fixed response. But the error happens in createContext which is called when the agent's method is invoked (the proxy invoke). Let me write a reproduction that uses a non-dynamic ToolProvider and a fake ChatModel.
Actually, the cleanest reproduction: I can directly test at the AgentBuilder level - build the agent and inspect that toolProviders isn't duplicated. But the field is private.
Actually, the simplest reproduction that triggers the actual error: I'll create a fake ChatModel that returns a ChatResponse with no tool calls (just a text answer). When the agent method is invoked, createContext is called, which processes the non-dynamic ToolProvider → addTools → duplicate → throw.
Assistant
Let me look at how existing tests build an agent with a fake model and invoke it. Let me look at SingleAgentIT or look for a test that uses a fake ChatModel and @Tool. Actually, let me look at the
internaltest dir.Assistant
This is a good template. I'll create a temporary test using a DUMMY_MODEL and a non-dynamic ToolProvider. Let me write a reproduction test. I'll model it after the issue: an agent with @ToolProviderSupplier returning a non-dynamic ToolProvider that produces a tool named "activate_skill" (or any fixed name), and invoke via createAgenticSystem.
Actually, I can use the real Skills class to be faithful to the issue. The langchain4j-skills module is separate. Let me check if langchain4j-agentic tests can depend on it. Probably not directly. Let me just use a custom non-dynamic ToolProvider.
Let me create a temporary test file. Let me write it.
Assistant
Let me check the ToolProviderResult builder API to make sure my test compiles.
Assistant
The ToolExecutor interface signature - let me check.
Assistant
My test uses
(toolExecutionRequest, memoryId) -> "ok"which matchesString execute(ToolExecutionRequest request, Object memoryId). Good. Let me fix the test to useToolSpecification.builder()directly to avoid import issues.Assistant
Now let me try to compile and run this test. Let me check the build tooling first.
Assistant
I've reproduced the bug:
Duplicated definition for tool: activate_skill. The exact stack trace matches the issue.Now let me implement the fix. Let me check where
hasChatMemory()is used and the package visibility situation.Assistant
Assistant
Now I understand the full structure. Let me look at the rest (the agentConfigurator line).
Assistant
Now I'll implement the fix. I'll refactor
DeclarativeUtilso that the 4-argconfigureAgent(called bycreateAgenticSystem/createSubagentafter the constructor already configured the declarative suppliers) no longer re-runs the non-chat suppliers (which caused duplicateToolProviderregistrations). It will only resolve the chat-model fallback and apply the agent configurator.Let me extract the chat-model handling into a helper and adjust the 4-arg method.
Assistant
Now let me implement. I'll edit lines 50-52 (the 4-arg public method) to not delegate to the 5-arg, and extract the chat-model block into a helper method.
First, let me change the 4-arg public method (lines 50-52).
Assistant
Now I need to extract the chat-model block into a
configureChatModelmethod, and have the 5-argconfigureAgentcall it. Let me view the current chat model block fully.Assistant
Now I'll replace the chat-model block with a call to
configureChatModel, and then add the new private method. Let me use the replace command.Assistant
The replace command requires the text to be in the displayed lines. Let me navigate to the chat model block and use line-range edit instead.
Assistant
The chat model block is lines 110-150 (including the listener, agentConfigurator, and closing brace). Let me replace that whole range with the new structure that calls
configureChatModeland adds the new method.Assistant
There's leftover duplicate code at lines 161-166 (orphaned listener/agentConfigurator/closing brace). Let me view and remove them.
Assistant
Lines 161-166 are orphaned code that must be removed. Let me replace lines 160-166 (keeping the method-closing brace, removing orphans).
Assistant
Now let me view the top portion (lines 40-120) to verify the structure is correct.
Assistant
The structure looks correct. Now let me compile and run my reproduction test to confirm the fix.
Assistant
The test ran without errors. Let me confirm the test result explicitly.
Assistant
The test passes now. The fix works for the single-agent case. Let me extend my repro test to cover edge cases: the composed-agent (sequence) path with a subagent that has a
@ToolProviderSupplier, and the directagentBuilder(class).build()path mentioned in the issue.Assistant
All 3 tests pass. Now let me run the existing declarative tests to make sure I didn't break anything. The
*IT.javatests likely require a real LLM. Let me check how ITs are gated and run the non-IT unit tests.Assistant
The DeclarativeAgentIT requires both OPENAI_API_KEY and GOOGLE_AI_GEMINI_API_KEY env vars, so it'll be skipped here. Let me check env and run the full agentic test suite (non-IT unit tests should run; ITs will be skipped).
Assistant
Let me check the overall test summary from surefire reports.
Assistant
Let me get a cleaner summary of the test results.
Assistant
48 tests ran, all passing. The ITs requiring API keys are disabled. Let me verify which test classes ran to ensure the relevant declarative tests executed.
Assistant
The unit tests all pass, including
ChatSupplierParameterResolverTestwhich exercises the parameterized@ChatModelSupplierpath that myconfigureChatModelextraction touches.Now let me check the skills module tests to see if there's an agentic integration test that could be affected, and verify the Skills
isDynamic()behavior matches the issue scenario.Assistant
The skills module doesn't depend on agentic, and there's no agentic+skills integration test. My repro test simulates the issue's scenario with a custom non-dynamic ToolProvider named "activate_skill", which is functionally equivalent.
Let me verify there are no other places that might double-register tools, and check the AbstractServiceBuilder for composed agents.
Assistant
No toolProvider usage in the composed builders - only the leaf
AgentBuilderregisters tool providers, and my fix prevents the double registration there.Now let me remove my temporary repro test file (per the instructions not to add tests) and review the final diff.
Assistant
The diff is clean and minimal. The constructor path (2-arg → 5-arg) retains identical behavior; only the re-configuration path (4-arg) changed to avoid re-registering declarative suppliers.
Let me run the agentic unit tests one final time to confirm everything still passes after removing the temp test.
Assistant
Let me verify there's no stale surefire report being counted (since I removed the temp test).
Assistant
The stale
ToolProviderDupReproTestreport is leftover from the previous run (surefire doesn't delete old reports). All existing tests pass with 0 failures/errors. Let me clean the stale reports and do a final confirmation run of the existing unit tests.Assistant
Now the count is 45 (existing tests only), all passing with 0 failures/errors.
Let me do a final review of the complete modified
DeclarativeUtilsection to ensure correctness.Assistant
The code is correct and clean. Let me verify no other modules call the changed
DeclarativeUtil.configureAgentmethods (to ensure no downstream breakage).Assistant
The only callers of
configureAgentare within the agentic module (AgenticServices lines 338/598 for the 4-arg, AgentBuilder line 141 for the 2-arg). No external modules call it. My change is isolated.Let me check the
StoryCreatorWithConfigurableStyleEditortest which uses a configurator, to confirm the configurator is still applied correctly by my change.Assistant
This test uses
createAgenticSystem(class, chatModel, configurator)where the configurator modifies a subagent'soutputKey. My change preserves configurator application (it's still applied in the 4-arg, after the constructor configured the suppliers). The configurator still runs last, so it can still override supplier-set values—identical end state.Let me view the final diff once more 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.