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'reworking with.
Assistant
Let me explore the relevant directories. The issue is about
@Toolmethods not being inherited from superclass.Assistant
Now I can see the issue. The
toolSpecificationsFrom(Class<?>)method usesclassWithTools.getDeclaredMethods()which only returns methods declared in the class itself, not those inherited from superclasses. Let me also check the related utility files.Assistant
Let me look at the AiServices class and other related code to understand how tool methods are discovered.
Assistant
Let me look at the relevant files where the issue might exist.
Assistant
Now let me look at the
getAnnotatedMethodutilitymethod.Assistant
Now let me reproduce the bug first.
Assistant
Let me search for any non-public @Tool annotated methods:
Assistant
Let me check if there are any non-public tool methods in the codebase. Most are public, but let me check.
Assistant
Now I have a clear understanding of the bug. Let me check how the bug also affects the
ToolServiceandDefaultToolExecutor:Assistant
The bug is reproduced in both files. Let me also test
DefaultToolExecutor.findMethod:Assistant
Now I have a clear understanding of the bug. Let me check existing tests for
ToolService:Assistant
Now let me think about the best approach. I'll implement the fix:
Assistant
Now I'll implement the fix. Let me first look at the complete
Utils.javato understand its structure:Assistant
Let me first write a small test to verify my approach using
getMethods():Assistant
getMethods()works as expected. It returns inherited methods and the most derived version for overridden methods. Now let me implement the fix:Assistant
Now let me update the
ToolSpecifications.javato usegetMethods()instead ofgetDeclaredMethods():Assistant
Let me make the edit properlyusing the edit tool.
Assistant
Let me use the replace toolto make the change.
Assistant
Let me use sed to make the change:
Assistant
Now let me update the JavaDoc comment to mention inheritance:
Assistant
Now let me also update the second JavaDoc comment that references the same method:
Assistant
Now let me also verify the fix works with my reproducer:
Assistant
The first fix works. Now let me update
ToolService.java:Assistant
Now let me update
DefaultToolExecutor.java:Assistant
Now let me rebuild and test:
Assistant
All three places are now fixed. Let me also test edge cases and ensure existing tests still pass:
Assistant
A test broke. Let me investigate:
Assistant
I need to handle package-private methods too. Let me revert my changes and add a helper that walks the class hierarchy:
Assistant
Let me revert the simple approach and implement a more robust solution that walks the class hierarchy:
Assistant
Good. Now let me add a helper method to
Utils.java:Assistant
Let me add the necessary imports and the helper method to
Utils.java:Assistant
Now let me add the helper method to
Utils.java:Assistant
Now let me insert this method right after the
getAnnotatedMethodmethod:Assistant
Let me also check if there are any duplicate method issues:
Assistant
Let me build to verify:
Assistant
Good. Now let me update the three places to use this helper:
Assistant
Now I need to add the import for
findAllMethodsInClassHierarchy:Assistant
Let me fix the import order:
Assistant
Now let me verify the change:
Assistant
Let me update the JavaDoc to mention inheritance:
Assistant
Now let me update
ToolService.javaandDefaultToolExecutor.java:Assistant
Now let me update
DefaultToolExecutor.java:Assistant
Let me verify the changes:
Assistant
Now let me build and test:
Assistant
I need to fix the call - I'm calling
stream()which expects an array. Let me fix it:Assistant
The langchain4j module's
langchain4j-coredependency is the published one, not the one I'm modifying. Let me check thepom to see how it's configured.Assistant
I need to install the langchain4j-core to the local Maven repository first:
Assistant
Now let me run my reproduction tests:
Assistant
All three are fixed. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All 18 tests pass. Now let me run the
DefaultToolExecutorTest:Assistant
Let me also run the broader tests for related functionality:
Assistant
All tool tests pass. Now let me write some additional tests to verify edge cases:
Assistant
All edge cases work correctly. Let me check the diff to verify my changes:
Assistant
Let me fix the import order in Utils.java:
Assistant
I duplicated some lines. Let me clean it up:
Assistant
Now let me add the imports and helper method back:
Assistant
Let me also verify the existing tests still pass:
Assistant
Let me also run any related utility tests:
Assistant
All 1088 tests in langchain4j-core pass. Let me also run the langchain4j tests:
Assistant
All 1383 tests in langchain4j pass. Let me also verify the final diff:
Assistant
Let me fix the import ordering in Utils.java to be alphabetical:
Assistant
Let me manually reorder the imports using sed:
Assistant
Now the imports are in alphabetical order. Let me rebuild and re-run all tests:
Assistant
All tests still pass. Let me also look at one more consideration - the
findMethodinDefaultToolExecutorlooks up methods by name only. With inheritance, if a method is overridden in a subclass (without@Toolannotation but with same name), the subclass version will be returned. This could potentially cause issues if the method signatures differ. Let me verify:Assistant
This is a pre-existing limitation of the
findMethod(it only matches by name, not signature). My fix doesn't introduce any new issues. Let me run a more comprehensive set of tests:Assistant
All 1088 tests in langchain4j-core pass. Let me also verify the final diff is clean:
Assistant
Let me clean up the temporary repro files:
Assistant
Now let me go through the checklist:
Assistant
Let me also check if the Kotlin module has the same issue:
Assistant
No Kotlin files use
getDeclaredMethodsor@Tooldirectly. The Kotlin module might use the Java APIs.Let me check.Assistant
The Kotlin module uses the Java APIs. Let me also check other modules that might be affected:
Assistant
Let me check
AgenticServices.java:Assistant
This is for
@Agentannotation processing, not@Tool. It's for static methods and not related to the bug. Let me focus on ensuring the fix is complete. Let me also look for other places that might be usinggetDeclaredMethods()for@Tool:Assistant
All places that discover
@Toolmethods are updated. Let me do a final comprehensive run of related tests:Assistant
All tests pass. Let me go through the checklist now:
Checklist Review
1. If an error was reported in the issue description, I have successfully reproduced it.
Yes. I created a reproduction script with:
- A superclass
SuperclassWithToolthat has@Tool-annotated methods - A concrete subclass
ConcreteSubclassthat inherits them - Initially, only methods declared in the concrete subclass were discovered, confirming the bug
2. If an error was reported in the issue description, I have confirmed that my fix resolves the error.
Yes. After applying the fix:
- All three places (
ToolSpecifications.toolSpecificationsFrom, , ) now correctly identify inherited methods
- A superclass
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.