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.
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
I'll start by exploring the repository structure to understand the codebase and find the relevant files related to
@Toolannotation and its processing.Assistant
Let me search for files related to
@Toolannotation and tool processing to understand the codebase better.Assistant
Let me search for the Tool annotation definition and the code that processes tools.
Assistant
Now let me search for the code that extracts/processes the @Tool methods:
Assistant
Now I can see the issue! In the
findToolsmethod, the code only looks atobjectWithTools.getClass().getDeclaredMethods(), which doesn't include inherited methods. ThegetDeclaredMethods()method only returns methods declared in that specific class, not methods inherited from superclasses.Let me verify this by checking the Java documentation and looking for tests that might confirm this behavior:
Assistant
Now let me search for any existing tests related to inheritance to understand if this was intended or not:
Assistant
Let me check if there are any existing tests for @Tool methods:
Assistant
Let me check the ToolServiceTest to see if there are any tests for tool inheritance:
Assistant
Let me look for other places in the codebase that might be scanning methods with annotations to see the pattern used:
Assistant
Let me check how other parts of the codebase handle method scanning:
Assistant
Let me check the AiServiceValidation to see how it handles method scanning:
Assistant
Interesting! The
AiServiceValidationusesserviceClass.getMethods()which includes inherited methods, whereasToolService.findTools()usesgetDeclaredMethods()which doesn't include inherited methods. This confirms the bug!Now let me create a test script to reproduce the issue:
Assistant
I seem to have lost track. Let me continue - I created a test file to reproduce the issue. Now I need to compile and run it to confirm the bug exists.
Assistant
Let me check if the project needs to be built first, or if I can run tests directly.
Assistant
Let me look at the ToolService.java file more carefully to understand what needs to be changed:
Assistant
Let me check the
getAnnotatedMethodutility to understand what it does:Assistant
Let me check if there's an existing utility method that handles finding methods from superclasses:
Assistant
The user wants me to continue analyzing the code to understand how to fix the bug. Let me look at the Utils.java file to understand what utilities are available.
Assistant
The user's session timed out and is now being resumed. Let me continue with my analysis. I was looking at the code to understand how to fix the bug where @Tool methods from superclasses are not being inherited.
I've identified the issue:
- In
ToolService.findTools()method at line 229, it usesobjectWithTools.getClass().getDeclaredMethods()which only returns methods declared in that specific class, not inherited methods. - The fix should be to use
getMethods()instead ofgetDeclaredMethods()to include inherited methods.
Let me continue with creating a test to reproduce the issue and then fix it.
- In
Assistant
Now I understand the structure. This is a Maven project. Let me:
- First, look at the exact code in ToolService.java that needs to be fixed
- Create a simple test to reproduce the issue
- Fix the code
- Run the test to confirm the fix works
- Run existing tests to make sure I didn't break anything
Let me start by looking at the exact section of ToolService.java that needs fixing.
Assistant
Now I can see the issue. Let me create a test to reproduce the bug first, then fix it.
Assistant
The user is asking me to create the test file again. I already created it in the previous step, but let me verify it exists and then compile and run it to reproduce the bug.
Assistant
Good, the test file exists. Now let me compile and run it to confirm the bug. First, I need to build the necessary modules.
Assistant
Good! The compilation succeeded. Now let me compile and run my test to reproduce the bug:
Assistant
Good! Now let me run the test to reproduce the bug:
Assistant
I need to include Jackson in the classpath. Let me simplify the test by using Maven to run it properly:
Assistant
The user is telling me that my action could not be parsed in JSON. I need to make sure I'm properly formatting my tool calls. Let me run the test to reproduce the bug.
Assistant
Perfect! The test confirms the bug:
- Expected 2 tools but only found 1 (derivedTool) - the inherited baseTool was not found
- Expected 3 tools but only found 1 (level3Tool) - the inherited level1Tool and level2Tool were not found
Now I'll fix the issue by changing
getDeclaredMethods()togetMethods()in ToolService.java:Assistant
The user hasn't said anything new. They're asking me to continue with my reasoning and action. Let me fix the bug by changing
getDeclaredMethods()togetMethods()in the ToolService.java file.I need to edit line 229 of ToolService.java to change
getDeclaredMethods()togetMethods().Assistant
Good! I've made the change. Let me verify it was applied correctly:
Assistant
Perfect! The change was applied. Now let me recompile and run the test again to confirm the fix works:
Assistant
Let me run the test again and see the full output:
Assistant
Excellent! The test passes now. The fix works! Now let me make sure I didn't break any existing tests by running the existing tests that might be affected by this change.
Assistant
Good! Now let me run more comprehensive tests related to tools to make sure nothing is broken:
Assistant
I'm seeing some test failures. Let me investigate what's going wrong. The errors might be related to my change. Let me look at the specific failures to understand what's happening.
Assistant
I see there are test failures. The error message is strange - it says the objects "does not have any methods annotated with @Tool" which shouldn't happen if we're now using
getMethods()which should find more methods. Let me investigate what's happening.Assistant
I see the issue! The
HelloWorldclass has a package-private methodhello(). When usinggetMethods(), it only returnspublicmethods, including inherited public methods. ButgetDeclaredMethods()returns all methods declared in that class regardless of access modifier.So the issue is that by changing to
getMethods(), we're now missing package-private, protected, and private methods that were being picked up bygetDeclaredMethods().The solution is to combine both approaches - use
getMethods()to get public inherited methods, and also includegetDeclaredMethods()to get non-public methods declared in the class itself. However, we need to avoid duplicates.Let me check if the test was working before my change:
Assistant
I need to fix my approach. The issue is that
getMethods()only returns public methods (including inherited public methods), butgetDeclaredMethods()returns all methods declared in the class regardless of visibility. I need to combine both to support inherited tools while also supporting non-public methods.Let me create a proper solution that scans all classes in the hierarchy:
Assistant
Let me now create a helper method that collects all methods from the class hierarchy:
Assistant
Now I need to create a proper fix. The approach should be to walk up the class hierarchy and collect all declared methods. Let me add a helper method and update the findTools method:
Assistant
Now let me add this helper method to ToolService.java and update the findTools method to use it:
Assistant
Let me add the helper method and update the findTools method:
Assistant
Now let me update the findTools method to use this helper method instead of
getMethods():Assistant
I need to continue with fixing the issue. I was in the middle of updating the findTools method to use the new getAllMethods helper method. Let me update the findTools method to use getAllMethods instead of getMethods.
Assistant
Good! I've replaced the loop to use getAllMethods instead of getMethods. Now let me verify the change was applied correctly and then run the tests to confirm everything works.
Assistant
Perfect! The change was applied. Now I can see that the
findToolsmethod now usesgetAllMethods(objectWithTools)instead ofobjectWithTools.getClass().getMethods().Let me verify that the helper method was added correctly by checking the code around line 207.
Assistant
Great! The helper method was added. Now let me compile and run the tests to verify everything works:
Assistant
Good! The ToolInheritanceTest passes. Now let me run the other tests that were failing before (HallucinatedToolNameStrategyTest and others) to confirm they also pass now.
Assistant
Great! The HallucinatedToolNameStrategyTest passes now. Let me run the AiServicesWithToolsWithDefaultValuesTest that was also failing before.
Assistant
The tests are all passing. Let me run all the tool-related tests to make sure everything works correctly.
Assistant
Good! All the tests that were previously failing are now passing. Let me run a broader set of tool-related tests to make sure I haven't broken anything else.
Assistant
Perfect! All 219 tool-related tests pass successfully! Now let me review my changes to make sure they're complete and correct:
Assistant
Perfect! My changes are complete. Let me now create a summary of what I've done and verify that my fix is correct by running some additional edge case tests:
-
Bug identified:
ToolService.findTools()was usinggetDeclaredMethods()which only returns methods declared in the specific class, not inherited methods from superclasses. -
Fix implemented: Added a helper method
getAllMethods()that walks up the class hierarchy and collects all declared methods from each class, avoiding duplicates. -
Tests created: Created
ToolInheritanceTestthat verifies tools from superclasses are inherited. -
All tests pass: All 219 tool-related tests pass successfully.
Let me add one more edge case test to verify my fix handles more complex scenarios:
-
Assistant
The user is asking me to continue. I was in the process of adding edge case tests to make sure my fix handles overridden methods and abstract classes correctly. Let me run these new tests to verify they pass.
Assistant
The user is telling me to continue with my thinking. Let me check what happened with the test - it seems like the build failed. I need to check what went wrong with my test additions.
Assistant
I see the issue - my test file has compilation errors. The problem is that I tried to add local classes inside methods (which is a Java 25 feature), and I also have a syntax error with the closing brace. Let me fix the test file by reverting it to the working version and not adding those problematic tests.
Actually, let me just remove the test file I created since it was just for verification and the fix is already confirmed working by the original tests that passed.
Assistant
The user is asking me to continue. Let me think about what happened:
- I created a test file
ToolInheritanceTest.javato verify that my fix works - I then tried to add more edge case tests to this file, but those tests had compilation errors (using Java 25 features like local classes which aren't supported in Java 17)
- I tried to checkout the file to revert it, but git said the file doesn't exist in git (because it's a new file I created)
Now I need to fix the test file by removing the problematic tests I added. Let me check the current state of the file and fix it.
- I created a test file
Assistant
I see the issue - I appended the new test methods after the closing brace
}of the class, which broke the syntax. Let me remove those problematic tests by truncating the file to only include the original working tests.Assistant
Let me check what's in the file now and make sure it's properly formatted.
Assistant
Let me recreate the test file properly with just the two original working tests.
Assistant
Let me run the inheritance test again to confirm it passes with the corrected test file.
Assistant
Good! The inheritance test passes. Now let me run all the tool-related tests again to make sure everything still works correctly, and then I'll provide a summary of my changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →