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 what we're dealing with. Let's first check what's in the current directory.
- Assistant
Now I need to look for files related to
@Toolannotation. Let me search for that. - Assistant
Let me find the
@Toolannotation and the tool scanning/processing logic to understand how tools are discovered. - Assistant
Let me look at the ToolService.java to understand how tools are discovered and see if the issue is related to not scanning superclass methods.
- Assistant
Let me look at the
findToolsmethod more closely. The issue is in this line:for (Method method : objectWithTools.getClass().getDeclaredMethods()) {getDeclaredMethods()only returns methods declared in the current class, not inherited methods from superclasses. To include inherited methods, we should usegetMethods()instead.However,
getMethods()returns all public methods including those from interfaces and Object class. We probably need to walk up the class hierarchy manually to get all declared methods from the class and its superclasses, but excluding methods fromObject.Let me also check the
ToolSpecificationsclass since that might also be relevant. - Assistant
Now I understand the issue. Let me examine the
findToolsmethod more closely. The problem is in lineobjectWithTools.getClass().getDeclaredMethods()-getDeclaredMethods()only returns methods declared in the current class, NOT inherited methods from superclasses. - Assistant
Let me check what
getAnnotatedMethoddoes, and also look at existing tests for tool inheritance: - Assistant
Let me look at the
getAnnotatedMethodutility method and how it's used, and also thetoolSpecificationFrommethod. - Assistant
Now I understand the issue clearly. The
findToolsmethod usesobjectWithTools.getClass().getDeclaredMethods(), which only returns methods declared directly in the current class, not inherited methods from superclasses. Let me also check theToolSpecifications.toolSpecificationFrommethod and look at existing tests: - Assistant
Now I can see the problem clearly. There are TWO places where
getDeclaredMethods()is used instead of traversing the class hierarchy:ToolService.findTools()(line 229) usesobjectWithTools.getClass().getDeclaredMethods()ToolSpecifications.toolSpecificationsFrom(Class<?> classWithTools)usesclassWithTools.getDeclaredMethods()
Let me also check the existing tests for tools and look at the test structure:
- Assistant
Let me look at the
DefaultToolExecutorto understand how tool methods are invoked - since if a method is inherited from a superclass, we need to ensure it can still be invoked on the subclass instance. - Assistant
Now I have a clear picture of the issue. The bug is in multiple places where
getDeclaredMethods()is used:ToolService.findTools()usesobjectWithTools.getClass().getDeclaredMethods()ToolSpecifications.toolSpecificationsFrom(Class<?>)usesclassWithTools.getDeclaredMethods()DefaultToolExecutor.findMethod()usesobject.getClass().getDeclaredMethods()
All three need to walk up the class hierarchy. Let me also check the existing tests:
- Assistant
Let me check the ToolSpecificationsTest and ToolService tests to understand the existing tests.
- Assistant
Now let me check if there are any existing tests for inherited tools:
- Assistant
Now I understand the issue clearly. The problem is in
ToolService.findTools()method at line 229 which usesobjectWithTools.getClass().getDeclaredMethods()- this only returns methods declared in the current class, not inherited methods from superclasses. The same issue exists inToolSpecifications.toolSpecificationsFrom(Class<?>).Let me first create a reproducer to verify the issue exists:
- Assistant
Let me explore the build system.
- Assistant
Let me look at the build structure.
- Assistant
Now let me look at the
getAnnotatedMethodutility to understand its behavior: - Assistant
Now I understand the issue completely. Let me create a reproducer to verify the bug:
- Assistant
Let me write a reproducer test first to confirm the issue:
- Assistant
Let me run this test to confirm the issue exists.
- Assistant
The HOME variable isn't set, let me try setting it.
- Assistant
The issue is confirmed. Now let me also create a reproducer for
ToolService.findTools(): - Assistant
Let me also check other places where
getDeclaredMethods()is used in the codebase to see if there are other places that need fixing. - Assistant
So there are 4 places where
getDeclaredMethods()is used:DefaultToolExecutor.findMethod()- line 71ToolService.findTools()- line 229ToolSpecifications.toolSpecificationsFrom(Class<?>)- line 63AgenticServices- line 741
Let me check the AgenticServices one to see if it's also related to tool discovery:
- Assistant
Now I have a clear understanding. The issue is in three places where
getDeclaredMethods()is used, which only returns methods declared in the current class, not inherited from superclasses. Let me implement the fix:- Add a utility method to collect all methods from a class hierarchy
- Use it in
ToolSpecifications.toolSpecificationsFrom(Class<?>) - Use it in
ToolService.findTools() - Use it in
DefaultToolExecutor.findMethod()
Let me now implement the fix. I'll need to modify three files. Let me start with
ToolSpecifications.java: - Assistant
Now let me look at the imports in ToolSpecifications.java to see what's already imported.
- Assistant
Now let me check if there are any existing utility methods for walking class hierarchies:
- Assistant
Now I have a clear understanding. The fix needs to be applied in three places:
ToolSpecifications.toolSpecificationsFrom(Class<?>)- usesgetDeclaredMethods()ToolService.findTools()- usesgetDeclaredMethods()DefaultToolExecutor.findMethod()- usesgetDeclaredMethods()
Let me implement the fix. I'll create a helper method to walk up the class hierarchy:
- Assistant
Now let me fix all three locations. Let me start with
ToolSpecifications.java: - Assistant
Now let me fix the
ToolSpecifications.toolSpecificationsFrom(Class<?>)method to walk up the class hierarchy. I need to add a helper method to collect all declared methods from the class hierarchy: - Assistant
Now let me fix
ToolService.findTools(): - Assistant
Now let me fix
ToolService.findTools(). I need to replaceobjectWithTools.getClass().getDeclaredMethods()with a method that walks the class hierarchy. - Assistant
Now I need to add the
getAllDeclaredMethodshelper method to theToolServiceclass. Let me add it after thecreateToolExecutormethod: - Assistant
Now I need to add the
getAllDeclaredMethodshelper method toToolService. Let me add it after thecreateToolExecutormethod: - Assistant
Now I need to make sure the necessary imports are in
ToolService.java. Let me check and add them: - Assistant
Good,
LinkedHashMapis already imported. Now let me fixDefaultToolExecutor.findMethod(): - Assistant
Now I need to fix the
findMethodmethod inDefaultToolExecutor.javato also look at superclass methods: - Assistant
Now let me fix the
findMethodmethod inDefaultToolExecutor.java: - Assistant
Now let me also check and fix the
AgenticServices.javafile which also hasgetDeclaredMethods(): - Assistant
Let me now run the test to verify my fix works:
- Assistant
The
ToolSpecificationsfix works. Now let me also run the existingToolSpecificationsTestto make sure nothing is broken: - Assistant
All existing tests pass. Now let me test the
ToolServiceInheritanceTest: - Assistant
Excellent! Now let me run the existing tests in the
langchain4jmodule to make sure nothing is broken: - Assistant
Let me also run some more tool-related tests to be thorough:
- Assistant
Let me also run the AiServicesWithTools tests to make sure the tool integration tests still pass:
- Assistant
Let me also fix the AgenticServices.java issue since it's the same pattern, and also run the FunctionMapperTest:
- Assistant
Let me focus on the core issue. Let me run the FunctionMapperTest to make sure it still passes:
- Assistant
All tests pass. Now let me also add more comprehensive edge case tests and run them:
- Assistant
Now let me run all the edge case tests:
- Assistant
All 7 edge case tests pass. Now let me clean up the test files I created (since they're not part of the fix) and run the broader test suite:
- Assistant
Now let me run the full test suite of the affected modules:
- Assistant
All 1088 tests pass in langchain4j-core. Now let me run the test suite for the
langchain4jmodule: - Assistant
All 1383 tests pass in langchain4j. Let me verify all the changes I've made:
- Assistant
The diff looks good. However, I notice that I have duplicate
getAllDeclaredMethods()andmethodKey()helper methods in bothToolSpecifications.javaandToolService.java. Let me refactor to put the shared utility inUtils.javato avoid duplication.I notice there's code duplication with the
getAllDeclaredMethods()andmethodKey()methods in bothToolSpecifications.javaandToolService.java. Let me refactor to put the shared utility inUtils.java: - Assistant
Let me look at the end of Utils.java to find a good place to add the utility method:
- Assistant
Let me add the utility method to Utils.java after the
getAnnotatedMethodmethod: - Assistant
Let me add the utility method to Utils.java right after
getAnnotatedMethod: - Assistant
Let me use a different approach to edit the file:
- Assistant
Let me try with python or java instead.
- Assistant
Let me use sed to make the edit:
- Assistant
Let me use a different approach. I'll create a sed script to insert the new methods after line 553.
- Assistant
I need to insert the new methods after line 553 (the closing brace of getAnnotatedMethod). Let me use a heredoc with cat and sed.
- Assistant
Let me verify the insertion was correct:
- Assistant
Now I need to check if
LinkedHashMapis already imported in Utils.java: - Assistant
LinkedHashMapis used but not imported! Let me add it: - Assistant
I need to add
import java.util.LinkedHashMap;afterimport java.util.List;. Let me do this with sed: - Assistant
Let me verify the import was added:
- Assistant
Good. Now I need to update
ToolSpecifications.javaandToolService.javato use the shared utility fromUtils.javainstead of having their own local copies. Let me update ToolSpecifications.java first: - Assistant
Now let me replace the local
getAllDeclaredMethodsandmethodKeymethods inToolSpecifications.javawith a call toUtils.getAllDeclaredMethods: - Assistant
Now I need to remove the local
getAllDeclaredMethodsandmethodKeymethods since we're now using the one from Utils: - Assistant
Let me use sed to remove the local method definitions.
- Assistant
Let me reconstruct the file without the local methods:
- Assistant
Let me verify:
- Assistant
Good. Now I also need to remove the unused LinkedHashMap import since it's no longer used locally in ToolSpecifications.java:
- Assistant
Now let me also update ToolService.java to use the shared utility from Utils.java instead of having its own local copy:
- Assistant
Let me first add the import for
getAllDeclaredMethods: - Assistant
Now I need to remove the local
getAllDeclaredMethodsandmethodKeymethods from ToolService.java: - Assistant
Let me remove the local helper methods:
- Assistant
Let me verify the changes:
- Assistant
There seems to be an extra closing brace. Let me check:
- Assistant
There's an extra
}on line 215 which is a leftover from the removal. Let me fix this: - Assistant
Let me look at the file more carefully:
- Assistant
I see the issue. There's a leftover
}at line 209. Let me remove it: - Assistant
Let me verify:
- Assistant
Good. Let me verify the
findToolsmethod still usesgetAllDeclaredMethods: - Assistant
Good. Now let me also check if the LinkedHashMap import is still needed in ToolService.java (it should be, since it's used elsewhere):
- Assistant
Good, the import is still needed. Now let me run the tests again to make sure everything still works:
- Assistant
The LinkedHashMap import was removed from ToolSpecifications.java but it's still used elsewhere in that file. Let me check line 170:
- Assistant
The LinkedHashMap import was removed from ToolSpecifications.java but it's still used elsewhere in that file. Let me add it back:
- Assistant
Let me run the tests again:
- Assistant
All tests pass in langchain4j-core. Now let me run the langchain4j module tests too:
- Assistant
The
langchain4j-corehasn't been installed yet, so thelangchain4jmodule can't find the newgetAllDeclaredMethodsmethod. Let me installlangchain4j-corefirst. - Assistant
Now let me run the langchain4j tests:
- Assistant
All 1383 tests pass in langchain4j. Let me do a final review of all the changes:
- Assistant
The diff looks clean and correct. There's one extra blank line in ToolService.java, let me clean that up:
- Assistant
Let me remove the extra blank line:
- Assistant
Let me also check and clean the extra blank line in Utils.java:
- Assistant
Let me clean up the extra blank line in Utils.java:
- Assistant
Let me do a final diff review to make sure everything looks clean:
- Assistant
Let me install langchain4j-core again and run the final test suite:
- Assistant
Now let me run the final test suite for both modules:
- Assistant
Good, langchain4j-core tests pass. Let me also run the langchain4j tests:
- Assistant
All tests pass. Let me also check the FunctionMapperTest and the Google AI Gemini module:
- Assistant
All tests pass. Let me now clean up any temporary files:
- Assistant
Let me also check that no test files were left behind:
- Assistant
Let me do a final review of the complete diff:
- Assistant
The diff looks clean. There's a minor extra blank line in ToolService.java, let me clean it up:
- Assistant
Let me fix the double blank line:
- Assistant
Let me verify:
- Assistant
Still two blank lines. Let me fix this properly:
- Assistant
Let me verify the fix:
- Assistant
Let me try a different approach. Let me check the original file:
- Assistant
I see - the original has just one blank line. Let me fix my version:
- Assistant
Let me verify:
- Assistant
Still two blank lines. Let me look at this more carefully. It seems like line 206 is blank AND line 207 is blank. Let me check the line numbers more carefully:
- Assistant
Lines 207 and 208 are both empty. I need to remove one of them:
- Assistant
Let me verify:
- Assistant
Good. Now let me do a final diff to see the clean changes:
- Assistant
Wait, I see something concerning in the diff. It looks like a closing brace
}was removed from ToolService.java. Let me check: - Assistant
I see the problem! The closing brace of
createToolExecutorwas removed. Let me fix this: - Assistant
Let me verify:
- Assistant
That's messed up. Let me restore the file from git and redo the edit more carefully:
- Assistant
Now let me redo the edit more carefully. Let me first look at the original file:
- Assistant
Good. Now let me make the two changes to ToolService.java using sed:
- Assistant
Let me verify the changes:
- Assistant
Now the ToolService.java diff is clean. Let me verify the full diff:
- Assistant
Let me run the final test suite to make sure everything works:
- Assistant
Good. Now let me run the tests for both modules:
- Assistant
All tests pass. Let me also run the FunctionMapperTest one more time:
- Assistant
All tests pass. Let me clean up any remaining temporary files:
- Assistant
Let me also verify that the
UtilsTest.javafile doesn't need any updates:
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 →
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 →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →