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 to understand the codebase and locate the issue about @Tool methods not being inherited from superclass. Let's first get a high-level view.
- Assistant
Let me look at the repository structure to understand the project better.
- Assistant
This is a Java repository (langchain4j). The issue is about
@Toolmethods not being inherited from superclass. I need to find where tools are processed and ensure they look at inherited methods as well.Let me look for the
@Toolannotation and the code that processes it. - Assistant
Let me look at how tools are discovered. The key issue is that
@Toolannotated methods in a superclass are not being discovered when scanning a subclass. Let me look at the tool service implementation. - Assistant
Let me look at the ToolService.java and DefaultToolExecutor.java to understand how tools are discovered.
- Assistant
I found the issue! In the
findToolsmethod, it usesobjectWithTools.getClass().getDeclaredMethods()which only returns methods declared in the class itself, not inherited methods from superclasses. Let me examine this more closely and also check related classes. - Assistant
Let me also check the DefaultToolExecutor and the Utils class for the
getAnnotatedMethodutility: - Assistant
Let me look at the
getAnnotatedMethodfunction to understand what it does. - Assistant
Now I understand the issue clearly. There are two places where
getDeclaredMethods()is used instead ofgetMethods():- In
ToolService.findTools()(line ~226):objectWithTools.getClass().getDeclaredMethods() - In
ToolSpecifications.toolSpecificationsFrom(Class<?>)(line ~66):classWithTools.getDeclaredMethods()
Both of these only return methods declared in the class itself, not inherited methods from superclasses. To fix this issue, we need to change both to use
getMethods()instead ofgetDeclaredMethods().Let me also check if there are any tests for this scenario to understand the expected behavior better.
- In
- Assistant
Let me check the exact line numbers of the methods we need to change:
- Assistant
Now let me look at the
findToolsmethod more carefully to understand the full context: - Assistant
Let me look at the
DefaultToolExecutorto understand how methods are invoked: - Assistant
Let me also check if there are any existing tests for tool inheritance:
- Assistant
Now let me understand the core issue more carefully by looking at the
findToolsmethod in detail: - Assistant
Now I understand the issue. The problem is in two places:
ToolService.findTools()(line 229): UsesgetDeclaredMethods()which only returns methods declared in the class itself, not inherited methods.ToolSpecifications.toolSpecificationsFrom(Class<?>)(line 63): UsesgetDeclaredMethods()which only returns methods declared in the class itself.
Let me also look at the
ToolSpecifications.toolSpecificationsFrommethod: - Assistant
Now let me check if there are other places in the codebase that might have the same issue:
- Assistant
Let me examine the DefaultToolExecutor.java to understand its context:
- Assistant
Now I can see the full picture. There are THREE places that need to be fixed:
ToolService.findTools()(line 229) - scans for @Tool annotationsToolSpecifications.toolSpecificationsFrom(Class<?>)(line 63) - scans for @Tool annotationsDefaultToolExecutor.findMethod()(line 71) - looks up method by name at execution time
All three use
getDeclaredMethods()instead ofgetMethods(), which prevents inherited methods from being discovered.Let me first create a reproduction script to verify the issue:
- Assistant
Let me first understand the build system to be able to create and run tests:
- Assistant
Let me first look at the existing test files for tool-related tests:
- Assistant
Let me look at the existing ToolServiceTest.java:
- Assistant
Let me check the ToolExecutorTest to understand how tests are structured:
- Assistant
Let me also look at the AgenticServices.java to see if it has the same issue:
- Assistant
Let me also look at DefaultToolExecutorTest.java:
- Assistant
Now I have a complete understanding of the issue. Let me look at how
DefaultToolExecutoris created from aToolExecutionRequest: - Assistant
Now I have a clear picture. Let me implement the fix. I need to change three locations to walk the class hierarchy instead of only looking at the direct class. Let me start with the fix:
- Assistant
Let me look at the imports of ToolService.java:
- Assistant
Now let me write a quick reproduction test to confirm the bug:
- Assistant
Let me compile and run it:
- Assistant
Let me look at the Tool annotation:
- Assistant
Now I understand the issue completely. Let me implement the fixes. There are three places that need to be changed:
ToolService.findTools()(line 229) - scans for@Toolannotations usinggetDeclaredMethods()ToolSpecifications.toolSpecificationsFrom(Class<?>)(line 63) - scans for@Toolannotations usinggetDeclaredMethods()DefaultToolExecutor.findMethod()(line 71) - looks up method by name usinggetDeclaredMethods()
All three use
getDeclaredMethods()which only returns methods declared in that specific class, not inherited methods from superclasses. Let me fix each one: - Assistant
Now let me fix the three files. Let's start with
ToolService.java: - Assistant
Let me check what imports are already available:
- Assistant
Now I need to fix the
ToolService.findTools()method. Let me implement the fix. The safest approach is to walk the class hierarchy usinggetDeclaredMethods()at each level: - Assistant
Let me use the edit command directly:
- Assistant
Now let me add the helper method and update the
findToolsmethod. Let me navigate to wherefindToolsis: - Assistant
Now let me add the helper method before
findTools: - Assistant
Now let me update the
findToolsmethod to usegetAllMethods: - Assistant
Good. Now let me fix
ToolSpecifications.toolSpecificationsFrom(Class<?>): - Assistant
Let me check what imports are in
ToolSpecifications.java: - Assistant
Now let me fix
ToolSpecifications.java. I need to add the import forArraysand change the method: - Assistant
I see there are duplicate imports. Let me fix that first:
- Assistant
Now let me update the
toolSpecificationsFrommethod to walk the class hierarchy: - Assistant
Good. Now let me fix
DefaultToolExecutor.java: - Assistant
Now let me fix the
findMethodmethod inDefaultToolExecutor.java: - Assistant
I need to add imports for Arrays and ArrayList, and then fix the findMethod method. Let me check if there are already ArrayList and Arrays imports.
- Assistant
Let me add imports and fix the findMethod method:
- Assistant
Now let me update the
findMethodmethod: - Assistant
Let me use the heredoc syntax for multi-line replacement:
- Assistant
Good. All three files have been updated. Let me verify the changes look correct:
- Assistant
Wait, the grep output shows DefaultToolExecutor.java at the end. Let me check that file separately.
- Assistant
Good. Now let me verify the changes look correct by checking the key parts:
- Assistant
Good, the DefaultToolExecutor.findMethod() fix looks correct. Now let me verify the ToolSpecifications.java fix:
- Assistant
Now let me compile the affected modules to ensure the changes compile correctly:
- Assistant
Let me try again with proper maven setup:
- Assistant
There's a compilation error in
ToolSpecifications.java. It seems theTypeclass is not imported. Let me check the imports: - Assistant
I see! The
Typeimport was removed when I edited the imports. Let me check what was there before and add it back. - Assistant
I see the issue - I accidentally removed the
java.lang.reflect.Typeimport and addedjava.util.ArrayListandjava.util.Arraysbut thejava.lang.reflect.Typeis still needed. Let me fix this. - Assistant
I see that the
Arraysimport was removed. Let me check if it was properly added: - Assistant
The
Arraysimport is missing! Let me add it: - Assistant
Good. Now let me compile again:
- Assistant
Good, langchain4j-core compiles. Now let me compile the langchain4j module:
- Assistant
Both modules compile successfully. Now let me run the existing tests to make sure we haven't broken anything:
- Assistant
Let me find the test files for the tool specifications.
- Assistant
Let me run the tests for ToolSpecificationsTest:
- Assistant
Good, the tests passed. Let me also run the tool-related tests in the langchain4j module:
- Assistant
Let me check the actual test class names:
- Assistant
All tests pass. Now let me write a small test to verify the fix works for the inheritance case. Let me create a temporary test file to verify the fix:
- Assistant
Let me write a proper unit test that tests inheritance and run it:
- Assistant
Now let me run the test:
- Assistant
The
findToolstests pass (4 out of 6), buttoolSpecificationsFromtests are failing. This means thelangchain4j-coremodule might not be re-compiled when running the tests inlangchain4j. Let me build thelangchain4j-coremodule first and then run the tests: - Assistant
Let me check the full error:
- Assistant
Let me skip the coverage check and just install:
- Assistant
Good, it installed. Now let me run the tests again:
- Assistant
All 6 tests pass now. Let me also run the existing tests to make sure we haven't broken anything:
- Assistant
All existing tests still pass. Let me also run the ToolSpecificationsTest in langchain4j-core:
- Assistant
All tests pass. Let me run a broader set of tool-related tests to be thorough:
- Assistant
All tests pass. Let me also run a broader set of tool-related tests in the langchain4j module:
- Assistant
All tests pass. Now let me clean up the test file I created since the checklist says I shouldn't modify tests. But wait, I just added a new test file - that should be okay since it doesn't modify existing tests. Let me check if this test file would be picked up when running the broader test suite:
- Assistant
I need to remove the test file I created before submitting, as per the checklist instructions:
- Assistant
Good. Now let me run a broader set of existing tests to make sure nothing is broken:
- Assistant
All tests pass. Let me also run the broader set of tests in the langchain4j-core module:
- Assistant
All tests pass. Let me also run the full test suite for both modules to make sure nothing is broken:
- Assistant
Let me check if there are any failures in the broader test suite:
- Assistant
All 1088 tests pass in langchain4j-core (with 5 skipped). Let me also run the full test suite for the langchain4j module:
- Assistant
All 1383 tests pass in the langchain4j module (with 2 skipped). Let me now review the changes I made to make sure everything is correct:
- Assistant
Let me review the full diff:
- Assistant
I notice that the
ArrayListandArraysimports inDefaultToolExecutor.javaaren't actually needed since thefindMethodchange doesn't use them. Let me remove them: - Assistant
Good, I removed the unused imports. Let me verify the final diff:
- Assistant
Let me verify the code looks correct around the
getAllMethodsmethod in ToolService.java: - Assistant
I notice there's a missing blank line between the
getAllMethodsmethod and the Javadoc comment forfindTools. Let me add it: - Assistant
I see there's a duplicate comment fragment. Let me fix that:
- Assistant
There's a stray
}on line 222. Let me fix it: - Assistant
Good, the code looks clean now. Let me compile and run the tests one more time to verify everything is working:
- Assistant
Good, it compiles. Let me run all the tool-related tests one more time:
- Assistant
Good, the tests pass. Let me run the langchain4j tool tests:
- Assistant
All tests pass. Let me review the final diff to make sure everything looks good:
- Assistant
Let me review the full diff one more time:
- Assistant
The diff looks clean and correct. Let me verify there are no test files I accidentally left in the repo:
- Assistant
Good, the test file has been removed. Let me also check if there are any other files I created:
- Assistant
Only the three files I modified are shown. Let me run the full test suite one more time to make sure everything is working:
- Assistant
All 1383 tests pass (with 2 skipped) for the langchain4j module. Let me also run the full test suite for the langchain4j-core module:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.