ISSUE DESCRIPTION [BUG] @Tool methods not inherited from superclas @Tool methods not…
- User
ISSUE DESCRIPTION
[BUG] @Tool methods not inherited from superclas
@Toolmethods not inherited from superclass. I think this is a bug as this is something that should work.I didn't 100% verify this, but my inherited tools weren't being called and the LLM was stating that it didn't know what tool to call. Once I put the tool methods back in the concrete class, things worked, so I'm assuming that this bug report is true.
This should be done in LC4J too if it already doesn't work there.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /langchain4j, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand what we're working with.
Assistant
Let me look at the langchain4j-core module since that's likely where the tool-related code is.
Assistant
Let me look at the ToolSpecifications class which is likely where the bug resides:
Assistant
I see the bug! The
toolSpecificationsFrommethod usesgetDeclaredMethods()which only returns methods declared in the specific class, not inherited methods. Let me look at the test file and other related files:Assistant
Let me check if there are similar tool specifications code in other modules that need to be fixed:
Assistant
Let me check the DefaultToolExecutor and ToolService files for similar issues:
Assistant
The DefaultToolExecutor.findMethod method also has the bug! It only searches declared methods, so if a method is overridden in a subclass, the inherited method won't be found. Wait, actually, in
findMethod, we're searching for the method to execute, and if a method is overridden,getDeclaredMethods()would find it in the subclass. But if a method is just inherited without overriding,getDeclaredMethods()won't find it. That's the bug.Let me look at ToolService.java too
Assistant
Now let me look for similar patterns in other parts of the code:
Assistant
Let me look at the getAnnotatedMethod utility since it's used in ToolService:
Assistant
check if there's any related issue in AgenticServices.java:
Assistant
check if there are any existing utilities to get all methods including inherited ones:
Assistant
me see how ReflectionUtils is used in the spring project:
Assistant
Spring's
ReflectionUtils.getDeclaredMethods(clazz)walks the class hierarchy. But we don't have this in our codebase. We need to implement it manually.Now let me first write a reproduction script to confirm the bug. Let me check the tests directory to understand how to write a test:Let me first write a reproduction script to confirm the bug. Let me look at the langchain4j-core test infrastructure:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[ls langchain4j-core/src/test/java/dev/langchain4j/agent/tool/]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me run a command to investigate the test infrastructure
Assistant
Let me create a reproduction script to verify the bug. First let me check what build tools are available:
Assistant
Let me create a test script to confirm the bug exists:
Assistant
I've confirmed the bug! Now let me fix the code. First, let me modify
ToolSpecifications.java:Assistant
let me modify the
ToolSpecifications.javafile to walk the class hierarchy:Assistant
Let me try the edit command with a different syntax for multi-line text:
Assistant
Let me try a different approach using sed:
Assistant
let me compile and run the test to verify the fix works:
Assistant
Need to add slf4j to the classpath:
Assistant
This is a Jackson version mismatch. Let me use the right jackson version that matches langchain4j-core. Let me check the langchain4j-core/pom.xml:
Assistant
The fix works. Let me write a more comprehensive test to verify edge cases:
Assistant
There's a problem with the override case. When a method is overridden, the validation throws because it thinks there are duplicates. Let me fix this properly - we need to handle overrides correctly. The subclass version should take precedence:
Assistant
Wait, let me check the test case more carefully. The validation is detecting duplicates, but my dedupe should work. Let me trace through:
Assistant
OK, so you can't override void with String. Let me update the test case
Assistant
Wait, the test should pass. Let me debug by checking if the class was properly recompiled:
Assistant
I see the bug now. The record uses
Class<?>[]which has reference equality. Let me fix this by overriding equals/hashCode:Assistant
Let me verify the Java version is supported:
Assistant
tests pass! Now let me fix the other two places where similar issues exist:
Assistant
Now let me fix ToolService.java's findTools method:
Assistant
Let me check the imports in ToolService.java:
Assistant
, HashSet import is already there. Let me clean up by using
Arrays.toStringwith proper import:Assistant
Now let me compile the langchain4j module:
Assistant
Now let me run the existing tests to make sure I didn't break anything:
Assistant
18 existing tests pass. Let me also run the tests for the langchain4j module:
Assistant
me think about edge cases more. Let me also test for overloaded methods:
Assistant
Assistant
Let me run the tests for the langchain4j-core module
Assistant
8 tests pass in langchain4j-core. Let me run the langchain4j module tests:
Assistant
All tests pass! Let me also run the agentic tests to be safe:All 1383 tests pass in langchain4j. Let me also check the agentic tests since
AgenticServiceshas a similar pattern (but for@Agentstatic methods):]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && mvn -pl langchain4j-agentic -am compile -DskipTests 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Let me run the agentic tests to verify they still work:
Assistant
Let me look at the final state of the three modified files to ensure they look good:All 47 tests pass. Let me look at the final state of the modified files:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && cat langchain4j-core/src/main/java/dev/langchain4j/agent/tool/ToolSpecifications.java]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me look at the file:
Assistant
All three files look good. Now let me think about whether there are any other places where this issue might exist.
Wait, I'm checking the issue title more carefully. The issue title says "@Tool methods not inherited from superclass" and the description says "This should be done in LC4J too". LC4J probably refers to LangChain4j itself.
Let me check if there are other places that might have similar issues:
DefaultToolExecutor.findMethod- DONEToolService.findTools- DONEToolSpecifications.toolSpecificationsFrom- DONE
But let me also check if there's a similar pattern in the agentic framework:Let me also check the agentic framework for similar issues:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && grep -rn "@Tool" langchain4j-agentic --include="*.java" | head -20]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me search for more patterns that might be affected:
Assistant
Let me look at the AgenticServices one to see if it might have a similar issue, but I already checked - it's looking for static @Agent methods, which can't be inherited.
Let me also look at GenericClassMetadataProviderFactory to see if it's
Assistant
This is in the integration-tests directory, so it's probably not part of the main code we need to fix. Let me check the SPI for classloading:Let me check the SPI for classloading to see if it affects anything:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && find . -path "/spi/classloading/" -type f | head -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me run a command to search for classloading SPI
Assistant
Interesting!
ReflectionBasedClassMetadataProviderFactory.getNonStaticMethodsOnClassusesgetMethods()which DOES include inherited methods (but only public ones).But the other implementations (
SpringClassMetadataProviderFactoryandGenericClassMetadataProviderFactory) usegetDeclaredMethods()which does NOT include inherited methods.However, this is for class metadata providers (not specifically for @Tool). Let me check where these are used:
Assistant
This is used for guardrails, not for @Tool. The issue specifically mentions @Tool, so I won't change this.
However, the
ReflectionBasedClassMetadataProviderFactoryusesgetMethods()which only returns public methods, while the other implementations usegetDeclaredMethods()which is more inclusive but doesn't include inherited methods.This is outside the scope of the bug report though. Let me focus on the @Tool issue.
Let me look at the kotlin module since the user mentioned "LC4J":Let me check the kotlin module too since the issue mentioned LC4J:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && grep -rn "getDeclaredMethods|getMethods" --include=".kt" --include=".java" langchain4j-kotlin 2>/dev/null | head -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me search for getDeclaredMethods and getMethods in the kotlin module
Assistant
Kotlin module uses the Java code. Let me check if there are other modules that have tool-related code that might need to be fixed:
Assistant
Just the three files I've already fixed. Let me verify all changes are correct by looking at the diffs:Just the three files I've already fixed. Let me verify all changes are correct by looking at the diffs:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && git diff --stat]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me view the diff to verify changes:
Assistant
Assistant
run the test to verify the fix works:
Assistant
The file wasn't created because the last command was blocked. Let me create it now.
Assistant
The issue is fixed. Let me also write a test for ToolService.findTools and DefaultToolExecutor.findMethod:Let me also test the ToolService.findTools and DefaultToolExecutor.findMethod:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cat > /tmp/reproduce/IssueTestToolService.java << 'JEOF' import dev.langchain4j.agent.tool.Tool; import dev.langchain4j.service.tool.AiServiceTool; import dev.langchain4j.service.tool.ToolService; import java.lang.reflect.Method; import java.util.List;
public class IssueTestToolService { public static class BaseClassWithTools { @Tool("Get the current weather for a location") public String getWeather(String location) { return "Weather in " + location + ": sunny"; } }
codeAssistant
Let me check the langchain4j module is built and run my test:
Assistant
The fix works for ToolService.findTools too. Let me also test DefaultToolExecutor.findMethod:Let me also test DefaultToolExecutor.findMethod (which is used when the LLM calls a tool to find the method to invoke):]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[printf '%s\n' 'import dev.langchain4j.agent.tool.Tool;' 'import dev.langchain4j.agent.tool.ToolExecutionRequest;' 'import dev.langchain4j.service.tool.DefaultToolExecutor;' 'import java.lang.reflect.Method;' '' 'public class IssueTestDefaultToolExecutor {' ' public static class BaseClassWithTools {' ' @Tool' ' public String getWeather(String location) {' ' return "Weather in " + location + ": sunny";' ' }' ' }' ' ' ' public static class ConcreteClassWithoutTools extends BaseClassWithTools {' ' }' ' ' ' public static void main(String[] args) throws Exception {' ' ConcreteClassWithoutTools obj = new ConcreteClassWithoutTools();' ' ' ' ToolExecutionRequest request = ToolExecutionRequest.builder()' ' .id("test")' ' .name("getWeather")' ' .arguments("{"location": "Paris"}")' ' .build();' ' ' ' DefaultToolExecutor executor = new DefaultToolExecutor(obj, request);' ' String result = executor.execute(request, null);' ' System.out.println("Result: " + result);' ' ' ' if (result.contains("Paris") && result.contains("sunny")) {' ' System.out.println("ISSUE IS FIXED for DefaultToolExecutor");' ' } else {' ' System.out.println("ISSUE IS NOT FIXED for DefaultToolExecutor");' ' }' ' }' '}' > /tmp/reproduce/IssueTestDefaultToolExecutor.java ls /tmp/reproduce/IssueTestDefaultToolExecutor.java]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me run the test for DefaultToolExecutor:
Assistant
test seems to work (the method is found and called) but my test data was malformed. Let me check the existing test to see the correct way to test:
Assistant
Looking at the existing test, I see that they use the ToolExecutionRequest with the arguments as a JSON string. Let me check what JSON format is expected:Looking at the existing test, they use
argumentsas a JSON string. Let me check how the JSON should be formatted:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && grep -B 2 -A 20 "execute_with_argument_value_might_be_null" langchain4j/src/test/java/dev/langchain4j/service/tool/DefaultToolExecutorTest.java | head -60]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
look at the test file again:
Assistant
I see - the arguments use
arg0,arg1, etc. (matching parameter indices). Let me fix my test:Assistant
All three places are fixed. Now let me clean up my test files:All three places are fixed and verified. Let me clean up my test files and also run all the tests once more to confirm nothing is broken:All three places are fixed and verified. Let me clean up my test files and also run all the tests once more to confirm nothing is broken:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[rm -rf /tmp/reproduce && ls /tmp/reproduce 2>&1]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me clean up the test files and run all tests once more:
Assistant
All tests pass:
- langchain4j-core: 1088 tests pass
- langchain4j: 1383 tests pass
- langchain4j-agentic: 47 tests pass
Now let me verify the final state of the code by viewing the diff:All tests pass. Let me verify the final state of the code:All tests pass:
- langchain4j-core: 1088 tests pass
- langchain4j: 1383 tests pass
- langchain4j-agentic: 47 tests pass
Let me verify the final state of the code:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && git diff --stat]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me run a clean command:
Assistant
Good - just the three files modified. Let me verify the final state by looking at all three files:Good - just the three files modified. Let me verify the final state by looking at all three files:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /langchain4j && git diff langchain4j-core/src/main/java/dev/langchain4j/agent/tool/ToolSpecifications.java langchain4j/src/main/java/dev/langchain4j/service/tool/DefaultToolExecutor.java langchain4j/src/main/java/dev/langchain4j/service/tool/ToolService.java]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Assistant
run a clean command:
Assistant
Let me verify all my changes are correct and the tests pass one more time:
Assistant
Assistant
Let me try without the heredoc since it's been problematic:
Assistant
All three places are now working correctly with inherited @Tool methods:
ToolSpecifications.toolSpecificationsFrom- returns 2 specs ✓ToolService.findTools- returns 2 tools ✓DefaultToolExecutor.execute- actually executes the inherited method ✓
Let me clean up the test files and submit:All three places are working correctly with inherited @Tool methods:
ToolSpecifications.toolSpecificationsFrom- returns 2 specs ✓ToolService.findTools- returns 2 tools ✓DefaultToolExecutor.execute- actually executes the inherited method ✓
Let me clean up the test files and submit:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[rm -rf /tmp/finaltest && rm -rf /tmp/edit_*.pl && ls /tmp/]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me clean up:
Assistant
Assistant
run the test directly:
Assistant
So:
MyClass.getDeclaredMethods()doesn't includemyDefaultMethod()since the method is from the interfaceMyInterface.getDeclaredMethods()includes since it's a default method
Assistant
me cd back to the langchain4j directory and prepare to submit:
Assistant
My changes are clean and ready. Let me submit:
Summary of changes:
-
langchain4j-core/src/main/java/dev/langchain4j/agent/tool/ToolSpecifications.java- ThetoolSpecificationsFrom(Class<?>)method now walks up the class hierarchy to find inherited@Toolmethods, instead of just looking at methods declared directly in the class. A new helper methodallDeclaredMethodsIncludingInheritedcollects all methods from the class and its superclasses (excludingObject), and aMethodSignaturehelper class is used to correctly handle method overrides (the subclass's version takes precedence). -
langchain4j/src/main/java/dev/langchain4j/service/tool/DefaultToolExecutor.java- The method now walks up the class hierarchy when looking for a method by name. This is needed so that when the LLM calls a tool, the executor can find inherited methods.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.