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 start by understanding the repository structure and the issue. The bug is about duplicate
activate_skilltool definition when usingAgenticServices.createAgenticSystem(). Let me explore the relevant files.Let me first explore the repository structure, then look at the specific files mentioned in the stack trace and issue description.
Assistant
Let me look at the specific file mentioned in the issue:
langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgenticServices.javaAssistant
Let me look at both files.
Assistant
Let me just read the files using cat.
Assistant
Now let me look at the ToolService.java to understand the duplicate tool error:
Assistant
Let me look at the
addToolsmethod which is where the error occurs (lineAssistant
Let me look at the
addToolsmethod around line 391 and thecreateContextFromStaticToolsAndProvidersmethod around line 371.Assistant
Now let me look at the
createAgenticSystemmethod in AgenticServices.java:Assistant
Let me look at the
createAgenticSystemmethod moreAssistant
Let me look at
DeclarativeUtil.configureAgentand also theAgentBuilderclass:Assistant
Let me look at both files.
Assistant
Now let me look at AgentBuilder.java to understand how tools and tool providers are registered.
Assistant
Let me look at the
setupToolsmethod in AgentBuilder and theToolService.createContextFromStaticToolsAndProvidersmore carefully.Assistant
Let me look at the full
setupToolsmethod in AgentBuilderAssistant
Let me look at how AiServices handles
toolProvidersAssistant
Let me look at the relevant parts of AiServices.java for tool handling.
Assistant
Now let me look at the DefaultAiServices to understand the invocation flow
Assistant
Let me look at how
createContextis called in DefaultAiServices.Assistant
Let me now look at the full
createContextandcreateContextFromStaticToolsAndProvidersin ToolService.Assistant
Now I have a clear understanding of the issue. Let me verify my understanding by looking at the full flow:
createAgenticSystemcallsagentBuilder(agentServiceClass)which in theAgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)- this registersToolProvideronce- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)again - this registersToolProvidera second time - When tools are resolved, both
ToolProviderinstances provide the sameactivate_skilltool, causing the duplicate error
Let me verify this by checking the
configureAgentcalls.Assistant
Now let me check if there are any tests related to this behavior to understand the expected behavior better
Assistant
Let me look at the Skills.java to understand how
activate_skillgets added, and also check for existing tests of the duplicate tool detectionAssistant
Let me search for any existing tests related to this issue:
Assistant
Let me look at relevant test files to understand the test patterns:
Assistant
Let me now look at the
ToolProviderResultclass:Assistant
Let me search for tests that might rely on the duplicate tool exception:
Assistant
Now I have a complete understanding of the bug. Let me implement the fix.
The root cause:
configureAgentis called twice - once from theAgentBuilderconstructor and once fromcreateAgenticSystem. Each call invokes the@ToolProviderSuppliermethod, creating a newToolProviderinstance. Both instances end up in theToolService, and when their non-dynamic tools are resolved, the same tools (likeactivate_skill) are added twice, causing theIllegalConfigurationException.The fix: Change the
addToolsmethod inToolService.javato silently skip duplicate tools instead of throwing an exception. This handles deduplication at the lowest level.Assistant
Now let me implement the fix. I'll change the
addToolsmethod to skip duplicate tools instead of throwingAssistant
Let me apply the fix directly using the replace command:
Assistant
I need to use
gototo navigate to the right line first, then useedit.Assistant
Now I can edit the file. Let me replace lines 397-399:
Assistant
I need to fix the missing closing brace. Let me check the current state and fix it:
Assistant
I need to add the closing brace. Let me fix line 399:
Assistant
The fix looks correct. Now let me verify it compiles and run the relevant tests:
Assistant
Good, it compiles. Now let me search for any tests that test the
addToolsmethod or duplicate tool detectionAssistant
Let me check if there are tests for the
ToolProviderResultduplicate detection:Assistant
The fix looks correct. Let me now run the relevant tests to make sure nothing is broken. Let me first check what tests exist for the
ToolServiceand `AiServicesAssistant
Let me look at the ToolServiceTest to understand the existing test coverage
Assistant
Let me run the agentic tests that are related to declarative agent creation with tools:
Assistant
Now let me also try to compile the agentic module to make sure nothing is broken:
Assistant
Let me verify the final state of the fix:
Assistant
Let me run the unit tests for the
langchain4jmodule that don't require API keys:Assistant
Let me run a broader set of tool-related tests:
Assistant
Let me verify the diff of my changes:
Assistant
The fix is clean and handles the issue. Let me now also verify there are no compile issues with the full build of just the changed module:
Assistant
Let me also check if there are any tests that expect the
IllegalConfigurationExceptionwith "Duplicated definition"Assistant
The fix is complete. Let me now verify everything compiles and run a final check:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.